我在一页上有两个表格。一种是删除用户帐户,另一种是UPDATE
用户详细信息(准确地说是用户“节食”)。这些表单为其提交按钮使用不同的值
,因为它们是两个单独的表单,用户只能选择一个来填写,而不是同时填写。
我正在尝试让我的 UPDATE 语句的提交按钮起作用,但是当您按下该按钮时没有任何反应。 UPDATE 语句用于更新存储在我的 MySQL 数据库中的“users”表中的用户“dietID”。我是 php 的新手,如果有任何帮助,我将不胜感激!
为登录用户更新饮食的表单处理代码:
<?php
$sess_userID =$_SESSION['userID'];
$dietopt = trim($_POST['dietopt']);
if(trim($_POST['submit']) == "Change") {
if (trim($_POST['dietopt']) == 1) {
require_once("connect.php");
if (!$db_server) {
die("Unable to connect to MySQL: " . mysqli_connect_error($db_server));
} else {
mysqli_select_db($db_server, $db_database) or die("<h1>Couldn't find db</h1>");
//UPDATE records of users table
$query="UPDATE users SET option= .$option . WHERE ID= $dietopt";
mysqli_query($db_server, $query) or die("Update failed" . mysqli_error($db_server));
header('location: account.php');
}
require_once("db_close.php");
} else {
//do nothing
}
}
HTML 格式:
Would you like to change what your current diet is? Please select one
<br>
<td><input type="radio" name="dietopt" value="Meat-eater"/>Meat-eater</td>
<tr>
<td><input type="radio" name="dietopt" value="Vegetarian"/>Vegetarian</td></tr>
<tr>
<td><input type="radio" name="dietopt" value="Vegan"/>Vegan</td></tr>
<br>
<input type="submit" name="Change" value="Change">
在我的注册页面上,我创建了以下变量:
$dietopt=trim($_POST['dietopt']);
$_SESSION['diet'] = $diet;
我的删除表单的工作代码是:
<?php
$sess_userID = $_SESSION['userID'];
if (trim($_POST['submit']) == 'submit') {
if (trim($_POST['delete']) == 1) {
require_once("connect.php");
if (!$db_server) {
die("Unable to connect to MySQL: " . mysqli_connect_error($db_server));
} else {
mysqli_select_db($db_server, $db_database) or die("<h1>Couldn't find db</h1>");
//DELETE records from comments table
$query = "DELETE FROM comments WHERE userID=$sess_userID";
mysqli_query($db_server, $query) or die("Delete 1 failed" . mysqli_error($db_server));
//DELETE record from users table
$query = "DELETE FROM users WHERE ID=$sess_userID";
mysqli_query($db_server, $query) or die("Delete 2 failed" . mysqli_error($db_server));
//LOGOUT AND DESTROY SESSION
$_SESSION = array();
session_destroy();
header('Location: index.php');
}
require_once("db_close.php");
} else {
header('location: home.php');
}
}
与形式:
<h3><p> Are you sure you want to delete your entire account <?php echo $_SESSION['username'];?>?</p>
This will remove your username, password and any comments or photos you have uploaded to our Community forum. We promise to delete all of your details and we will not store or sell your information.</h3>
<form action="account.php" method="post">
Yes:<input type="radio" name="delete" value="1" /><br />
No: <input type="radio" name="delete" value="0" checked="checked" /><br />
<input type="submit" name="submit" value="submit" />
</form>
最佳答案
这是因为你的html代码中没有form元素 你的表格
Would you like to change what your current diet is? Please select one
<br>
<form action="your-action-page.php" method="post">
<td><input type="radio" name="dietopt" value="Meat-eater"/>Meat-eater</td>
<tr>
<td><input type="radio" name="dietopt" value="Vegetarian"/>Vegetarian</td>
</tr>
<tr>
<td><input type="radio" name="dietopt" value="Vegan"/>Vegan</td></tr>
<br>
<input type="submit" name="Change" value="Change">
</form>
处理页面
<?php
$sess_userID =$_SESSION['userID'];
$dietopt = trim($_POST['dietopt']);
if(trim($_POST['submit']) == "Change") {
if ($_POST['dietopt'] == 1) {
require_once("connect.php");
if (!$db_server) {
die("Unable to connect to MySQL:".mysqli_connect_error($db_server));
} else {
mysqli_select_db($db_server, $db_database) or die("<h1>Couldn't find db</h1>");
//UPDATE records of users table
$query="UPDATE users SET option='".$option."' WHERE ID= ".$sess_userID;
mysqli_query($db_server, $query) or die("Update failed" . mysqli_error($db_server));
header('location: account.php');
}
require_once("db_close.php");
} else {
//do nothing
}
}
关于php - 为什么我的提交按钮不能与 UPDATE 语句一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34672785/