#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    How to update multiple rows in one call


    Hello friends, i am very new on this php world and i am trying to create a php file that updates multiple rows in one button.
    I use dreamweaver, and as you now it creates a lot of info when using the wizard.
    I have search the web and none of the tutorials seems to work. This is the code i have, I shows all the register of the table "relatores" but when press the submit button it does not update anything.

    PHP Code:
    <?php require_once('Connections/gestion.php'); ?>
    <?php
    header
    ('Content-Type: text/html; charset=ISO-8859-1');
    ?>
    <?php
    function GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
    {
      
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

      switch (
    $theType) {
        case 
    "text":
          
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;    
        case 
    "long":
        case 
    "int":
          
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case 
    "double":
          
    $theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
          break;
        case 
    "date":
          
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;
        case 
    "defined":
          
    $theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
          break;
      }
      return 
    $theValue;
    }

    $editFormAction $_SERVER['PHP_SELF'];
    if (isset(
    $_SERVER['QUERY_STRING'])) {
      
    $editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
    }

    if ((isset(
    $_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
    for(
    $i=0;$i<$count;$i++){ 
      
    $updateSQL sprintf("UPDATE relatores SET id_estado=%s WHERE Id_relator=%s",
                           
    GetSQLValueString($_POST['id_estado'], "int"),
                           
    GetSQLValueString($_POST['Id_relator'], "int"));

      
    mysql_select_db($database_gestion$gestion);
      
    $Result1 mysql_query($updateSQL$gestion) or die(mysql_error());


      
    $updateGoTo "oo";
      if (isset(
    $_SERVER['QUERY_STRING'])) {
        
    $updateGoTo .= (strpos($updateGoTo'?')) ? "&" "?";
        
    $updateGoTo .= $_SERVER['QUERY_STRING'];
      }
      
    header(sprintf("Location: %s"$updateGoTo));
    }

    mysql_select_db($database_gestion$gestion);
    $query_cambiaestadorelator "SELECT relatores.Id_relator, relatores.nombre_relator, relatores.ap_paterno_relator, relatores.ap_materno_relator, relatores.id_region, relatores.id_estado FROM relatores";
    $cambiaestadorelator mysql_query($query_cambiaestadorelator$gestion) or die(mysql_error());
    $row_cambiaestadorelator mysql_fetch_assoc($cambiaestadorelator);
    $totalRows_cambiaestadorelator mysql_num_rows($cambiaestadorelator);
    $result=mysql_query($query_cambiaestadorelator$gestion); 
    $count mysql_num_rows($result); 

    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Documento sin t&iacute;tulo</title>
    </head>

    <body>
    <form method="post" name="form1" action="<?php echo $editFormAction?>">
    <table border="1" width="460" nowrap="NOWRAP">
      <tr background="images/bannerflash.gif" height="32" >
        <td align="center" nowrap="NOWRAP" class="titulos" width="20">Id</td>
        <td align="center" nowrap="NOWRAP" class="titulos" width="100">Nombre</td>
        <td align="center" nowrap="NOWRAP" class="titulos" width="100">Ap. Paterno</td>
        <td align="center" nowrap="NOWRAP" class="titulos" width="100">Ap. Materno</td>
        <td align="center" nowrap="NOWRAP" class="titulos" width="100">Regi&oacute;n</td>
        <td align="center" nowrap="NOWRAP" class="titulos" width="40">Estado</td>
      </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
     <tr valign="baseline">
          <td><? $id[]=$rows['Id_relator']; ?><? echo $rows['Id_relator']; ?></td>
          <td><?php echo $rows['nombre_relator']; ?></td>
          <td><?php echo $rows['ap_paterno_relator']; ?></td>
          <td><?php echo $rows['ap_materno_relator']; ?></td>
          <td><?php echo $rows['id_region']; ?></td>
          <td><input type="text" name="id_estado[]" value="<?php echo $rows['id_estado']; ?>" size="32"></td>
        </tr>
        <?php
    }
    ?>
        <tr valign="baseline">
          <td nowrap align="right">&nbsp;</td>
          <td><input type="submit" value="Actualizar registro"></td>
        </tr>
      </table>
      <input type="hidden" name="MM_update" value="form1">
      <input type="hidden" name="Id_relator" value="<?php echo $row_cambiaestadorelator['Id_relator']; ?>">
    </form>
    <p>&nbsp;</p>
    </body>
    </html>
    <?php
    mysql_free_result
    ($cambiaestadorelator);
    ?>
    Last edited by ManiacDan; April 8th, 2013 at 08:07 AM. Reason: added PHP tags
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    1) While I appreciate your attempt at being explicit with sprintf, you should be using PDO and not the deprecated mysql extension. Look into how to do prepared statements with PDO, it's a similar structure to what you have now.

    2) You did absolutely zero debugging of this code on your own. I'll show you how debugging works:

    - you say update isn't failing. Find update
    - there's no error checking on the query (naturally). Add some. But before you re-run the script...
    - It's an UPDATE so the WHERE clause may be wrong. What variable is in the WHERE?
    - It comes from $_POST, so it's a form element. You can print_r($_POST) and examine it to find out what's wrong, or...
    - Go down to your form and look for that element.
    - Its name ends with []. That means it's an array and you're using it wrong in your query.

    Ta-da, your issue is debugged. You were using an array as a scalar.

    The new user guide contains more debugging tips.

    Comments on this post

    • ptr2void agrees : You have a Dreamweaver user here, utilizing a wizard. To expect debugging skills is unfortunately setting the bar too high.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo