Using Multiple Submit Buttons to Delete and Modify
<p>I have an issue where I need to be able to use check boxes in order to delete and modify data in a mysql database.</p>

<p>What is the most efficient way of being able to use multiple submit buttons to either insert data based on what the user types into the text boxes, delete based on the check boxes selected, and modify based on the check boxes selected.</p>

<p>Here is the code I have so far:</p>

<pre><code> &lt;?php 
$host       = "localhost";
$user       = "root";
$pass       = "";
$dbName     = "ticket_history";
$table_name = "ticket_history";

################ Connect to the Database and SELECT DATA ####################################
$conn = mysql_connect($host, $user, $pass) or die ("Unable to connect");
mysql_select_db($dbName);
$query = "SELECT Auto,Date,Ticket_Number,Description,Result FROM $table_name";
$result = mysql_query($query);
$count=mysql_num_rows($result);
#############################################################################################
?&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;table width=50%&gt;
    &lt;form method="post"&gt;
        &lt;table width border='0'&gt;
            &lt;tr&gt;&lt;td&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Date:&lt;input type="text" name="date"/&gt;&lt;/td&gt;
            &lt;td&gt;Ticket #:&lt;input type="text" name="ticket"/&gt;&lt;/td&gt;&lt;/tr&gt;
            &lt;table&gt;
                &lt;tr&gt;&lt;td&gt;Description:&lt;TEXTAREA COLS=50 name="description"&gt;&lt;/TEXTAREA&gt;&lt;/td&gt;&lt;/tr&gt;
                &lt;tr&gt;&lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Result     :&lt;TEXTAREA COLS=50 name="result"&gt;&lt;/TEXTAREA&gt;&lt;/td&gt;&lt;/tr&gt;
            &lt;/table&gt;
            &lt;tr&gt;&lt;td&gt;&lt;input type="submit" name="create" value="Add"/&gt;&lt;/td&gt;&lt;/tr&gt;
            &lt;tr&gt;&lt;td&gt;&lt;input type="submit" name="delete" value="Delete"/&gt;&lt;/td&gt;&lt;/tr&gt;
            &lt;tr&gt;&lt;td&gt;&lt;input type="submit" name="modify" value="Modify"/&gt;&lt;/td&gt;&lt;/tr&gt;
        &lt;/table&gt;    
&lt;/table&gt;

&lt;?php
print "&lt;table width=80% border=1&gt;\n"; 
$cols = 0; 
while ($get_info = mysql_fetch_assoc($result)){ 
$id = $get_info['Auto'];
if($cols == 0) 
{ 
  $cols = 1; 
  print "&lt;tr&gt;";
  print "&lt;th&gt;Select&lt;/th&gt;";  
  foreach($get_info as $col =&gt; $value) 
  { 
    print "&lt;th&gt;$col&lt;/th&gt;"; 
  } 
  print "&lt;tr&gt;\n"; 
} 
print "&lt;tr&gt;\n"; 
print "&lt;td&gt;&lt;input type='checkbox' name='selected[]' id='checkbox[]' value=$id&gt;&lt;/td&gt;";
foreach ($get_info as $field) 
print "\t&lt;td align='center'&gt;&lt;font face=arial size=1/&gt;$field&lt;/font&gt;&lt;/td&gt;\n"; 
print "&lt;/tr&gt;\n"; 
} 
print "&lt;/table&gt;\n"; 

if (isset($_POST['create'])) {
    $query_insert = "INSERT INTO ticket_history (Date, Ticket_Number, Description, Result) 
                 VALUES ('$_POST[date]', '$_POST[ticket]', '$_POST[description]', '$_POST[result]')";
$result_insert = mysql_query($query_insert);
if ($result_insert) {
echo "win";
}
else {
echo "fail";
}

}
elseif (isset($_POST['delete'])) {
    $ids = array();
foreach($_POST['selected'] as $selected) {
    if (ctype_digit($selected)) {
        $ids[] = $selected;
    }
    else {
        die('invalid input');
    }
$sql_delete = sprintf('DELETE FROM ticket_history WHERE Auto IN (%s)', 
    implode(',', $ids));

$result_delete = mysql_query($sql_delete);
}
if ($result_delete) {
echo $result_delete;
}
else {
echo "fail";
}
}
elseif (isset($_POST['modify'])) {
    header('Location: modify_ticket.php');
}



mysql_close($conn);
?&gt;
&lt;/form&gt;

&lt;/BODY&gt;
&lt;/HTML&gt;

Insert.php
&lt;?php
$host       = "localhost";
$user       = "root";
$pass       = "";
$dbName     = "ticket_history";
$table_name = "ticket_history";

$conn = mysql_connect($host, $user, $pass) or die ("Unable to connect");
mysql_select_db($dbName);


$query_insert = "INSERT INTO ticket_history (Date, Ticket_Number, Description, Result) 
                 VALUES ('$_POST[date]', '$_POST[ticket]', '$_POST[description]', '$_POST[result]')";
$result_insert = mysql_query($query_insert);
if ($result_insert) {
echo "win";
}
else {
echo "fail";
}
 #header( 'Location: ticket_history.php' ); 
?&gt;



Delete.php

&lt;?php
$host       = "localhost";
$user       = "root";
$pass       = "";
$dbName     = "ticket_history";
$table_name = "ticket_history";

################ Connect to the Database and SELECT DATA ####################################
$conn = mysql_connect($host, $user, $pass) or die ("Unable to connect");
mysql_select_db($dbName);
$query = "SELECT Date,Ticket_Number,Description,Result FROM $table_name";
$result = mysql_query($query);
$count=mysql_num_rows($result);
#####################################

$ids = array();
foreach($_POST['selected'] as $selected) {
    if (ctype_digit($selected)) {
        $ids[] = $selected;
    }
    else {
        die('invalid input');
    }
$sql = sprintf('DELETE FROM ticket_history WHERE Auto IN (%s)', 
    implode(',', $ids));

$result = mysql_query($sql);
}

 header( 'Location: ticket_history.php' ); 

?&gt;
</code></pre>

<p>Any help is appreciated! </p>

<p>Thank you!</p>
