god_zun Posted September 11, 2008 Share Posted September 11, 2008 I have a code that is supposed to update the shipping status of products on my site, however it everything seems to work except that function. When I go through the program it says the record has been updated, but nothing takes effect. Can someone give me any suggestions? list_shippingrecords.php <?php $host="localhost"; // Host name $username="intranet"; // Mysql username $password="int$123"; // Mysql password $db_name="Intranet"; // Database name $tbl_name="shipping"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td> <table width="400" border="1" cellspacing="0" cellpadding="3"> <tr> <td colspan="4"><strong>List data from mysql </strong> </td> </tr> <tr> <td align="center"><strong>Date</strong></td> <td align="center"><strong>Merchant</strong></td> <td align="center"><strong>Tracking Number</strong></td> <td align="center"><strong>Status</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td><?php echo $rows['Date']; ?></td> <td><?php echo $rows['Merchant']; ?></td> <td><?php echo $rows['TrackingNum']; ?></td> <td><?php echo $rows['rest_cat']; ?></td> // link to update.php and send value of id <td align="center"><a href="update.php?ID=<?php echo $rows['ID']; ?>">update</a></td> </tr> <?php } ?> </table> </td> </tr> </table> <?php mysql_close(); ?> update.php <?php $host="localhost"; // Host name $username="intranet"; // Mysql username $password="int$123"; // Mysql password $db_name="Intranet"; // Database name $tbl_name="shipping"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // get value of id that sent from address bar $ID=$_GET['ID']; // Retrieve data from database $sql="SELECT * FROM $tbl_name WHERE ID='$ID'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <form name="form1" method="post" action="update_ac.php"> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td> </td> <td colspan="3"><strong>Update data in mysql</strong> </td> </tr> <tr> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> </tr> <tr> <td align="center"> </td> <td align="center"><strong>Date</strong></td> <td align="center"><strong>Merchant</strong></td> <td align="center"><strong>Tracking Number</strong></td> </tr> <tr> <td> </td> <td align="center"><input name="name" type="text" id="name" value="<?php echo $rows['Date']; ?>"></td> <td align="center"><input name="lastname" type="text" id="lastname" value="<?php echo $rows['Merchant']; ?>" size="15"></td> <td><input name="email" type="text" id="email" value="<?php echo $rows['TrackingNum']; ?>" size="15"></td> </tr> <tr> <td> </td> <td><input name="ID" type="hidden" id="ID" value="<?php echo $rows['ID']; ?>"></td> <td align="center"><input type="submit" name="Submit" value="Submit"></td> <td> </td> </tr> </table> </td> </form> </tr> </table> <?php // close connection mysql_close(); ?> update_ac.php <?php $host="localhost"; // Host name $username="intranet"; // Mysql username $password="int$123"; // Mysql password $db_name="Intranet"; // Database name $tbl_name="shipping"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // update data in mysql database $sql="UPDATE $tbl_name SET Date='$Date', Merchant='$Merchant', TrackingNum='$TrackingNum' WHERE ID='$ID'"; $result=mysql_query($sql); // if successfully updated. if($result){ echo "Successful"; echo "<BR>"; echo "<a href='list_shippingrecords.php'>View result</a>"; } else { echo "ERROR"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/ Share on other sites More sharing options...
pocobueno1388 Posted September 11, 2008 Share Posted September 11, 2008 Change $result=mysql_query($sql); To $result=mysql_query($sql)or die(mysql_error() ."<p>With Query: $sql"); Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639056 Share on other sites More sharing options...
DeanWhitehouse Posted September 11, 2008 Share Posted September 11, 2008 Add die statements to all of your sql querys , when debugging. Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639058 Share on other sites More sharing options...
god_zun Posted September 11, 2008 Author Share Posted September 11, 2008 Okay the die statements have been added Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639061 Share on other sites More sharing options...
DeanWhitehouse Posted September 11, 2008 Share Posted September 11, 2008 Then read the errors produced by them and fix them Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639064 Share on other sites More sharing options...
god_zun Posted September 11, 2008 Author Share Posted September 11, 2008 no errors were produced. It says that the update was successful, however they don't show on the database. Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639067 Share on other sites More sharing options...
DeanWhitehouse Posted September 11, 2008 Share Posted September 11, 2008 $sql="UPDATE $tbl_name SET Date='$Date', Merchant='$Merchant', TrackingNum='$TrackingNum' WHERE ID='$ID'"; Where is $date , etc. defined? Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639069 Share on other sites More sharing options...
god_zun Posted September 11, 2008 Author Share Posted September 11, 2008 Within a separate script Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639079 Share on other sites More sharing options...
DeanWhitehouse Posted September 11, 2008 Share Posted September 11, 2008 Vars dont carry over scripts like that . Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639081 Share on other sites More sharing options...
god_zun Posted September 11, 2008 Author Share Posted September 11, 2008 okay, I've added the variable within the update_ac script ad still no luck. <?php $Date=$_POST['Date']; $Merchant=$_POST['Merchant']; $Tracking=$_POST['TrackingNum']; $host="localhost"; // Host name $username="intranet"; // Mysql username $password="int$123"; // Mysql password $db_name="Intranet"; // Database name $tbl_name="shipping"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // update data in mysql database $sql="UPDATE $tbl_name SET Date='$Date', Merchant='$Merchant', TrackingNum='$TrackingNum' WHERE ID='$ID'"; $result=mysql_query($sql)or die(mysql_error() ."<p>With Query: $sql"); // if successfully updated. if($result){ echo "Successful"; echo "<BR>"; echo "<a href='list_shippingrecords.php'>View result</a>"; } else { echo "ERROR"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639086 Share on other sites More sharing options...
DeanWhitehouse Posted September 11, 2008 Share Posted September 11, 2008 Ok, you have no clue what your doing , do you? When you post a var , you retrieve it using $_POST[''];, and what you put in between the braces is the name of the field the value is in. e.g. <input name="name" type="text" id="name" value="<?php echo $rows['Date']; ?>"> //The post var would be $_POST['name']; , not $_POST['date']; Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639090 Share on other sites More sharing options...
PFMaBiSmAd Posted September 11, 2008 Share Posted September 11, 2008 Turning on full php error_reporting and display_errors would expose which variables are not being set due to the form field names, the $_POST names, and the variable names that do not match. Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639107 Share on other sites More sharing options...
god_zun Posted September 11, 2008 Author Share Posted September 11, 2008 Vars defined and still no success. Here's what I have now for update_ac.php <?php $Date=$_POST['Date']; $Merchant=$_POST['Merchant']; $TrackingNum=$_POST['TrackingNum]' $host="localhost"; // Host name $username="intranet"; // Mysql username $password="int$123"; // Mysql password $db_name="Intranet"; // Database name $tbl_name="shipping"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // update data in mysql database $sql="UPDATE $tbl_name SET Date='$Date', Merchant='$Merchant', TrackingNum='$TrackingNum' WHERE ID='$ID'"; $result=mysql_query($sql)or die(mysql_error() ."<p>With Query: $sql"); // if successfully updated. if($result){ echo "Successful"; echo "<BR>"; echo "<a href='list_shippingrecords.php'>View result</a>"; } else { echo "ERROR"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639150 Share on other sites More sharing options...
DeanWhitehouse Posted September 11, 2008 Share Posted September 11, 2008 omg, PLEASE read what i wrote above about post values ^^, then fix your code . Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639153 Share on other sites More sharing options...
Btown2 Posted September 11, 2008 Share Posted September 11, 2008 I dont know if this is the main issue, but you have a syntax error in your last post var. You have $TrackingNum=$_POST['TrackingNum]' it should be $TrackingNum=$_POST['TrackingNum']; Also just saw somthing else. I think your if statement that checks $result is a tautology. Since $result wouldn't be null because it would evaluate as true all the time, thus lieing to you. But i am rusty on my php so i could be wrong. Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639161 Share on other sites More sharing options...
god_zun Posted September 11, 2008 Author Share Posted September 11, 2008 Blade the VARS is fixed, and I still get the error. here is the form... <?php $host="localhost"; // Host name $username="intranet"; // Mysql username $password="int$123"; // Mysql password $db_name="Intranet"; // Database name $tbl_name="shipping"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // get value of id that sent from address bar $ID=$_GET['ID']; // Retrieve data from database $sql="SELECT * FROM $tbl_name WHERE ID='$ID'"; $result=mysql_query($sql)or die(mysql_error() ."<p>With Query: $sql"); $rows=mysql_fetch_array($result); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <form name="form1" method="post" action="update_ac.php"> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td> </td> <td colspan="3"><strong>Update data in mysql</strong> </td> </tr> <tr> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> </tr> <tr> <td align="center"> </td> <td align="center"><strong>Date</strong></td> <td align="center"><strong>Merchant</strong></td> <td align="center"><strong>Tracking Number</strong></td> </tr> <tr> <td> </td> <td align="center"><input name="Date" type="text" id="Date" value="<?php echo $rows['Date']; ?>"></td> <td align="center"><input name="Merchant" type="text" id="Merchant" value="<?php echo $rows['Merchant']; ?>" size="15"></td> <td><input name="TrackingNum" type="text" id="TrackingNum" value="<?php echo $rows['TrackingNum']; ?>" size="15"></td> </tr> <tr> <td> </td> <td><input name="ID" type="hidden" id="ID" value="<?php echo $rows['ID']; ?>"></td> <td align="center"><input type="submit" name="Submit" value="Submit"></td> <td> </td> </tr> </table> </td> </form> </tr> </table> <?php // close connection mysql_close(); ?> Along with update_ac.php... <?php $Date=$_POST['Date']; $Merchant=$_POST['Merchant']; $TrackingNum=$_POST['TrackingNum']; $host="localhost"; // Host name $username="intranet"; // Mysql username $password="int$123"; // Mysql password $db_name="Intranet"; // Database name $tbl_name="shipping"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // update data in mysql database $sql="UPDATE $tbl_name SET Date='$Date', Merchant='$Merchant', TrackingNum='$TrackingNum' WHERE ID='$ID'"; $result=mysql_query($sql)or die(mysql_error() ."<p>With Query: $sql"); // if successfully updated. if($result){ echo "Successful"; echo "<BR>"; echo "<a href='list_shippingrecords.php'>View result</a>"; } else { echo "ERROR"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639190 Share on other sites More sharing options...
DeanWhitehouse Posted September 11, 2008 Share Posted September 11, 2008 where is $ID defined?? WHERE ID='$ID'" Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639197 Share on other sites More sharing options...
god_zun Posted September 11, 2008 Author Share Posted September 11, 2008 Problem Solved. Please forgive my ignorance, I am new to PHP programming, and may find myself in here often. Thanks for the assistance. The problem was that I had the ID var defined as GET instead of POST on a separate page. Once I moved it to the update_ac.php page and switched it to POST, it worked. Thanks for the map and directions. Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639202 Share on other sites More sharing options...
DeanWhitehouse Posted September 11, 2008 Share Posted September 11, 2008 Ok, click solved at the bottom of the page. Also try out some tutorials here http://www.w3schools.com/PHP/ and http://www.tizag.com/phpT/ Quote Link to comment https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/#findComment-639205 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.