bhaslip — 2010-01-13T13:48:17-05:00 — #1
I am having some trouble updating multiple rows at once. Below is the code. Any help is greatly appreciated.
$host="188.8.131.52"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="arcola399"; // Database name
$tbl_name="availability"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
// Count table rows
<br /><br />
<form name="form1" method="post" action="">
<table cellspacing="0" cellpadding="0" class="chart" align="center">
<td><? $id=$rows['ID']; ?><? echo $rows['ID']; ?></td>
<td><? echo $rows['Suite']; ?></td>
<td><input name="Level" type="text" id="Level" value="<? echo $rows['Level']; ?>"></td>
<td><input name="Sqft" type="text" id="Sqft" value="<? echo $rows['Sqft']; ?>"></td>
<td><input name="Available" type="text" id="Available" value="<? echo $rows['Available']; ?>"></td>
<td><input name="Extras" type="text" id="Extras" value="<? echo $rows['Extras']; ?>"></td>
</table><br /><br />
<div class="align-center"><input type="submit" name="Submit" value="Submit"></div>
// Check if button name "Submit" is active, do this
$sql1="UPDATE $tbl_name SET Level='$Level[$i]', Sqft='$Sqft[$i]', Available='$Available[$i]', Extras='$Extras[$i]' WHERE ID='$id[$i]'";
$result1=mysql_query($sql1) or die("Cannot Update");
corbyboy — 2010-01-13T14:01:10-05:00 — #2
What is the problem you are experiencing? Are you getting any error messages. While you are debugging I would get rid if the header() redirect at the end of the script.
I am not totally sure if the header redirect might be causing the mysql query to stop early when the redirect is applied.
bhaslip — 2010-01-13T14:03:22-05:00 — #3
I have removed the header redirect but it is still not updating the database. I am not receiving any errors it just seems to refresh the page.
Here is a link to the page.
corbyboy — 2010-01-13T14:07:39-05:00 — #4
You need to do a little debugging here.
echo "we have submitted";
If you see the text then you know the loop is being executed.
I suspect the line may need modifying to this:
bhaslip — 2010-01-13T14:23:36-05:00 — #5
That seems to get me closer. Now the issue seems to be with my update statement, it clears all data from my table on submit it must not be getting the values from the form.
corbyboy — 2010-01-13T14:28:09-05:00 — #6
Try echoing out the values you are putting into the sql query. Since you are using the values of a submitted form there should be $_POST in there somewhere.
Also, the form displays before the query has executed, so once you submit the form, the form will be populated with the old data.
bhaslip — 2010-01-13T15:16:52-05:00 — #7
I am not sure what is going on. If I echo out the values, everything looks right, but it still doesn't get entered in the database. There is some issue with the update statement that I can't figure out. I am just starting to learn PHP, I develop in ASP typically.
r937 — 2010-01-13T15:21:13-05:00 — #8
echo out the entire UPDATE statement, please
bhaslip — 2010-01-13T16:10:54-05:00 — #9
Thanks. After messing around trying to echo the Update statement, I figured it out. Everything is now working.
r937 — 2010-01-13T16:22:26-05:00 — #10
mind sharing what the problem was?
you had several people take time out of their day to try to help you, so it's only fair