Jump to content


Photo
- - - - -

Multiple PHP actions on one form

php mysql forms

Best Answer BrowserBugs, 07 September 2017 - 10:49 AM

Aha hang on $row['ID'] is not defined in adddel.php. You need to send the id of the row along with the form, add to the form something like ...

<input type="hidden" name="whichRow" value="<?php echo $row['id']; ?>">

then in adddel.php ...

$whichRow = intval($_POST['whichRow']);
Go to the full post


  • Please log in to reply
9 replies to this topic

#1 techkid

techkid

    Forum Newcomer

  • Members
  • Pip
  • 12 posts
  • Gender:Male
  • Location:Sydney, Australia
  • Experience:Beginner
  • Area of Expertise:I'm Learning

Posted 07 September 2017 - 12:24 AM

Hi there,

 

I am writing a website for a friend. The goal is to allow people to write and submit poetry to help with depression and anxiety, provide messages of hope and things like that. I have created most of the website, but one section has got me stumped.

 

What I want to implement is an approval process, where submitted poetry is segregated until reviewed and approved, given the proliferation of online trolls around such things. What I want to achieve is to have a form where a piece is reviewed with radio button selections for "approve" or "delete", and this is where I have stalled.

 

This is what I have created:

 

review.php

<!DOCTYPE html>
<html>
	<head>
	<title>Soul's Fire - Review Submissions</title>
	</head>
	<body background="Images/background.png">
		<center><img src="Images/heading.png"></center>
		<h1 align="center"><font color="#cc9900">Soul's Fire - Review Submissions</font></h1>
		<p align="center">Words go here</p>
		<?php
			error_reporting(E_ALL & ~(E_WARNING));
			$dbconnect=mysqli_connect("127.0.0.1","user","password","poems");
			$result=mysqli_query($dbconnect, "SELECT * FROM submissions WHERE Approved='N' ORDER BY Time_Submit LIMIT 1");
			if ($row=mysqli_fetch_array($result))
			{
			echo "<form action='adddel.php' method='post'>\n";
			echo "<div align='center'>\n";
			echo "<table border=1 width='80%'>\n";
			echo "<caption align='bottom'><p><input type='submit' name='AppDel' value='Submit Approval'></caption>\n";
			echo "<tr><th width='10%'>Author</th><th width='15%'>Title</th><th width='15%'>Time Submitted</th><th width='*'>Entry</th></tr>\n";
			do
				{
				printf("<tr><td align='center'>%s</td><td align='center'><b>%s</b></td><td align='center'>%s</td><td>%s</td></tr>\n",
				$row["Author"], $row["Title"], $row["Time_submit"], $row["Entry"]);
				}
			while ($row=mysqli_fetch_row($result));
			echo "<tr><td colspan='4' align='center'><INPUT TYPE='radio' NAME='ConApprove' VALUE='Approve'>Approve Submission
			<br><INPUT TYPE='radio' NAME='ConApprove' VALUE='Delete'>Delete Submission</td></tr>\n";
			echo "</table></div><br></form>\n";
			echo "<p align='center'><a href='#top'>Back to Top</a></p>";
			}
	   	else
			{
			echo "No entries to review.";
			}
		?>
		<div align="center">
		<A HREF="index.php">Return to the main page</A>
		</div>
		<h1 align="center"><font color="#cc9900">If you would like me to add content down here, let me know</font></h1>
    </body>
</html>

adddel.php

<?php
	$error="";
	$go=TRUE;

	$dbconnect=mysqli_connect("127.0.0.1", "user", "password", "poems");
	if(!$dbconnect)
		{
		die('Unable to connect to database.');
		}

	$AppDel=$_POST["ConApprove"];

	if($AppDel=="Approve")
		{
		$message .="This will approve the submission. Continue?\\n";
		$ApproveSet=("UPDATE submissions SET Approved='Y' WHERE ID=$row['ID']");
		}
	elseif($AppDel=="Delete")
		{
		$message .="This will delete the submission. Continue?\\n";
		$ApproveSet=("DELETE FROM submissions WHERE ID=$row['ID']");
		}
	else
		{
		$error .="Please select before continuing\\n";
		$go=FALSE;
		}

	if($go==TRUE)
		{
		echo ("<script language='JavaScript'>message('$message');</script>");
		mysqli_query($dbconnect, $ApproveSet) or die(mysqli_error());
		echo "<A HREF='review.php'>Return to review page</A>\n";
		}
	else
		{
		echo ("<script language='JavaScript'>alert('$error');</script>");
		echo ("<script language='JavaScript'>javascript:history.go(-1)</script>");
		}
?>

But when I select any option I only receive an error message (see attachment).

 

I am only just recently getting back into website design after about 15 years, so my knowledge is pretty archaic and rudimentary. I have created basic forms for this website, which work with no problem, but this one is probably too ambitious for my limited knowledge.

 

Thank you in advance.

 

Attached Files



#2 BrowserBugs

BrowserBugs

    Unhinged

  • Privileged
  • PipPipPipPipPip
  • 2,117 posts
  • Gender:Male
  • Location:Surrey, UK
  • Experience:Intermediate
  • Area of Expertise:I'm Learning

Posted 07 September 2017 - 10:03 AM

Ahoy! I'm pretty sure the error as stated is in line 16, which by counting shows;

$ApproveSet=("UPDATE submissions SET Approved='Y' WHERE ID=$row['ID']");

I think the problem is the extra brackets, try ...

$ApproveSet = "UPDATE submissions SET Approved = 'Y' WHERE ID = " . $row['ID'];

... then it'll become for example ...

mysqli_query($dbconnect, "UPDATE submissions SET Approved = 'Y' WHERE ID = 44")

etc



#3 techkid

techkid

    Forum Newcomer

  • Members
  • Pip
  • 12 posts
  • Gender:Male
  • Location:Sydney, Australia
  • Experience:Beginner
  • Area of Expertise:I'm Learning

Posted 07 September 2017 - 10:27 AM

Unfortunately not. It still generates the same error, with or without the brackets... :(



#4 BrowserBugs

BrowserBugs

    Unhinged

  • Privileged
  • PipPipPipPipPip
  • 2,117 posts
  • Gender:Male
  • Location:Surrey, UK
  • Experience:Intermediate
  • Area of Expertise:I'm Learning

Posted 07 September 2017 - 10:49 AM   Best Answer

Aha hang on $row['ID'] is not defined in adddel.php. You need to send the id of the row along with the form, add to the form something like ...

<input type="hidden" name="whichRow" value="<?php echo $row['id']; ?>">

then in adddel.php ...

$whichRow = intval($_POST['whichRow']);

Edited by BrowserBugs, 07 September 2017 - 10:49 AM.


#5 techkid

techkid

    Forum Newcomer

  • Members
  • Pip
  • 12 posts
  • Gender:Male
  • Location:Sydney, Australia
  • Experience:Beginner
  • Area of Expertise:I'm Learning

Posted 07 September 2017 - 11:47 AM

This is the revised code:

 

review.php

<!DOCTYPE html>
<html>
	<head>
	<title>Soul's Fire - Review Submissions</title>
	</head>
	<body background="Images/background.png">
		<center><img src="Images/heading.png"></center>
		<h1 align="center"><font color="#cc9900">Soul's Fire - Review Submissions</font></h1>
		<p align="center">Words go here</p>
		<?php
			error_reporting(E_ALL & ~(E_WARNING));
			$dbconnect=mysqli_connect("127.0.0.1","user","password","poems");
			$result=mysqli_query($dbconnect, "SELECT * FROM submissions WHERE Approved='N' ORDER BY Time_Submit LIMIT 1");
			if ($row=mysqli_fetch_array($result))
			{
			echo "<form action='adddel.php' method='post'>\n";
			echo "<div align='center'>\n";
			echo "<table border=1 width='80%'>\n";
			echo "<caption align='bottom'><p><input type='submit' name='AppDel' value='Submit Approval'></caption>\n";
			echo "<tr><th width='10%'>Author</th><th width='15%'>Title</th><th width='15%'>Time Submitted</th><th width='*'>Entry</th></tr>\n";
			do
				{
				printf("<tr><td align='center'>%s</td><td align='center'><b>%s</b></td><td align='center'>%s</td><td>%s</td></tr>\n",
				$row["Author"], $row["Title"], $row["Time_submit"], $row["Entry"]);
				}
			while ($row=mysqli_fetch_row($result));
			echo "<tr><td colspan='4' align='center'><INPUT TYPE='radio' NAME='ConApprove' VALUE='Approve'>Approve Submission
			<br><INPUT TYPE='radio' NAME='ConApprove' VALUE='Delete'>Delete Submission</td></tr>\n";
(*)			echo '<input type="hidden" name="whichRow" value="<?php echo $row["ID"]; ?>"';
			echo "</table></div></form>\n";
			echo "<p align='center'><a href='#top'>Back to Top</a></p>";
			}
	   	else
			{
			echo "<p align='center'>No entries to review.</p>";
			}
		?>
		<div align="center">
		<A HREF="index.php">Return to the main page</A>
		</div>
		<h1 align="center"><font color="#cc9900">If you would like me to add content down here, let me know</font></h1>
    </body>
</html>

adddel.php

<?php
	$error="";
	$go=TRUE;

	$dbconnect=mysqli_connect("127.0.0.1", "user", "password", "poems");
	if(!$dbconnect)
		{
		die('Unable to connect to database.');
		}

	$AppDel=$_POST["ConApprove"];
(*)	$whichRow = intval($_POST['whichRow']);

	if($AppDel=="Approve")
		{
		$message .="This will approve the submission. Continue?\\n";
		$ApproveSet="UPDATE submissions SET Approved='Y' WHERE ID=$row['ID']";
		}
	elseif($AppDel=="Delete")
		{
		$message .="This will delete the submission. Continue?\\n";
		$ApproveSet="DELETE FROM submissions WHERE ID=$row['ID']";
		}
	else
		{
		$error .="Please select before continuing\\n";
		$go=FALSE;
		}

	if($go==TRUE)
		{
		echo ("<script language='JavaScript'>message('$message');</script>");
		mysqli_query($dbconnect, $ApproveSet) or die(mysqli_error());
		echo "<A HREF='review.php'>Return to review page</A>\n";
		}
	else
		{
		echo ("<script language='JavaScript'>alert('$error');</script>");
		echo ("<script language='JavaScript'>javascript:history.go(-1)</script>");
		}
?>

Still I encounter the same problem. I am reasonably sure I have the syntax (or possibly placement) wrong for the highlighted lines (I added an asterisk (*) at the start of the line to highlight my placement only).


Edited by techkid, 07 September 2017 - 11:48 AM.


#6 BrowserBugs

BrowserBugs

    Unhinged

  • Privileged
  • PipPipPipPipPip
  • 2,117 posts
  • Gender:Male
  • Location:Surrey, UK
  • Experience:Intermediate
  • Area of Expertise:I'm Learning

Posted 07 September 2017 - 11:54 AM

You still have the following ...

$ApproveSet="UPDATE submissions SET Approved='Y' WHERE ID=$row['ID']";

This should now use the posted row id which is $whichRow.



#7 techkid

techkid

    Forum Newcomer

  • Members
  • Pip
  • 12 posts
  • Gender:Male
  • Location:Sydney, Australia
  • Experience:Beginner
  • Area of Expertise:I'm Learning

Posted 07 September 2017 - 12:53 PM

I seem to be going from bad to worse. I realised a mistake in my entry on review.php, but my attempts to correct it causes it to fail.
 
Here is my revised code (review.php):
 

<!DOCTYPE html>
<html>
	<head>
	<title>Soul's Fire - Review Submissions</title>
	</head>
	<body background="Images/background.png">
		<center><img src="Images/heading.png"></center>
		<h1 align="center"><font color="#cc9900">Soul's Fire - Review Submissions</font></h1>
		<p align="center">Words go here</p>
		<?php
			error_reporting(E_ALL & ~(E_WARNING));
			$dbconnect=mysqli_connect("127.0.0.1","David","D15cw0rld","poems");
			$result=mysqli_query($dbconnect, "SELECT * FROM submissions WHERE Approved='N' ORDER BY Time_Submit LIMIT 1");
			if ($row=mysqli_fetch_array($result))
			{
			echo "<form action='adddel.php' method='post'>\n";
			echo "<div align='center'>\n";
			echo "<table border=1 width='80%'>\n";
			echo "<caption align='bottom'><p><input type='submit' name='AppDel' value='Submit Approval'></caption>\n";
			echo "<tr><th width='10%'>Author</th><th width='15%'>Title</th><th width='15%'>Time Submitted</th><th width='*'>Entry</th></tr>\n";
			do
				{
				printf("<tr><td align='center'>%s</td><td align='center'><b>%s</b></td><td align='center'>%s</td><td>%s</td></tr>\n",
				$row["Author"], $row["Title"], $row["Time_submit"], $row["Entry"]);
				}
			while ($row=mysqli_fetch_row($result));
			echo "<tr><td colspan='4' align='center'><INPUT TYPE='radio' NAME='ConApprove' VALUE='Approve'>Approve Submission
			<br><INPUT TYPE='radio' NAME='ConApprove' VALUE='Delete'>Delete Submission
(*)			<br><INPUT TYPE='hidden' NAME='whichRow' VALUE='<?php echo $row["ID"]; ?>'></td></tr>\n";
			echo "</table></div></form>\n";
			echo "<p align='center'><a href='#top'>Back to Top</a></p>";
			}
	   	else
			{
			echo "<p align='center'>No entries to review.</p>";
			}
		?>
		<div align="center">
		<A HREF="index.php">Return to the main page</A>
		</div>
		<h1 align="center"><font color="#cc9900">If you would like me to add content down here, let me know</font></h1>
    </body>
</html>

Which generates the attached error at the marked location.

Attached Files



#8 techkid

techkid

    Forum Newcomer

  • Members
  • Pip
  • 12 posts
  • Gender:Male
  • Location:Sydney, Australia
  • Experience:Beginner
  • Area of Expertise:I'm Learning

Posted 07 September 2017 - 12:58 PM

I'm sure adding my form tags inside the PHP section of the script is a massive no-no, given how things are going for me here. And that, because of that, the added line you supplied is failing because of my poor use of quotation marks...



#9 BrowserBugs

BrowserBugs

    Unhinged

  • Privileged
  • PipPipPipPipPip
  • 2,117 posts
  • Gender:Male
  • Location:Surrey, UK
  • Experience:Intermediate
  • Area of Expertise:I'm Learning

Posted 07 September 2017 - 01:28 PM

Ok I think I need to rewind slightly. If you have one form with multiple entries and each entry has 2 options to approve and delete then the following should output your array. I've split out the html and php for easy reading and I'm using a simple list for now. The exception is instead of radio i'm using 2 buttons for each, one for delete and one for approve. The buttons value is set to the id of the record.

<form action="adddel.php" method="post">
<?php while($row = mysqli_fetch_row($result)) { ?>
<ul>
<li><?php echo $row['Title']; ?></li>
<li><?php echo $row['Author']; ?></li>
<li><?php echo $row['Entry']; ?></li>
<li><?php echo $row['ID']; ?></li>
<li><button name="del" type="submit" value="<?php echo $row['ID']; ?>">Delete</button></li>
<li><button name="app" type="submit" value="<?php echo $row['ID']; ?>">Approve</button></li>
</ul>
<?php } ?>
</form>

Now if you collect the $_POST value of which submit button was used you can determine which record and what to do with it. Below checks if a button name was pressed and the value is which id you want to do something to (assuming the ID is a number).

<?php if($_SERVER['REQUEST_METHOD']=="POST" && intval($_POST['del'])>=1) {
    $del_id = intval($_POST['del']);
} elseif($_SERVER['REQUEST_METHOD']=="POST" && intval($_POST['app'])>=1) {
    $approved_id = intval($_POST['app']);
} else {
    // Neither app was pressed or del was pressed so something is fishy.
} ?>

Edited by BrowserBugs, 07 September 2017 - 01:30 PM.


#10 techkid

techkid

    Forum Newcomer

  • Members
  • Pip
  • 12 posts
  • Gender:Male
  • Location:Sydney, Australia
  • Experience:Beginner
  • Area of Expertise:I'm Learning

Posted 08 September 2017 - 10:15 AM

I actually ended up scrapping the pages and rebuilding from scratch, trying to split the HTML from the PHP, and have made a great deal of progress. While the end result is not exactly what I want, the proof of concept is done!

 

Here is my final code:

 

review.php

<!DOCTYPE html>

<html>
<head>
<title>Soul's Fire - Visitor Contributions</title>
</head>
<body background="Images/background.png">
<center><img src="Images/heading.png"></center>
<h1 align="center"><font color="#cc9900">Soul's Fire - Visitor Contributions</font></h1>
<p align="center">Words go here</p>

<?php
error_reporting(E_ALL & ~(E_WARNING));
$dbconnect=mysqli_connect("127.0.0.1", "David", "D15cw0rld", "poems");
$result=mysqli_query($dbconnect, "SELECT * FROM submissions WHERE Approved='N' ORDER BY Time_submit LIMIT 1");
$row=mysqli_fetch_array($result);
?>

<form action="adddel.php" method="post">
<p align="center">You are reviewing entry: <input type="text" name="HEntry" value="<?php echo $row['ID']; ?>">
<table width="80%" border="1" align="center">
<caption align="bottom"><p><input type="submit" name="AppDel" value="Submit Approval"></caption>
<tr>
<th width="10%">Author</th><th width="15%">Title</th><th width="15%">Time Submitted</th><th width="*">Entry</th>
</tr>

<?php
do
{
printf("<tr><td align='center'>%s</td><td align='center'><b>%s</b></td><td align='center'>%s</td><td>%s</td></tr>\n",
$row["Author"], $row["Title"], $row["Time_submit"], $row["Entry"]);
}
while ($row=mysqli_fetch_row($result));
?>

<tr>
<td colspan='4' align='center'><INPUT TYPE='radio' NAME='ConApprove' VALUE='Approve'>Approve Submission
<br><INPUT TYPE='radio' NAME='ConApprove' VALUE='Delete'>Delete Submission</td></tr>
</table>
</form>
<p align="center"><A HREF="index.php">Return to the home page</A>
</body>
</html>

adddel.php

<?php
$error="";
$go=TRUE;
$message="";

$dbconnect=mysqli_connect("127.0.0.1", "user", "password", "poems");
if(!$dbconnect)
{
die('Unable to connect to database.');
}

$AppDel=$_POST["ConApprove"];
$ID=$_POST["HEntry"]

if($AppDel=="Approve")
{
$message .="This will approve the submission. Continue?\\n";
$ApproveSet="UPDATE submissions SET Approved='Y' WHERE ID=$ID";
}
elseif($AppDel=="Delete")
{
$message .="This will delete the submission. Continue?\\n";
$ApproveSet="DELETE FROM submissions WHERE ID=$ID";
}
else
{
$error .="Please select before continuing\\n";
$go=FALSE;
}

if($go==TRUE)
{
(*)echo ("<script language='JavaScript'>confirm('$message');</script>");
mysqli_query($dbconnect, $ApproveSet) or die(mysqli_error());
echo "<A HREF='review.php'>Return to review page</A>\n";
}
else
{
echo ("<script language='JavaScript'>alert('$error');</script>");
echo ("<script language='JavaScript'>javascript:history.go(-1)</script>");
}
?>

The only niggling detail is that the confirm box, marked with an asterisk (*), proceeds whether you press "OK" or "Cancel", but for right now, I can live with that!

 

Thank you very much for your help with this!







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users