2

Database is not getting updated after submitting. I am not even getting an error. Here I'm trying to retrieve the values from a database and displaying in the HTML Table format and also user can update the values from here. So when I change the value here and submit the database is not getting updated.

  <?php

    print"<center><h3><a href=\"index.html\">Go Back</a></h3><br>"; 


    // Connect to server and select databse.
    $con = mysql_connect('localhost','tpsadmin','tps')or die("Cannot connect to DB");
    mysql_select_db('traffic',$con)or die("Cannot select DB");


    $query = "SELECT * FROM emergency";
    $result = mysql_query($query) or die(mysql_error()); 

    // Count table rows
    $count=mysql_num_rows($result);

    //UPDATE  `emergency` SET  `t_sigid` =  '2',`e_priority` =  '3' WHERE  `emergency`.`e_jid` =70;

    if( isset($_POST['Submit'])){
        for($i=0;$i<$count;$i++){
            $sql1 = "UPDATE emergency SET 
                e_latitude='{$_POST['e_latitude'][$i]}', 
                e_longitude='{$_POST['e_longitude'][$i]}', 
                t_sigid='{$_POST['t_sigid'][$i]}', 
                e_priority='{$_POST['e_priority'][$i]}'   
                WHERE e_jid='$id[$i]'"; 
            $result1 = mysql_query($sql1) or die(mysql_error());
        }
    }


    if($result1){
        header("location: em_disp.php");
    }
    mysql_close();
    ?>



    <!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" />
    <script language="JavaScript1.1" type="text/javascript">
    <!--
    function mm_jumpmenu(targ,selObj,restore){ //v3.0
      eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
      if (restore) selObj.selectedIndex=0;
    }
    //-->
    </script>
    <title>Emergency Table - Update</title>
    </head>

    <body>


    <table width="500" border="0" cellspacing="1" cellpadding="0">
    <form name="form1" method="post" action="">
    <tr>
    <td>
    <table border="5" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#808080" width="50&#37;" text-align="center" id="AutoNumber2" bgcolor="#C0C0C0">
    <tr>
    <td align="center">e_jid</td> 
    <td align="center">amb_id</td> 
    <td align="center">e_latitude</td> 
    <td align="center">e_longitude</td> 
    <td align="center">t_sigid</td> 
    <td align="center">e_priority</td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="left"><?php $id[]=$rows['e_jid']; ?><?php echo $rows['e_jid']; ?></td>
    <td align="center"><?php echo $rows['amb_id']; ?></td>
    <td align="center"><input name="e_latitude[]" type="text" id="e_latitude" value="<?php echo $rows['e_latitude']; ?>" size="10"></td>
    <td align="center"><input name="e_longitude[]" type="text" id="e_longitude" value="<?php echo $rows['e_longitude']; ?>" size="10"></td>
    <td align="center"><input name="t_sigid[]" type="text" id="t_sigid" value="<?php echo $rows['t_sigid']; ?>" size="10"></td>
    <td align="center"><input name="e_priority[]" type="text" id="e_priority" value="<?php echo $rows['e_priority']; ?>" size="10" /></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="6" align="center"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    </table>
    </td>
    </tr>
    </form>
    </table>



    </body>
    </html>

Please some one help me. thanks !

11
  • e_jid='$id[$i]' should not have ' as it is an integer. Commented Apr 26, 2013 at 9:24
  • replace $result1 = mysql_query($sql1) or die(mysql_error()) with echo "$sql1", and post the queries that get printed. Commented Apr 26, 2013 at 9:24
  • Does if( isset($_POST['Submit'])){ verify TRUE after post ? Commented Apr 26, 2013 at 9:25
  • imho for($i=0;$i<$count;$i++){ looks wrong. Either your WHERE-clause is enough to qualify the "to be updated" entries, or its not. I dont see why you need to eval $count and or the prior SELECT * on emergency table. Commented Apr 26, 2013 at 9:26
  • @Ejay here is the query output. "UPDATE emergency SET e_latitude='13.786584000000', e_longitude='77.996540000000', t_sigid='2', e_priority='3' WHERE e_jid=UPDATE emergency SET e_latitude='0.000000000000', e_longitude='0.000000000000', t_sigid='3', e_priority='2' WHERE e_jid=''" Commented Apr 26, 2013 at 10:01

3 Answers 3

2

You forget the curly braces at $id[$i].

That, and don't put $_POST values directly in your query. Use MySQLi or PDO instead.

There is more wrong with your code by the way:

  1. the header might not work on other places, as you output data at line 1 (you seem to rely on some server setting)
  2. This: <?php $id[]=$rows['e_jid']; ?> does nothing else than generating a PHP notice.
Sign up to request clarification or add additional context in comments.

1 Comment

header() might work if output_buffering is enabled (which is by default on a lot of places)
0

It seems t_sigid,e_priority and e_jid are integer. so below code is safer.

for($i=0;$i<$count;$i++){
    $t_sigid=intval($_POST['t_sigid'][$i]);
    $e_priority=intval($_POST['e_priority'][$i]);
    $e_jid=intval($id[$i]);
    $sql1 = "UPDATE emergency SET 
        e_latitude='{$_POST['e_latitude'][$i]}', 
        e_longitude='{$_POST['e_longitude'][$i]}', 
        t_sigid='$t_sigid', 
        e_priority='$e_priority'   
        WHERE e_jid='$e_jid'"; 
    $result1 = mysql_query($sql1) or die(mysql_error());
}

EDIT 1:

change

  <td align="left"><?php $id[]=$rows['e_jid']; ?><?php echo $rows['e_jid']; ?></td>

to

   <td align="left"><input name="id[]" type="text" id="id" value="<?php echo $rows['e_jid']; ?>" size="10"></td>

and also change $e_jid=intval($id[$i]); which i mentioned above to $e_jid=intval($_POST['id'][$i]);

Comments

0

You never POST $id[] !

I used to POST it :

<input type="hidden" name="idArray[]" value="<?php echo $rows['e_jid']; ?>" >

and toget it

WHERE e_jid={$_POST['idArray'][$i]}";

You should note the following points.

  • Never make real updates in the development stage.
  • Watch with "View page source code" always at the html generated.
  • To keep it simple, always use just a few data from tables.
  • SQL: String data in quotes. Numbers without quotes.

You can use following code to test

...
Here, come in, your instructions database.
should be : mysqli or PDO instructions.
...

if( isset($_POST['Submit'])){
        for($i=0;$i<$count;$i++){
            $sql1 = "UPDATE emergency SET 
                e_latitude='{$_POST['e_latitude'][$i]}' 
                WHERE e_jid={$_POST['idArray'][$i]}"; 
                echo "sql ".$sql1."<br />\n";
        }
    if($result1){
       ... Database close action...
       header("location: em_disp.php");
       }
    }

<table width="500" border="0" cellspacing="1" cellpadding="0">
  <form name="form1" method="post" action="">
    <tr>
    <td>
    <table border="5" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#808080" width="50&#37;" text-align="center" id="AutoNumber2" bgcolor="#C0C0C0">
      <tr>
         <td align="center">e_jid</td> 
         <td align="center">amb_id</td> 
         <td align="center">e_latitude</td>
      </tr>
    <?php
    while($rows=....){
    ?>
  <tr>
         <td align="left"><?php echo $rows['e_jid']; ?></td>
          <input type="hidden" name="idArray[]" value="<?php echo $rows['e_jid']; ?>" >
         <td align="center"><?php echo $rows['amb_id']; ?></td>                              
         <td align="center"><input name="e_latitude[]" type="text" id="e_latitude" value="<?php echo $rows['e_latitude']; ?>" size="10"></td>
      </tr>
    <?php
    }
    ?>
  <tr>
         <td colspan="6" align="center"><input type="submit" name="Submit" value="Submit"></td>
      </tr>
    </table>
    </td>
    </tr>
  </form>
</table>
</body>
</html>

Watch with "View page source code" always at the html generated. Looks good :

enter image description here

The output after submit :

enter image description here

Other actions only when it is working properly until then.

If you had followed this, never had your question looked like this.

Database is not updating ! No error in PHP & MySQL !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.