Form Data / Database problem

Hello, I’m working on a school project… it’s coming along, but I’ve hit a snag that I can’t get over.
http://140.142.222.8/~garbed/final/editpet.php
What’s got me is that no matter which Edit button the user hits, the only data that is ever returned is whatever data is LAST.

For example, a user inputs Lucy, their cat, Horace, their goldfish, and Gilbert, their horse.
I do a database call to retrieve all pets ‘belonging’ to the user, the pets are listed in rows; at the end of each row is an EDIT RECORDS button. No matter what EDIT REDORDS button they hit (which should correspond to a separate pet) INVARIABLY the only pet record that comes up is THE LAST ONE.

I actually clicked on the button to edit Zuzu, and this is what is returned:

the data for bumbles the alpaca is what shows up.

[code]$query = “SELECT pet_id, pet_name, animal, years_owned, age, afford_sat, health_sat, clean_sat, enjoy_sat, care_sat, repeat_sat FROM PETS WHERE owner_id = {$_SESSION[‘session_user_id’]}”;
$result = @mysql_query($query); // Run the query.
if ($result) { // If it ran OK, display the records.
// This creates a form to show the db record and handle the Edit Pet Record submit button.
// Note that only those animals that this particular user owns should show up
// Users should see, edit, add, or delete pets only to their own account.

?>

<?php
//this first part is the heading, setting up the columns for the data to align
echo ’



’;
// Fetch and print all the records that belong to the user
        while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
            echo "<tr><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td>$row[5]</td><td>$row[6]</td><td>$row[7]</td><td>$row[8]</td><td>$row[9]</td><td>$row[10]</td><td><input input name="$row[0]" type="submit" value="Edit This Record" /><input type='hidden' name='selectedit' value="$row[0]" /></td></tr>n";[/code]

This is the part of the code that handles that first form data:

[code]else if (isset($_POST[‘selectedit’]))
{
echo “{$_POST[‘selectedit’]}”;
echo “

Pet Animal Years Highest Affordability Health Cleanliness Enjoyment Ease of Care Own This Edit
Type Owned Age Satisfaction Satisfaction Satisfaction Satisfaction Satisfaction Type Again? This Pet?
”;
$query2 = “SELECT pet_name, animal, years_owned, age, afford_sat, health_sat, clean_sat, enjoy_sat, care_sat, repeat_sat FROM PETS WHERE pet_id = {$_POST[‘selectedit’]}”;
	$result2 = @mysql_query($query2);	// Run the query.
		if ($result2) { // If it ran OK, display the records.
	while ($row = mysql_fetch_array($result2, MYSQL_BOTH)) {

	echo 	"<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td>$row[5]</td><td>$row[6]</td><td>$row[7]</td><td>$row[8]</td><td>$row[9]</td></tr>";
		}
	echo '</table>';[/code]

I have a feeling that I’m doing something wrong with the form data in this line:

<input input name="$row[0]" type="submit" value="Edit This Record" /><input type='hidden' name='selectedit' value="$row[0]" /></td></tr>n";
Pet Animal Years Highest Affordability Health Cleanliness Enjoyment Ease of Care Own This
Name Type Owned Age Satisfaction Satisfaction Satisfaction Satisfaction Satisfaction Type Again?

You’re partly right, the indicated line is the culprit.

What you’re doing now is the following:

<form>
<editbutton'> <input type='hidden' name='edit' value='1'>
<editbutton'> <input type='hidden' name='edit' value='2'>
<editbutton'> <input type='hidden' name='edit' value='3'>
</form>

When submitting this form, the $_POST[‘edit’] will ONLY pass 1 value, which is the last value it was set to in the HTML, which is, in the above example, 3. Always. You can fix this by passing the ID in the button:

<form>
<input type='submit' name='edit' value='Edit zuzu'>
<input type='submit' name='edit' value='Edit Peter'>
<input type='submit' name='edit' value='Edit alpaca'>
</form>

And then detecting which button was pushed within the form:

if (isset($_POST['edit'])) {
  $edit_pet = trim($_POST['edit']);

  $sql = 'SELECT pet_id, animal, age, etc FROM pets WHERE pet_name = "'.mysql_real_escape_string($edit_pet).'"';
}

Hi again, Zyppora, thanks for your reply and for your attention.

I believe I failed to pass on a piece of data: the rows in the first part (listing the stored data) are being pulled from a multi-user database. A user inputs records, and when they log in to the site, only the pets that belong to them (via user_id in a session) will show up.

Zuzu, peter, and bumbles names and info show up because their rows are being pulled from the database.

[code]$query = “SELECT pet_id, pet_name, animal, years_owned, age, afford_sat, health_sat, clean_sat, enjoy_sat, care_sat, repeat_sat FROM PETS WHERE owner_id = {$_SESSION[‘session_user_id’]}”;
$result = @mysql_query($query); // Run the query.
if ($result) { // If it ran OK, display the records.
// This creates a form to show the db record and handle the Edit Pet Record submit button.
// Note that only those animals that this particular user owns should show up
// Users should see, edit, add, or delete pets only to their own account.

?>

<?php
//this first part is the heading, setting up the columns for the data to align
echo ’



’;
// Fetch and print all the records.
        //
        while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
            echo "<tr><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td>$row[5]</td><td>$row[6]</td><td>$row[7]</td><td>$row[8]</td><td>$row[9]</td><td>$row[10]</td><td><input name="$row[0]" type="submit" value="Edit This Record" /><input type='hidden' name='selectedit' value="$row[0]" /></td></tr>n";
            // Again, keep the row[x] in agreement with the table headings AND the order of the MySQL query
        }

        echo '</table>';
        echo '</form>';[/code]

I don’t know how else to assign a handle to each individual pet besides “$row[0]”.

I don’t know if this code alteration really cleared the water, if I indeed coded correctly based on your suggestion.

else if (isset($_POST['selectedit'])) { echo "{$_POST['selectedit']}"; $edit_pet = trim($_POST['selectedit']);

For complete disclosure, here’s the entire page’s code: Note that the actual UPDATING of edits to a particular pet record ‘row’ is the first part of the page… I haven’t put much serious thought into the code there, I’m trying to get past this proper form hand-off.

[code]<?php # Pet Satisfaction Survey ITA343 Final Project Edit Pet Record
session_start(); //start the session
// Set the page title and include the HTML header and any db scripts.

$page_title = ‘Pet Satisfaction Survey | Edit a Pet Record’;

require_once (’./includes/db_connect.php’); // Connect to the database.
include_once (’./includes/header.html’);
?>

Pet Information | Edit A Pet Record

<?php //======================= Update the Database with corrected values ================================ // if - UPDATE database // else if - display pet record line and form data to edit/revise // else - show all pets with EDIT button at end of row for each if (isset($_POST['submitedit'])) { // Handle the form. // Check for a pet name. if (eregi('^[[:alpha:].' -]{2,15}$', stripslashes(trim($_POST['pet_name'])))) { $petname = escape_data($_POST['pet_name']); } else { $petname = false; echo '

Please enter your pets name!

'; } // Check for pets years owned if (is_numeric($_POST['years_owned'])) { $yo = escape_data($_POST['years_owned']); } else { $yo = false; echo '

Please enter years owned for this pet.

'; } // Check for pet's age if (is_numeric($_POST['age'])) { $age = escape_data($_POST['age']); } else { $age = false; echo '

Please enter pets age less than 99 years!

'; } if ($petname && $yo && $age) { // If everything's OK. $pet_id = $_POST['pet_id']; $animal = $_POST['animal']; $health_sat = $_POST['health_sat']; $clean_sat = $_POST['clean_sat']; $afford_sat = $_POST['afford_sat']; $enjoy_sat = $_POST['enjoy_sat']; $care_sat = $_POST['care_sat']; $repeat_sat = $_POST['repeat_sat']; //UPDATE the pet data. $query = "UPDATE PETS SET pet_name ='$petname', years_owned ='$yo', age ='$age', animal ='$animal', health_sat ='$health_sat', clean_sat ='$clean_sat', afford_sat ='$afford_sat', enjoy_sat ='$enjoy_sat', care_sat ='$care_sat', repeat_sat ='$repeat_sat' WHERE owner_id = {$_SESSION['session_user_id']} and pet_id ={$_POST['pet_id']}"; $result = mysql_query($query) or trigger_error("Query: $queryn
MySQL Error: " . mysql_error()); if (mysql_affected_rows() == 1) { // If it ran OK. echo 'Pet record has been Edited.
Click to Edit another Record'; exit(); } else { // If it did not run OK. // If one of the data tests failed. echo '

Please try again.

'; } mysql_close(); // Close the database connection. // End of the main Submit conditional. exit(); } } // ======================= Display Edit Form of selected Pet to Edit ======================== else if (isset($_POST['selectedit'])) { echo "{$_POST['selectedit']}"; $edit_pet = trim($_POST['selectedit']); function animal_pulldown() { // Make the animals array. $animal = array(1 => 'dog', 'cat', 'ferret', 'bird', 'horse', 'pony', 'small mammal', 'pig', 'fish', 'ostrich', 'alpaca', 'bear', 'lion', 'turtle', 'frog', 'snake', 'lizard', 'insect', 'monkey'); // Make the animal pull-down menu. echo ''; foreach ($animal as $value) { echo "$valuen"; } echo ''; } // The top part of this page's code will display the selected pet to edit and its saved ratings. // Below that will be essentially a repeat of the addpet form // The submit form will pass off the data to editpetfinal.php page where the actual // DB UPDATE command will be executed // This is where we display all the pet records in a row for the one pet the user // wants to edit from below in the page // Their current values (satisfaction ratings, names, etc) are all shown // and below that will be essentially a repeat of the addpet form // The submit form will pass off the data to editpetfinal.php page where the actual // DB UPDATE command will be executed // and the pet data has been changed note along with a link to EDIT another pet. // We pull all the data required from the users stored database info for this form here // The numerical order that the query is accomplished should be maintained echo "
Pet Animal Years Highest Affordability Health Cleanliness Enjoyment Ease of Care Own This Edit
Type Owned Age Satisfaction Satisfaction Satisfaction Satisfaction Satisfaction Type Again? This Pet?
"; $query2 = "SELECT pet_name, animal, years_owned, age, afford_sat, health_sat, clean_sat, enjoy_sat, care_sat, repeat_sat FROM PETS WHERE pet_id = 'mysql_real_escape_string($edit_pet)'"; $result2 = @mysql_query($query2); // Run the query. if ($result2) { // If it ran OK, display the records. while ($row = mysql_fetch_array($result2, MYSQL_BOTH)) { echo ""; } echo '
Pet Animal Years Highest Affordability Health Cleanliness Enjoyment Ease of Care Own This
Name Type Owned Age Satisfaction Satisfaction Satisfaction Satisfaction Satisfaction Type Again?
$row[0] $row[1] $row[2] $row[3] $row[4] $row[5] $row[6] $row[7] $row[8] $row[9]
'; // That concludes the display of the stored information. // What follows is a repeat of the addpet form ?>

Pet's Name:

Animal: <?php animal_pulldown(); ?>

Years Owned:

Age (or highest age reached) (you may indicate with decimals, ie 5.5, if you wish):

We will now ask you to RATE your satisfaction of this pet you owned. On a Scale from 1 to 5 (with 1 indicating the lowest satisfaction, and 5 indicating the highest satisfaction) please tell us your satisfaction for each topic:

Your Satisfaction with this particular pet's Overall Health: 1 2 3 4 5



Your Satisfaction with this particular pet's Overall Cleanliness: 1 2 3 4 5

Your Satisfaction with this particular pet's Overall Affordability: 1 2 3 4 5

Your rating of Enjoyment you had with this particular pet: 1 2 3 4 5

Your Satisfaction with this particular pet's Overall Ease of Care: 1 2 3 4 5

All things considered, would you own this same type of pet again?: YES No

<div align="center"><input type="submit" name="submitedit" value="Edit Pet's Record" /></div>
<input type="hidden" name="submitedit" value="TRUE" />
<?php exit(); } else { // If it did not run OK. echo '

The pet data could not be displayed due to a system error. We apologize for any inconvenience.

' . mysql_error() . ''; } unset($_POST['selectedit']); } // =============== Select Pets To Edit ===================================================== else { // ELSE STATEMENT FOR SUBMIT FORM // This is the beginning part // This is where we display all the user's pets' records in a row // Their current values (satisfaction ratings, names, etc) are all shown // At the end of the row we have one Edit button as a submit button // When submitted, the single row will show up, again with the values in a row // and below that will be essentially a repeat of the addpet form // We pull all the data required from the users stored database info for this form here // The numerical order that the query is accomplished should be maintained $query = "SELECT pet_id, pet_name, animal, years_owned, age, afford_sat, health_sat, clean_sat, enjoy_sat, care_sat, repeat_sat FROM PETS WHERE owner_id = {$_SESSION['session_user_id']}"; $result = @mysql_query($query); // Run the query. if ($result) { // If it ran OK, display the records. // This creates a form to show the db record and handle the Edit Pet Record submit button. // Note that only those animals that this particular user owns should show up // Users should see, edit, add, or delete pets only to their own account. ?>
	<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<?php
        //this first part is the heading, setting up the columns for the data to align
        echo '<table align="center" cellspacing="2" cellpadding="2">
	<tr><td><b>Pet</td><td>Animal</td><td>Years</td><td>Highest</td><td>Affordability</td><td>Health</td><td>Cleanliness</td><td>Enjoyment</td><td>Ease of Care</td><td>Own This</td><td><b>Edit</b></td></tr>
	<tr><td><b><Name></td><td>Type</td><td>Owned</td><td>Age</td><td>Satisfaction</td><td>Satisfaction</td><td>Satisfaction</td><td>Satisfaction</td><td>Satisfaction</td><td>Type Again?</td><td><b>This Pet?</b></td></tr>';
        // Fetch and print all the records.
        
        //
        while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
            echo "<tr><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td>$row[5]</td><td>$row[6]</td><td>$row[7]</td><td>$row[8]</td><td>$row[9]</td><td>$row[10]</td><td><input name="$row[0]" type="submit" value="Edit This Record" /><input type='hidden' name='selectedit' value="$row[0]" /></td></tr>n";
            // Again, keep the row[x] in agreement with the table headings AND the order of the MySQL query
        }

        echo '</table>';
        echo '</form>';


    } else { // If it did not run OK.
        echo '<p>The pet data could not be displayed due to a system error. We apologize for any inconvenience.</p><p>' .
            mysql_error() . '</p>';
    }


}

?>

  </p>
  </td>
<?php include ('./includes/footer.html'); // Include the HTML footer.

?>[/code]

Sponsor our Newsletter | Privacy Policy | Terms of Service