Jump to content

Recommended Posts

This code has options for adding and deleting data from a table. Adding data works, my problem arises when trying to echo out the current data with a delete button for each row. The data echos out fine, but the delete buttons do not work. Help?

 

	if(isset($_POST["add"])){
	$a = mysql_real_escape_string($_POST["a"]);
	$b = mysql_real_escape_string($_POST["b"]);
	$c = mysql_real_escape_string($_POST["c"]);
	$d = mysql_real_escape_string($_POST["d"]);
	$e = mysql_real_escape_string($_POST["e"]);
	if(!$a || !$b || !$c || !$d || !$e) {
		die("Please <a href=\"edit_writing.php\">try again</a> and ensure the form is completed properly.");
	}
	mysql_query("INSERT INTO writing (a, b, c, d, e) VALUES ('$a', '$b', '$c', '$d', '$e')") or die($error_message);
	$success_message = "Question was added.";
}
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
	<head>
		<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
		<title>Title</title>
		<link rel=\"stylesheet\" type=\"text/css\" href=\"css.css\" />
	</head>
	<body>
		<div id=\"timer\">
		</div>
		<div id=\"wrap\">
			<div id=\"mainheader\">
			</div>
			<div id=\"content\">
				<div id=\"orangeheader\"><br/>
					Grammar Test
				</div>
				<div id=\"text\">
					" . $success_message . "
					<table>
						<tr>
							<td><strong>Question One</strong></td>
							<td><strong>Question Two</strong></td>
							<td><strong>Question Three</strong></td>
							<td><strong>Question Four</strong></td>
							<td><strong>Question Five</strong></td>
						</tr>";	
$result = mysql_query("SELECT * FROM writing");
$number = mysql_numrows($result);
$counter = 0;
while ($counter<$number){
	$a = mysql_result($result, $counter,"a");
	$b = mysql_result($result, $counter,"b");
	$c = mysql_result($result, $counter,"c");
	$d = mysql_result($result, $counter,"d");
	$e = mysql_result($result, $counter,"e");
	echo "<tr>
			<td>" . $a . "</td>
			<td>" . $b . "</td>
			<td>" . $c . "</td>
			<td>" . $d . "</td>
			<td>" . $e . "</td>
			<td>
				<form action=\"edit_writing.php\" method=\"post\">
					<input type=\"submit\" name=\"" . $counter . "\" value=\"Delete\"/>
				</form>
			</td>
		</tr>";
	if(isset($_POST["$counter"])){
		$a = mysql_real_escape_string($_POST["a"]);
		mysql_query("DELETE FROM writing WHERE question='$a'") or die($error_message); 
		$success_message = "Question was deleted.";
	}
$counter++;
}
?>
</table>
	<form action="edit_writing.php" method="post">
		<table>
			<tr>
				<td><label for="a">Question One</label></td>
				<td><label for="b">Question Two</label></td>
				<td><label for="c">Question Three</label></td>
				<td><label for="d">Question Four</label></td>
				<td><label for="e">Question Five</label></td>
			</tr>
			<tr>			
				<td><input type="text" name="a" /></td>
				<td><input type="text" name="b" /></td>
				<td><input type="text" name="c" /></td>
				<td><input type="text" name="d" /></td>
				<td><input type="text" name="e" /></td>
			</tr>
		</table>
		<input type="submit" name="add" value="Add Question"/>
	</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/203280-trying-to-get-multiple-delete-buttons/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.