MirceaLzr Posted August 17, 2011 Share Posted August 17, 2011 Hello first, secondly I'm glad to join your community and I hope i learn something. I have a script that I use for a website where should I edit and update a table in the database. Everything is almost better but when i press submit to make the changes in the database the script say that is Successful but in the table nothing change. The script is: Create file - list_records.php ############### Code <?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // 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>Name</strong></td> <td align="center"><strong>Lastname</strong></td> <td align="center"><strong>Email</strong></td> <td align="center"><strong>Update</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td><? echo $rows['name']; ?></td> <td><? echo $rows['lastname']; ?></td> <td><? echo $rows['email']; ?></td> // link to update.php and send value of id <td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td> </tr> <?php } ?> </table> </td> </tr> </table> <?php mysql_close(); ?> The script is: Create file - update.php ############### Code <?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // 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>Name</strong></td> <td align="center"><strong>Lastname</strong></td> <td align="center"><strong>Email</strong></td> </tr> <tr> <td> </td> <td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td> <td align="center"><input name="lastname" type="text" id="lastname" value="<? echo $rows['lastname']; ?>" size="15"></td> <td><input name="email" type="text" id="email" value="<? echo $rows['email']; ?>" 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> <? // close connection mysql_close(); ?> The script is: Create file - update_ac.php ############### Code <?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // 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 name='$name', lastname='$lastname', email='$email' 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"; } ?> 1.Open list_records.php 2.Press Update Button 3.Edit name or email and press submit 4.The screen say Successful but nothing change in db. If you can help me or tell me what to edit or delete or insert tu make this scripts work i'll be happy.Pleasant day. Quote Link to comment https://forums.phpfreaks.com/topic/244986-update-mysql-table-problem/ Share on other sites More sharing options...
MirceaLzr Posted August 17, 2011 Author Share Posted August 17, 2011 Sorry for double post.I USE MYSQL SERVR 5.0 AND FOR DB NAVICAT Quote Link to comment https://forums.phpfreaks.com/topic/244986-update-mysql-table-problem/#findComment-1258448 Share on other sites More sharing options...
fenway Posted August 17, 2011 Share Posted August 17, 2011 Please don't use giant bold fonts. Echo the sql statement you think isn't working. Quote Link to comment https://forums.phpfreaks.com/topic/244986-update-mysql-table-problem/#findComment-1258490 Share on other sites More sharing options...
MirceaLzr Posted August 17, 2011 Author Share Posted August 17, 2011 Please don't use giant bold fonts. Echo the sql statement you think isn't working. Sorry.I'm new here and novice with php and mysql code.The problem is in the update_ac.php but i dont now how to echo the statement.Can you give me one exemple or make some exemple directly to the update_ac.pgp file?Thnks Quote Link to comment https://forums.phpfreaks.com/topic/244986-update-mysql-table-problem/#findComment-1258491 Share on other sites More sharing options...
Muddy_Funster Posted August 17, 2011 Share Posted August 17, 2011 change your code to the following: // if successfully updated. if($result){ echo "Successful"; echo "<BR>"; echo "<a href='list_records.php'>View result</a>"; } else { die ("ERROR: <br>".mysql_error()."<br>THIS ERROR OCCURED WHEN RUNNING THE FOLLOWING QUERY:<br><br>$sql"); //--------this is the line that is changed } Quote Link to comment https://forums.phpfreaks.com/topic/244986-update-mysql-table-problem/#findComment-1258513 Share on other sites More sharing options...
MirceaLzr Posted August 17, 2011 Author Share Posted August 17, 2011 I make the change. The result is the same.Nothing change:( ############### Code Successful View result The corect code is <?php ini_set("display_errors", "10"); error_reporting(E_ALL); $host="localhost"; // Host name $username="root"; // Mysql username $password="cocacola"; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // 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"); $name = $_POST['name']; // get the value from the form field named "stitle" $lastname = $_POST['lastname']; // get the value from the form field named "datepicker" $email= $_POST['email']; $id = $_POST['id']; // update data in mysql database $sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' 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"; } ?> I find the corect syntax for the code and now work.Thanks for the support anyway Quote Link to comment https://forums.phpfreaks.com/topic/244986-update-mysql-table-problem/#findComment-1258527 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.