我在一个页面上有两个表单。一种是删除用户帐户,另一种是UPDATE
用户详细信息(用户的饮食要精确)。表单使用不同的values
作为提交按钮,因为它们是两个单独的表单,用户只选择一个填写,而不是两个。
我试图让我的UPDATE语句的提交按钮工作,但按下按钮时没有任何反应。 UPDATE语句用于更新存储在MySQL数据库中的“用户”表中的用户'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;
我删除表单的WORKING代码是:
<?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>
答案 0 :(得分:1)
这是因为你的html代码中没有表单元素 你的表格
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
}
}