使用多个提交按钮删除和修改

时间:2011-03-29 23:34:28

标签: php

我有一个问题,我需要能够使用复选框来删除和修改mysql数据库中的数据。

能够使用多个提交按钮根据用户在文本框中键入的内容插入数据,根据所选复选框进行删除以及根据所选复选框进行修改的最有效方法是什么? / p>

这是我到目前为止的代码:

 <?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);
#############################################################################################
?>
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<table width=50%>
    <form method="post">
        <table width border='0'>
            <tr><td> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date:<input type="text" name="date"/></td>
            <td>Ticket #:<input type="text" name="ticket"/></td></tr>
            <table>
                <tr><td>Description:<TEXTAREA COLS=50 name="description"></TEXTAREA></td></tr>
                <tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result     :<TEXTAREA COLS=50 name="result"></TEXTAREA></td></tr>
            </table>
            <tr><td><input type="submit" name="create" value="Add"/></td></tr>
            <tr><td><input type="submit" name="delete" value="Delete"/></td></tr>
            <tr><td><input type="submit" name="modify" value="Modify"/></td></tr>
        </table>    
</table>

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

</BODY>
</HTML>

Insert.php
<?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' ); 
?>



Delete.php

<?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' ); 

?>

任何帮助表示赞赏!

谢谢!

5 个答案:

答案 0 :(得分:2)

另一种方法是提交按钮具有相同的名称,

所以:

<input type="submit" name="submit" value="Delete" />
<input type="submit" name="submit" value="Edit" />

PHP:

switch(strtolower($_POST['submit'])){
    case "delete":
    // delete logic
    break;

    case "edit":
    // edit logic
    break;
}

答案 1 :(得分:1)

我会将插入和删除代码放在主文件的顶部而不是将它们放入文件中。最简单的方法是将表单提交给自己,并根据单击的提交按钮运行代码块

if($_POST['create']){
  // insert code
}
elseif($_POST['delete']){
  // delete code
}

继续使用if / else / elseif的逻辑来处理所有情况。这让我觉得这是完成你想做的最简单的方法。

编辑: 不确定,但似乎你正在处理HTML代码后的$ _POST ['create']等。在html渲染之前,甚至在查询之前,你应该总是进行那种处理,以获得你想要显示的记录,这样你的获取查询将始终带来最新的结果。

答案 2 :(得分:0)

使用多个提交按钮时,可以使用PHP确定按下了哪个按钮。基于此,您可以让应用程序对数据执行不同的操作。

目前还不清楚你想用多个按钮完成什么。也许你可以提供更多细节。

[编辑]

详细查看您的代码,很明显它很快就会成为维护的噩梦。即使您的应用程序很小并且您不想使用MVC模式进行编码,我仍然建议您使用类并将表示与应用程序逻辑数据分开访问。然后,维护应用程序(修复错误)并进行更改会更容易。

如果您构建的不仅仅是一个简单的脚本,我建议使用一个优秀的PHP框架:

答案 3 :(得分:0)

<?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);
#############################################################################################

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');
}


?>
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<table width=50%>
    <form method="post">
        <table width border='0'>
            <tr><td> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date:<input type="text" name="date"/></td>
            <td>Ticket #:<input type="text" name="ticket"/></td></tr>
            <table>
                <tr><td>Description:<TEXTAREA COLS=50 name="description"></TEXTAREA></td></tr>
                <tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result     :<TEXTAREA COLS=50 name="result"></TEXTAREA></td></tr>
            </table>
            <tr><td><input type="submit" name="create" value="Add"/></td></tr>
            <tr><td><input type="submit" name="delete" value="Delete"/></td></tr>
            <tr><td><input type="submit" name="modify" value="Modify"/></td></tr>
        </table>    
</table>

我稍微修改了一下代码,这样就行了;但是,它只适用于第二次点击。

当我选择某个内容并单击“删除”时,它将不会删除,但是当我再次执行时它将

这是为什么?想法?

答案 4 :(得分:0)

将代码删除并插入到select之上,以便在显示数据之前完成所需的更新删除。