zurih Posted November 7, 2008 Share Posted November 7, 2008 Hey, I'm trying to update a row in a table through PHP but it just doesn't update anything, and doesn't return an error. Here is the code including the HTML with the form: // get value of id that sent from address bar $id=$_GET['id']; // Retrieve data from database $sql="SELECT * FROM $table_name WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?> <html> <head> <title>Menu Admin</title> <style>* {font-family: Arial; font-size: 12px;}</style> </head> <body> <center> <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="6"><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> <td align="center"> </td> </tr> <tr> <td align="center"> </td> <td align="center"><strong>Cat ID</strong></td> <td align="center"><strong>Menu ID</strong></td> <td align="center"><strong>Cat title</strong></td> <td align="center"><strong>File name</strong></td> <td align="center"><strong>Class name</strong></td> </tr> <tr> <td> </td> <td align="center"><input name="cat_id" type="text" id="cat_id" value="<? echo $rows['cat_id']; ?>"></td> <td align="center"><input name="menu_id" type="text" id="menu_id" value="<? echo $rows['menu_id']; ?>" size="15"></td> <td><input name="cat_title" type="text" id="cat_title" value="<? echo $rows['cat_title']; ?>" size="15"></td> <td><input name="file_name" type="text" id="file_name" value="<? echo $rows['file_name']; ?>" size="15"></td> <td><input name="class_name" type="text" id="class_name" value="<? echo $rows['class_name']; ?>" 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> <? mysql_close(); ?> And this is the "update_ac.php": // 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"); mysql_query("SET NAMES utf8;"); // update data in mysql database $sql="UPDATE $table_name SET cat_id='$cat_id', menu_id='$menu_id', cat_title='$cat_title', file_name='$file_name', class_name='$class_name' WHERE id='$id'"; $result=mysql_query($sql); // if successfully updated. if($result){ echo "Successful"; echo "<BR>"; echo "<a href='list_records.php'>View result</a>"; } else { echo "ERROR"; } Anyone knows what's the problem? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/131826-update-doesnt-work-mysql/ Share on other sites More sharing options...
F1Fan Posted November 7, 2008 Share Posted November 7, 2008 What happens when you echo the query and run it manually? Quote Link to comment https://forums.phpfreaks.com/topic/131826-update-doesnt-work-mysql/#findComment-684776 Share on other sites More sharing options...
Maq Posted November 7, 2008 Share Posted November 7, 2008 Yeah you never grab the input field from the first page with the $_POST method you use. You can't use the variables. Try adding this to the top of update_rc.php: $cat_id = $_POST['cat_id']; $menu_id = $_POST['menu_id']; $cat_title = $_POST['cat_title']; $file_name = $_POST['file_name']; $class_name = $_POST['class_name']; $id = $_POST['id']; Quote Link to comment https://forums.phpfreaks.com/topic/131826-update-doesnt-work-mysql/#findComment-684778 Share on other sites More sharing options...
zurih Posted November 7, 2008 Author Share Posted November 7, 2008 What happens when you echo the query and run it manually? It works with no problem Quote Link to comment https://forums.phpfreaks.com/topic/131826-update-doesnt-work-mysql/#findComment-684781 Share on other sites More sharing options...
zurih Posted November 7, 2008 Author Share Posted November 7, 2008 Yeah you never grab the input field from the first page with the $_POST method you use. You can't use the variables. Try adding this to the top of update_rc.php: $cat_id = $_POST['cat_id']; $menu_id = $_POST['menu_id']; $cat_title = $_POST['cat_title']; $file_name = $_POST['file_name']; $class_name = $_POST['class_name']; $id = $_POST['id']; Yes! Works!! Thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/131826-update-doesnt-work-mysql/#findComment-684783 Share on other sites More sharing options...
F1Fan Posted November 7, 2008 Share Posted November 7, 2008 Maq's suggestion is a good one, as it will better your code. But, many servers have a setting that creates variables based on the keys and values of $_REQUEST ($_POST or $_GET) variables. It sounds like you were used to a server with that setting turned on, but as you have learned, you should never assume that this will work. Quote Link to comment https://forums.phpfreaks.com/topic/131826-update-doesnt-work-mysql/#findComment-684786 Share on other sites More sharing options...
zurih Posted November 7, 2008 Author Share Posted November 7, 2008 Maq's suggestion is a good one, as it will better your code. But, many servers have a setting that creates variables based on the keys and values of $_REQUEST ($_POST or $_GET) variables. It sounds like you were used to a server with that setting turned on, but as you have learned, you should never assume that this will work. Yeah.. guess my web hosting server has this setting turned on. Quote Link to comment https://forums.phpfreaks.com/topic/131826-update-doesnt-work-mysql/#findComment-684791 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.