lildamien09 Posted September 29, 2009 Share Posted September 29, 2009 okay heres the problem.. i can pull data and insert new data into the sql.. but when i goto update over existing data it says the process is successful but its not actually updating the table.. heres a look at my code from the form and the process pages.. FORM.PHP: <?php $host="localhost"; // Host name $username="xxxx"; // Mysql username $password="xxxxx"; // Mysql password $db_name="xxxx"; // Database name $tbl_name="xxxx"; // 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="process.php"> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td> </td> <td colspan="3"><strong>Update {TNT} Poker Bank Data</strong> </td> </tr> <tr> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> </tr> <tr> <td align="center"> </td> <td align="center"><strong>Username</strong></td> <td align="center"><strong>Balance Owed</strong></td> </tr> <tr> <td> </td> <td align="center"><input name="xusername" type="hidden" id="xusername" value="<?php echo $rows['xusername']; ?>"><?php echo $rows['xusername']; ?></td> <td align="center"><input name="bankbalance" type="text" id="bankbalance" value="<?php echo $rows['bankbalance']; ?>" size="15"></td> </tr> <tr> <td> </td> <td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td> <td align="center"><input type="submit" name="Submit" value="Submit"></td> <td> </td> </tr> </table> </td> </form> </tr> </table> PROCESS.PHP: <?php $host="localhost"; // Host name $username="xxxx"; // Mysql username $password="xxxxx"; // Mysql password $db_name="xxxx"; // Database name $tbl_name="xxxx"; // 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 xusername='$xusername', bankbalance='$bankbalance' WHERE id='$id' "; $result=mysql_query($sql); // if successfully updated. if($result){ echo "Update Was Successful"; echo "<BR>"; echo "<a href='index.php'>Click To Go Back To Main Bank Page!</a>"; } else { echo "ERROR"; } ?> Any suggestions on why its not actually updating the table with the new values... Quote Link to comment https://forums.phpfreaks.com/topic/175980-update-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 29, 2009 Share Posted September 29, 2009 An update query that executes without any errors returns a TRUE value. Your existing code is testing that. You should actually be testing the value returned by mysql_affected_rows to determine if the update actually changed a row(s). Your update query is not updating any row because the WHERE clause is false. Where in PROCESS.PHP are you setting $id to a value so that the WHERE clause would have a value to match with the id column? Quote Link to comment https://forums.phpfreaks.com/topic/175980-update-help/#findComment-927270 Share on other sites More sharing options...
xangelo Posted September 29, 2009 Share Posted September 29, 2009 You're not actually gathering the values in process.php. Since you're using the POST method, your code should look more like this: <?php $host="localhost"; // Host name $username="xxxx"; // Mysql username $password="xxxxx"; // Mysql password $db_name="xxxx"; // Database name $tbl_name="xxxx"; // 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"); $xusername = $_POST['xusername']; $bankbalance = $_POST['bankbalance']; $id = $_POST['id']; // update data in mysql database $sql="UPDATE $tbl_name SET xusername='$xusername', bankbalance='$bankbalance' WHERE id='$id' "; $result=mysql_query($sql); // if successfully updated. if($result){ echo "Update Was Successful"; echo "<BR>"; echo "<a href='index.php'>Click To Go Back To Main Bank Page!</a>"; } else { echo "ERROR"; } ?> That will load the values from the previous page into the variables you used. However, note that the code above does not contain ANY error checking what-so-ever. Meaning if you went to process.php it would still process the page and xusername/bankbalance/id would all be empty. Also, you have to add your own value-checks (strip_tags(), mysql_escape_string() etc) Quote Link to comment https://forums.phpfreaks.com/topic/175980-update-help/#findComment-927275 Share on other sites More sharing options...
lildamien09 Posted September 29, 2009 Author Share Posted September 29, 2009 Your update query is not updating any row because the WHERE clause is false. Where in PROCESS.PHP are you setting $id to a value so that the WHERE clause would have a value to match with the id column? i have the id hidded inside the form, so it should pull it from there correct? You're not actually gathering the values in process.php. Since you're using the POST method, your code should look more like this i changed my code to be similar to what you posted but it returns ERROR. the error is because which i know this, i dont need to _post['id'] the id is auto_incremented in the database. when i remove the _post['id'] the form displays successful entry but still is not updating the table.. Quote Link to comment https://forums.phpfreaks.com/topic/175980-update-help/#findComment-927298 Share on other sites More sharing options...
lildamien09 Posted September 29, 2009 Author Share Posted September 29, 2009 ok i fixed it myself anyways thinking about what you said as far as where is the process.php getting the id from so i add into the <form method="post" action="process.php?id=<?php echo $rows['id']; ?>">... ?id=<?php echo $rows['id']; ?>" Quote Link to comment https://forums.phpfreaks.com/topic/175980-update-help/#findComment-927304 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.