This is a long one...
I am new to PHP and am reading "PHP Essentials" to learn the language. I am working on the script to modify records in the database, but I can't seem to get it to work properly. If someone who knows PHP wouldn't mind looking at what I have below to see if it is good code, I would be so thankful.
The script actually has three parts. admin_modifyrecord1.php creates a dropdown list so that I can select the record I want to edit. Here is that code:
<?php
$connection = mysql_connect("localhost", "xx", "xx") or die ("could not connect to db");
$db = mysql_select_db("xx", $connection) or die ("could not select db");
$sql = "SELECT FirstName, LastName FROM Feedback ORDER BY LastName ASC";
$sql_result = mysql_query($sql,$connection) or die ("could not execute query");
if (!$sql_result) {
echo "could not get list";
}
else {
echo"
<head>
</head>
<body>
<FORM method=\"POST\" action=\"admin_modrecord2.php\">
<table>
<tr>
<td>Name:</td>
<td>
<select name=\"sel_record\">
<option value=\"\">Select a Name</option>
";
while ($row =mysql_fetch_array($sql_result)) {
$FirstName=$row["FirstName"];
$LastName=$row["LastName"];
echo"
<option value\"$FirstName\">$FirstName : $LastName </option>
" ;
}
echo"
</select>
</td>
</tr>
<tr>
<td>
<INPUT type=\"submit\" value=\"Select Name\">
</td>
</tr>
</table>
</form>
</body>
";
}
?>
When I select a name from the list, I am taken to admin_modrecord2.php page as is expected, but the fields are blank...they are supposed to display the info for that record so it may be updated.
Here is the admin_modrecord2.php code:
<?php
$connection = mysql_connect("localhost", "xx", "xx") or die ("could not connect to db");
$db = mysql_select_db("xx", $connection) or die ("could not select db");
$sql = "SELECT * FROM Feedback WHERE FirstName = \"$sel_record\"";
$sql_result = mysql_query($sql,$connection) or die ("could not execute query");
if (!$sql_result) {
echo "could not get record";
}
else {
$row=mysql_fetch_array($sql_result);
$FirstName=$row["FirstName"];
$LastName=$row["LastName"];
echo "
<head>
</head>
<body>
<h1>you have selected to modify the following record:</h1>
<FORM method=\"POST\" action=\"admin_modrecord3.php\">
<table>
<tr>
<td>First Name:</td>
<td><INPUT type=\"text\" name=\"FirstName\" value=\"$FirstName\"></td>
</tr>
<tr>
<td>Last Name:</td>
<td><INPUT type=\"text\" name=\"LastName\" value=\"$LastName\"></td>
</tr>
<tr>
<td>
<INPUT type=\"submit\" value\"Modify Listing\"></td>
</tr>
</table>
</FORM>
</body>
";
}
?>
The odd thing is...if I type in a correct first name and something new for the last name, the record successfully changes. So, the only part that doesn't seem to be working is the extraction of the data when I select a name from the dropdown.
Here is the code for the last part, admin_modrecord3.php:
<?php
$connection = mysql_connect ("localhost", "xx", "xx") or die ("could not connect to db");
$db = mysql_select_db("xx", $connection) or die ("could not select db");
$sql = "UPDATE Feedback SET FirstName = \"$FirstName\", LastName = \"$LastName\" where FirstName=\"$FirstName\"";
$sql_result = mysql_query($sql,$connection) or die ("could not execute query");
if (!$sql_result) {
echo "<p>could not update record";
} else {
echo"
<head>
<body>
<h1>you have made the following changes:</h1>
<table>
<tr>
<td>First Name:</td>
<td>$FirstName</td>
</tr>
<tr>
<td>Last Name:</td>
<td>$LastName</td>
</tr>
</table>
</body>
";
}
?>
IF ANYONE OUT THERE DOESN'T MIND TAKING A LOOK AT THIS, THAT WOULD BE GREAT. I'VE BEEN WORKING ON THIS ALL DAY AND AM ABOUT TO PULL MY HAIR OUT.
THANKS!!