Two Submit buttons in a loop problem

Get help on web editors (Frontpage, Dreamweaver) and web languages (HTML, ASP, PHP).
Post Reply
Shelv
Registered User
Posts: 3
Joined: 27 Mar 2011, 10:12

Two Submit buttons in a loop problem

Post 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
Shelv
Registered User
Posts: 3
Joined: 27 Mar 2011, 10:12

Re: Two Submit buttons in a loop problem

Post 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

Code: Select all

if (isset($_POST['cid']))
Any suggestions?
DeathStrike
Registered User
Posts: 2663
Joined: 29 Jul 2004, 02:00
Location: hidden deep in the depths of the underworld is my home.
Contact:

Re: Two Submit buttons in a loop problem

Post 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.
}
}
Spoiler: (show)
Image
SIG by HMAN 8)
Member of The Pride Of Darkness
DeathStrike on Twitter
About me
Spoiler: (show)
Asus P5KPL-CM motherboard, 4 GIG RAM, Q6600 @ 2.88GHz (Thanks Anthro), GeForce 8600GT, Samsung 2333 23" + CRT 17" Monitors. 500GB + 1.5TB HDD, Compro TV tuner, 350 WATT PSU
-Prometheus-
Resident Drama Llama
Posts: 967
Joined: 05 Mar 2008, 02:00
Contact:

Re: Two Submit buttons in a loop problem

Post by -Prometheus- »

........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
BBLounge - Broadband and Technology forum
Please like our facebook page
Post Reply