简体   繁体   English

根据不同的提交按钮在同一页面上提交表单

[英]Submit form on same page based on different submit buttons

I have three buttons on a page and I want to submit that form on the same page, depending to the button press, corresponding query will run. 我在页面上有三个按钮,我想在同一页面上提交该表单,具体取决于按下的按钮,将运行相应的查询。 But there are some problem with the script. 但是脚本有一些问题。

<?php
$db = mysql_connect('localhost', 'root', '') or
die ('Unable to connect. Check your connection parameters.');
mysql_select_db('easy_excel', $db) or die(mysql_error($db));

if ($_POST['submit'] =='delete_aabb') 
{
$query ='DELETE FROM aabb';
mysql_query($query, $db) or die(mysql_error($db));
echo 'All Data from aabb Deleted succecfully!';
}
elseif ($_POST['submit'] =='delete_bbcc') {
$query ='DELETE FROM bbcc';
mysql_query($query, $db) or die(mysql_error($db));
echo 'All Data from bbcc Deleted succecfully!';
}
elseif ($_POST['submit'] =='show_bbcc') {
$query = 'SELECT
name
FROM  bbcc';
$result = mysql_query($query, $db) or die(mysql_error($db));

echo '<table border="1">';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
foreach ($row as $value) {
echo '<td>' . $value . '</td>';
}
echo '</tr>';
}
echo '</table>';
}
?>

<html>
<head>
<title>Say My Name</title>
</head>
<body>
<form action="#" method="post">
<table>
<tr>
<input type="submit" name="submit" value="delete_aabb" />
</tr>

<tr>
<input type="submit" name="submit" value="delete_bbcc" /></td>
</tr>

<tr>
<input type="submit" name="submit" value="show_bbcc" /></td>
</tr>

</table>
</form>
</body>
</html>

This script is not running. 该脚本未运行。 Please help me. 请帮我。

I advice you to use MySQLi instead of deprecated MySQL . 我建议您使用MySQLi而不是不推荐使用的MySQL And I think you're referring to the isset() function. 而且我认为您指的是isset()函数。

<html>
<head>
<title>Say My Name</title>
</head>
<body>

<?php

/* ESTABLISH CONNECTION USING MYSQLi */

$con=mysqli_connect("localhost","root","","easy_excel");

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

if (isset($_POST['delete_aabb'])) /* IF DELETE_AABB IS CLICKED */
{
mysqli_query($con,"DELETE FROM aabb");
echo 'All Data from aabb Deleted succecfully!';
}
else if (isset($_POST['delete_bbcc'])) { /* IF DELETE_BBCC IS CLICKED */
mysqli_query($con,"DELETE FROM bbcc");
echo 'All Data from bbcc Deleted succecfully!';
}
else if (isset($_POST['show_bbcc'])) { /* IF SHOW_BBCC IS CLICKED */
$result=mysqli_query($con,"SELECT name FROM bbcc");

echo '<table border="1">';
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
foreach ($row as $value) {
echo '<td>' . $value . '</td>';
}
echo '</tr>';
}
echo '</table>';
}
?>

<form action="" method="POST">
<table>
<tr>
<input type="submit" name="submit" value="delete_aabb" name="delete_aabb"/>
</tr>

<tr>
<input type="submit" name="submit" value="delete_bbcc" name="delete_bbcc"/></td>
</tr>

<tr>
<input type="submit" name="submit" value="show_bbcc" name="show_bbcc"/></td>
</tr>

</table>
</form>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM