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!