a few notes...
Since you are using a editdog.php page to handle the results.. you want case 1 to be an error.. Noone should be getting to editdog.php unless they have gone through the form to get there. So:
Case 1:
exit ("You have reached this page in error. Please contact the system administrator"); // or something of that nature.
break;
If you don't have an appropriate $_POST['btnSubmit'] value, then they didn't come from the form ;-)
What you've outlined is a 2 phase process (which is very good and simple). Thus, you want the <form> attribute to be on Page 1.. Let me outline with some corrections:
Your Form to select the record for Editing/Deleting would be like so (getdogs.php):
<form action='editdogs.php' method='post' id='form' name='form'>
<?php
mysql_select_db("tttbd", $con);
$result = mysql_query("SELECT * FROM addpup");
echo "<table border='1'>
<tr>
<th> </th>
<th>Name</th>
<th>Breed</th>
<th>Price</th>
<th>Description</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='radio' id='pid' name='pid' value=" . $row['pid'] . "></td>";
echo "<td><input type='text' id='name' name='name' />" . $row['name'] . "</td>"; // this must be a text box if the user is going to edit the record
echo "<td><input type='text' id='breed' name='breed' />" . $row['breed'] . "</td>";
echo "<td><input type='text' id='price' name='price' />" . $row['price'] . "</td>";
echo "<td width=450><input type='textarea' id='description' name='description' />" . $row['description'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' name='btnSubmit' id='btnSubmit' Value='Edit Selected Record' />;"
echo "<input type='submit' name='btnSubmit' id='btnSubmit' Value='Delete Selected Record' />;"
mysql_close($con);
?>
</form>
Then.. your editdogs.php will be like so:
<?php
include("connect.php");
// Set stage level for form
If(!$_POST['btnSubmit']) { $stage=1; }
If($_POST['btnSubmit'] == 'Edit Selected Record') { $stage=2; }
If($_POST['btnSubmit'] == 'Delete Selected Record') { $stage=3; }
Switch ($stage) {
Case 1 :
exit ("There is a problem processing your request. Page aborted !!");
break;
Case 2 :
// your validation and character escaping here......
mysql_query("UPDATE addpup SET name = '$_POST['name']', price = '$_POST['price']', description = '$_POST['description']'
WHERE pid = '$_POST['pid']'");
break;
Case 3 :
mysql_query("DELETE FROM addpup
WHERE pid='$_POST['pid']'");;
break;
}
mysql_close($con);
?>
That is very basic and raw and I don't have a server to check it with here at work, but should get you pointed in the right direction...
You'll definitely want to do some testing before letting it loose. For example, you'll want to mysql_real_escape_string() amongst other things to parse the input or else someone evil could hack your database.
I also tend to create a $strSQL variable and echo to screen for testing.. I also like to look at the $_POST array during testing...
Examples of test codes I use:
$strSQL = "SELECT * FROM addpup";
echo "SQL = '" . $strSQL . "' <br />";
$result = mysql_query($strSQL);
That allows me to see the SQL statement on the screen for any errors (Notice I put single quotes around it).
This will display all the values in the $POST variable. There are other ways of doing it also... but this is what I use:
echo "<pre>";
print_r($_POST);
echo "</pre>";
Different little things I've come across during my year or so of PHP Programming...
Obviously, when you have it working the way you want it you just delete (or remark) them out.
Hope they help and feel free to keep the thread going until you get it working and I'll help as much as I can with my knowledge.