Undefined variable: result1 in D:\wamp\www\update_multiple.php on line

please find below code…getting error while running this code…please help to solve the code…

[size=10pt][font=verdana]

?php

mysql_connect (“localhost”,“root”,"") or die (“could not connect to server”);
mysql_select_db(“cpe”) or die (“database error”);

$sql = “select * from test_mysql”;
$result = mysql_query($sql);

$count = mysql_num_rows ($result);
?>

<?php while ($rows = mysql_fetch_array($result)) { ?> <?php } ?>
ID Name Job No Time1 Time2
<? $id [] = $rows['id'];?>
<?php if (isset($_post['Submit'])){ for ($i=0;$i<$count;$i++){ $sql1 = "update test_mysql set name='$name[$i]', job='$job[$i]', time1='$time1[$i]', time2='$time2[$i]' where id='$id[$i]'"; $result1 = mysql_query ($sql1); } } if ($result1){ header('location:update_multiple.php'); } mysql_close(); ?>

[/font][/size]

Well, first, you should look into moving up to MySQLi (the ‘i’ is for improved) as the older MySQL has been
deprecated and will not work at some point in the future. There are only a few minor changes to your code
to use the newer improved version.

Now, near the end of your code, you have this:

if (isset($_post['Submit'])){ for ($i=0;$i<$count;$i++){ $sql1 = "update test_mysql set name='$name[$i]', job='$job[$i]', time1='$time1[$i]', time2='$time2[$i]' where id='$id[$i]'"; $result1 = mysql_query ($sql1); } } if ($result1){ header('location:update_multiple.php'); }
If you look at just that outside of the rest of the code, you will see that you close the IF clause before you check to see if there are results. The problem is that if a user does NOT press the SUBMIT button, the PHP code still evaluates the results and since it does not exist until they press the button, it throw an error. Just take out the extra closing brace right before the checking of results... Like this: [php] if (isset($_post['Submit'])){ for ($i=0;$i<$count;$i++){ $sql1 = "update test_mysql set name='$name[$i]', job='$job[$i]', time1='$time1[$i]', time2='$time2[$i]' where id='$id[$i]'"; $result1 = mysql_query ($sql1); } if ($result1){ header('location:update_multiple.php'); }[/php] But, this brings up another big problem. You are looping thru a number of queries that do updates on a table. Then, you just check the last query results. Not sure if that is a good thing to do. If the first of five queries worked and the last one did not, you would not get a valid results. Most likely you should update all of the data and then create a new query that counts the table's entries. Not sure of your logic there.

Hope that helps… Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service