allyant Posted September 2, 2007 Share Posted September 2, 2007 Hey, new guy to here and PHP, Im having a problem updating a database, I ask the database to update but it inserts a new field insted, here is the update.php: <?php $connection = mysql_connect("localhost","root","********"); $db_select = mysql_select_db("address",$connection); $id = $_GET['id']; $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; if (intval($_GET['id']) == NULL) { header("Location: mysql.php"); } if ($name && $email && $phone != NULL) { $query = "UPDATE table SET `name` = '$name' WHERE `id` = '$id' LIMIT 1"; $result = mysql_query($query, $connection); } if (isset($id)) { $result = mysql_query("SELECT * FROM simple WHERE id = '{$id}'", $connection); $row = mysql_fetch_array($result); } ?> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>Edit A Contact</title> </head> <body > <h1>Edit A Contact</h1> <br /> <form action="newcontact.php" method="post"> Name: <input type="text" name="name" value="<?php echo $row["name"] ?>" /><br /> Email: <input type="text" name="email"value="<?php echo $row["email"] ?>" /><br /> Phone: <input type="text" name="phone"value="<?php echo $row["phone"] ?>" /><br /> <input type="submit"> </form> </body> <?php mysql_close($connection); ?> When I submit the updated information rather than updating the information it adds a new row with the information. I am using PHP with MySQL. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/67649-solved-mysql-inserts-instead-of-updates/ Share on other sites More sharing options...
recklessgeneral Posted September 2, 2007 Share Posted September 2, 2007 Hi allayant, I'm fairly new to PHP and stuff myself, but I've run into numerous problems now that I'm getting better at spotting them. The first thing I noticed is that your form goes to newcontact.php instead of the update.php you posted. I imagine that'll be why the update statement never happens. The second thing is that when you do get to the update.php page, there are a couple of issues with that also. You are trying to get the id from the _GET array, although the form has been posted. What I've done in these situations is to add a hidden field to your form along the lines of <input type="hidden" name="id" value="<?php echo $_GET['id'];?>"> You can then extract the ID from from the $_POST array as you do for the other fields. I'd also recommend doing some validation on the posted values, at least do a mysql_real_escape_string on the values before passing them to your mysql_query call. Hope this helps, Darren. Quote Link to comment https://forums.phpfreaks.com/topic/67649-solved-mysql-inserts-instead-of-updates/#findComment-339811 Share on other sites More sharing options...
allyant Posted September 2, 2007 Author Share Posted September 2, 2007 Thanks Darren! Thats It working fine now, I must have forgot to change the form action when I copied it from the newcontact.php script. Once again thanks! Quote Link to comment https://forums.phpfreaks.com/topic/67649-solved-mysql-inserts-instead-of-updates/#findComment-339816 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.