Updating a record from a dynamically generated drop-down.
Posted: Thu Jan 20, 2005 10:39 am
I can confidently add a new record but now I want to update an existing record. I'm using the following code but it's not working. As you can see in the code I'm using a fancy-schmancy dynamically generated form list feature and I don't think there's any conflicts from that code - but I could be wrong. If you see what's wrong - any help is greatly appreciated. Thanks in advance.
In the meantime I'll work on deleting records. That should be easy.
In the meantime I'll work on deleting records. That should be easy.
Code: Select all
<?php
//open database
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'lamps';
mysql_select_db($dbname);
$dbname = mysql_select_db($dbname) or die('Error connecting to database');
?>
<?php
if(isset($_POSTї'update']))
{
$family_name = $_POSTї'family_name'];
$id = $_POSTї'id'];
$nameadd = $_POSTї'nameadd'];
$query = "UPDATE family2 SET family_name = $nameadd" . "WHERE id = $id";
mysql_query($query) or die('Error, update failed');
}
else
{
?>
<form name="update" method="post" action="fam_add_del2.php">
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#666666">
<tr align="center" valign="top" bgcolor="#CCCCCC">
<td><span class="style34">Pick a Lamp Family Name </span></td>
<td><span class="style34">Enter Modified Name Here </span></td>
<td><span class="style34">Click this button to submit change. </span></td>
</tr>
<tr align="center" valign="top" bgcolor="#FFFFFF">
<td width="33%"><select name="fieldname">
<?php
// php to select drop down menu options from tablename table
$result = @mysql_query("SELECT family_name, id FROM family2 ORDER BY family_name asc");
if (!$result)
{
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
while ( $row = mysql_fetch_array($result) )
{
$searchfieldname=$rowї"family_name"];
$searchfieldid=$rowї"id"];
echo '<option value="' . $searchfieldid . '">' . $searchfieldname . '</option>';
}
?>
</select></td>
<td width="33%"><input name="nameadd" type="text" id="nameadd"></td>
<td width="33%"><input name="update" type="submit" id="update" value="Update"></td>
</tr>
</table>
</form><?php } ?>