Page 1 of 1
Two Submit buttons in a loop problem
Posted: 27 Mar 2011, 10:14
by Shelv
Hey guys, what I've got here is a loop that creates a form with a table in, it also has a delete button and edit button for every row it displays. I got the delete button working and doing what it should do but the edit button not. I coded the edit button to redirect me to the index page and if I click it I do get redirected but it also deletes the entry. Any suggestions?
Code: Select all
function manage() {
global $connection;
$manage = "SELECT * FROM subject ORDER BY id ASC";
$table = mysql_query($manage, $connection) or die ("Database course query failed: " . mysql_error());
$num = mysql_num_rows($table);
$i = 0;
while ($i < $num) {
$row = mysql_fetch_array($table);
$cid = $row['id'];
$cname = $row['name'];
echo '<form action="man.php" method = "post"><input type="hidden" name="id" value="'.$row['id'].'"/><input type="hidden" name="name" value="'.$row['name'].'"/></td>';
echo '<tr>';
echo"<td width = '40%'>" .$row['id']. " - " . $row['name']."</td>";
echo "<td align = 'center'><input type = 'submit' value = 'Delete'</type>";
echo "<td align = 'center'><input type = 'submit' value = 'Edit'</type></td>";
echo '</tr></form>';
$i++;
}
if (isset($_POST['id'])) {
$qry = "DELETE FROM subject WHERE id = $_POST[id]";
$result = mysql_query($qry, $connection) or die ("Database course delete query failed: " . mysql_error());
$increment = "ALTER TABLE course AUTO_INCREMENT = 1";
$inc_reset = mysql_query($increment,$connection) or die ("Could not reset auto increment: " . mysql_error());
header("location:man.php");
}
if (isset($_POST['name'])){
header("location:index.php");
}
}
Any suggestions would be appreciated
Thanks
Re: Two Submit buttons in a loop problem
Posted: 27 Mar 2011, 16:03
by Shelv
I changed it a bit, added check boxes next to every entry.
Code: Select all
function delete_course() {
global $connection;
$manages = "SELECT * FROM course ORDER BY cid ASC";
$tables = mysql_query($manages, $connection) or die ("Database course query failed: " . mysql_error());
$num = mysql_num_rows($tables);
$i = 0;
while ($i < $num) {
$row = mysql_fetch_array($tables);
$cid = $row['cid'];
$cname = $row['cname'];
echo '<form action="delete_course.php" method = "post"><input type="hidden" name="cid" value="'.$row['cid'].'"/></td>';echo $row['cid'];
echo '<tr>';
echo "<td align = 'left'><input type = 'checkbox' value='check'></type>" .$row['cid']. " - " . $row['cname']."</td>";
echo '</tr></form>';
$i++;
}
//echo "<br />";
if (isset($_POST['del'])) {
if (isset($_POST['cid'])) {
/* $qry = "DELETE FROM course WHERE cid = $_POST[cid]";
$result = mysql_query($qry, $connection) or die ("Database course delete query failed: " . mysql_error());
$increment = "ALTER TABLE course AUTO_INCREMENT = 1";
$inc_reset = mysql_query($increment,$connection) or die ("Could not reset auto increment: " . mysql_error());
header("location:man.php");*/
echo "yo";
} else {
echo "no";}
}
}
There now seems to be a problem here
Any suggestions?
Re: Two Submit buttons in a loop problem
Posted: 27 Mar 2011, 20:46
by DeathStrike
what kind of problem is it?
does it give an error message?
would make it easier to solve the issue.
otherwise i have to go thorough it all and find it.
Wait...
i do notice that for all the checkboxes you have the same name? "cid"
basically with the $_POST['cid'] it won't know which one you mean? as there may be 10 cid?
my idea would be to make it
Code: Select all
echo '<form action="delete_course.php" method = "post"><input type="hidden" name="cid"'.$row['cid'].' value="'.$row['cid'].'"/></td>';echo $row['cid'];
that should give it unique names.
then to call it you would need to use something like this.
Code: Select all
foreach($_POST as $key=>$value){
if(substr($key, 0, 3) == 'cid'){
then here you would need to store the cid as an array or something so you can compare it later in the code i think.
}
}
Re: Two Submit buttons in a loop problem
Posted: 28 Mar 2011, 04:42
by -Prometheus-
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................