I am trying to make an administrative page the handles the information that is put into a property flyer for a real estate website.

A part of the flyer is a list of property details. So on the admin page it prints a table that list all of the details and gives you the option to delete each detail listed, and the option to add a detail if there are less than 5 of them.
It all works fine except when you go to delete a detail it just deletes the last detail listed not whatever detail you actually wanted to delete. My guess is that this is something that can be fixed in my the script that handles the form. I just don't know how to fix it.
Here is my from handle script:


/* ________FORM HANDLERS_________ */

if (isset($_POST['delete'])) {
	if (empty($_POST['detail_id'])) {
	$errors[] = 'Could not id selling point.......gr!';
	} else {
	$d_id = $_POST['detail_id'];
	}
	if (empty($errors)) {
	$dq = "DELETE FROM detail WHERE detail_id='$d_id' LIMIT 1";
	$dr = @mysqli_query ($dbc, $dq);

	if (mysqli_affected_rows($dbc) == 1) {
	echo '<P>The selling point has been deleted</p>';
	}	else {
		echo '<P>The selling point was not deleted due to a system error.</P>';
		echo '<P>'. mysqli_error($dbc).'<br>Query:' .$dq. '</p>';
		} }

} elseif (isset($_POST['details'])) {
// Check for detail 1:
if (empty($_POST['detail1'])) {
$errors[] = 'You forgot to enter a property descriptions. 1';
} else {
$d1 = mysqli_real_escape_string($dbc, trim($_POST['detail1']));
}


	if (empty($errors)) { // If everything's OK.
		$q = "INSERT INTO detail (detail, property_id) VALUES ('$d1', '$id')";
		$r = @mysqli_query ($dbc,$q);
			if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.

				// Print a message:
				echo'
				<p>You added the selling point "'.$d1.'" to the flyer!</p>';

					} else { // If it did not run OK.
					echo '<p calss="error">The selling point could not be added due to a system error. We apologize for any inconvenience.</p>'; // Public Message.
					echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>'; //Debugging message.
					}
	} else {// Report the errors.
	echo '<p class="error">The following error(s) occured:<br />';
	foreach ($errors as $msg) {// Print each error
	echo " - $msg<br />\n";
	}
	echo '</p><p>Please try again.</p>';

}  //End of is  (empty($errors)) IF.

} // End of submit conditional.

/* _________END FORM HANDLERS_________ */

and Here is the script the prints the form:

echo '<h2>Selling points already entered:</h2>';
$q2 = "SELECT * FROM detail WHERE property_id=$id";
$r2 = @mysqli_query ($dbc,$q2);
$num = @mysqli_num_rows($r2);
if($num < 5 && $num != 0) {

echo '<table align="center" cellspacing="3" cellpadding="3" width="60%">
<tr>
<td align="left" width="10%"><b>Delete</b></td>
<td align="left" width="90%"><b>Selling Point</b></td>
</tr>';
$bg = '#EEEEEE'; // Set the intial background color.

while ($row2 = mysqli_fetch_array($r2, MYSQLI_ASSOC)) {

$bg = ($bg=='#EEEEEE' ? '#FFFFFF' : '#EEEEEE'); // Switch the background color.

	echo '<tr bgcolor="' . $bg . '">
	<td align="left">
	<form action="flyer.php?id=' . $id . '" method="post">
	<input type="submit" name="delete" value="Delete" onclick="return confirm(\'Are you SURE you wish to delete this selling point?\')">
	<input type="hidden" name="detail_id" value="'.$row2['detail_id'].'"/>
	</from></td>
	<td align="left">' . $row2['detail'] . '</td>
	</tr>
	';

} // End of WHILE loop
echo '</table>

<P><h2>Create more selling points:</h2>';

		// Create Form to add selling points:
		echo '<form action="flyer.php?id=' . $id . '" method="post">
		<P>Selling Point:<input type="text" name="detail1" size="50" maxlength="100" value=""/></p>
		<input type="submit" name="submit" value="Add Selling Points" /></p>
		<input type="hidden" name="details" value="TRUE"/>
		<input type="hidden" name="id" value="' . $id . '"/></form>';

} elseif($num >= 5) {


echo '<table align="center" cellspacing="3" cellpadding="3" width="60%">
<tr>
<td align="left" width="10%"><b>Delete</b></td>
<td align="left" width="90%"><b>Selling Point</b></td>
</tr>';
$bg = '#EEEEEE'; // Set the intial background color.

while ($row2 = mysqli_fetch_array($r2, MYSQLI_ASSOC)) {

$bg = ($bg=='#EEEEEE' ? '#FFFFFF' : '#EEEEEE'); // Switch the background color.

	echo '<tr bgcolor="' . $bg . '">
	<td align="left">
	<form action="flyer.php?id=' . $id . '" method="post">
	<input type="submit" name="delete" value="Delete" onclick="return confirm(\'Are you SURE you wish to delete this selling point?\')">
	<input type="hidden" name="detail_id" value="'.$row2['detail_id'].'"/>
	</from></td>
	<td align="left">' . $row2['detail'] . '</td>
	</tr>
	';

} // End of WHILE loop
echo '</table>';

echo '<p class="error">The max number of selling points have been added to add more you must delete a selling point.</p>';

} else {

echo 'No selling points have been added';

		// Create Form to add selling points:
		echo '<form action="flyer.php?id=' . $id . '" method="post">
		<P>Selling Point:<input type="text" name="detail1" size="50" maxlength="100" value=""/></p>
		<input type="submit" name="submit" value="Add Selling Points" /></p>
		<input type="hidden" name="details" value="TRUE"/>
		<input type="hidden" name="id" value="' . $id . '"/></form>';}

Any advice/suggestion welcomed.
Thanks!

    Yeah, if you look at your source code form the browser BEFORE you submit delete, you'll notice that you have several inputs like:

    <input type="hidden" name="detail_id" value="1"/>
    <!--....-->
    <input type="hidden" name="detail_id" value="2"/>

    The issue is that PHP keeps resetting the key in $_POST['detail_id'] to the value of the last post input named 'detail_id' in the post data submitted. This is expected behaviour. In order to get each individual input value, you need to let php know that your post key is an array by added brackets after the input name in your html:

    echo '<tr bgcolor="' . $bg . '">
            <td align="left">
            <form action="flyer.php?id=' . $id . '" method="post">
            <input type="submit" name="delete" value="Delete" onclick="return confirm(\'Are you SURE you wish to delete this selling point?\')">
            <input type="hidden" name="detail_id[]" value="'.$row2['detail_id'].'"/>
            </from></td>
            <td align="left">' . $row2['detail'] . '</td>
            </tr>
            '; 

    Now, $_POST['detail_id'] will be an array of values. The only thing you need to change (at a glance) is:

     $dq = "DELETE FROM detail WHERE detail_id='";
        $dq .= implode("' OR detail_id='", $_POST['detail_id']);
        $dq .= "' LIMIT 1";

      By the way, I just saw that in each iteration of your while loop, you have a separate form. This means my explanation is not quite right. The reason it deletes the last one is because your closing tag is misspelled. </from> should be </form>.

        bretticus;10879467 wrote:

        By the way, I just saw that in each iteration of your while loop, you have a separate form. This means my explanation is not quite right. The reason it deletes the last one is because your closing tag is misspelled. </from> should be </form>.

        Oh wow.... haha. I seem to be the master of making these kinds of mistakes, but thank you for your other post. It was very informative. I am pretty new to all this PHP and MySQL stuff. So, any advice, even if its not relative, is much appreciated.

          jpoladsky;10879564 wrote:

          Oh wow.... haha. I seem to be the master of making these kinds of mistakes, but thank you for your other post. It was very informative. I am pretty new to all this PHP and MySQL stuff. So, any advice, even if its not relative, is much appreciated.

          No worries, we have all made those kinds of mistakes (much more often than you'd think is statistically possible!)

          You are welcome. You could apply my earlier advice in an instance where you wanted to have a checkbox for each selling point that you checked and deleted all checked selling points with one button click. Of course that would constitute having them all under just one form (which is, in effect, what you HAD while the closing tag was missing And, consequently, what threw me off the first time around.)

          Cheers!

            Write a Reply...