I have 3 pages: search.html. search.php, and edit.php
I also have new.php which I can use to create an entry into my MySQL database, and it works perfectly, I can view the data and it’s where it’s supposed to be. Then, I have search.html and search.php which I use to do a very basic search of the database, and it again works perfectly.
Then, in the results (on search.php) I have an edit link that launches edit.php with the data beside the returned results, and it loads edit.php along with the correct data. So I think I’m ok up to this point. But then, when I edit my form and click submit, it overwrites every entry in the database, instead of just the entry that I initially tried to edit. Been messing with this for about an hour now and not sure where I’ve messed up. Code below:
Search.html
<html>
<head>
<title>Search the Database</title>
</head>
<body>
<form action="search.php" method="post">
Search:
<input type="text" name="term" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Search.php
<?php
mysql_connect ("myhost", "myuser","mypass") or die (mysql_error());
mysql_select_db ("mydb");
$term = $_POST['term'];
$sql = mysql_query("select * from mytable where fname like '%$term%'");
while ($row = mysql_fetch_array($sql)){
echo '<br/> First Name: '.$row['fname'];
echo '<br/> Last Name: '.$row['lname'];
echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
echo '<br/><br/>';
}
?>
Edit.php
<?php
function renderForm($id, $fname, $lname)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<div id="stylized" class="myform">
<form id="form" name="form" action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<h1>Edit Entry</h1>
<Label>First Name/Organization </label>
<input type="text" name="fname" value="<?php echo $fname; ?>"/>
<Label>Attn. </label>
<input type="text" name="attn" value="<?php echo $attn; ?>"/>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
include('settings.php');
if (isset($_POST['submit']))
{
if (is_numeric($_POST['id']))
{
$id = $_POST['id'];
$fname = mysql_real_escape_string(htmlspecialchars($_POST['fname']));
$lname = mysql_real_escape_string(htmlspecialchars($_POST['lname']));
if ($fname == '' || $lname == '')
{
$error = 'ERROR: Please fill in all required fields!';
renderForm($id, $fname, $lname);
}
else
{
mysql_query("UPDATE entries SET fname='$fname', lname='$lname'")
or die(mysql_error());
header("Location: index.html");
}
}
else
{
echo 'Error!';
}
}
else
{
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM entries WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
{
$fname = $row['fname'];
$lname = $row['lname'];
renderForm($id, $fname, $lname, '');
}
else
{
echo "No results!";
}
}
else
{
echo 'Error!';
}
}
?>
Mistake
you have
mysql_query("UPDATE entries SET fname='$fname', lname='$lname'")What it means is you want to set new first and last name for all entries….
Learning
Whenever we update we provide condition for which id you want to change…
so your query should be
mysql_query("UPDATE entries SET fname='$fname', lname='$lname' WHERE id=$id")For
UPDATEit should be alwaysGood Luck