Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/67649-solved-mysql-inserts-instead-of-updates/
Share on other sites

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.