php - 为什么我的提交按钮不能与 UPDATE 语句一起使用?

标签 php mysql forms submit

我在一页上有两个表格。一种是删除用户帐户,另一种是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/

相关文章:

php - 我使用 phpmailer,它只在我的 gmail 帐户中工作,但我尝试使用 cpanel 电子邮件(例如 :me@domainname. com)它不起作用

PHP-FPM 在执行繁重工作时用户过多时崩溃

php - 检查用户名是否已经存在不会显示正确答案

php - 如何在 OpenShift 上配置 CodeIgniter 连接?

C# 如何创建代码以使用按钮单击事件将表单设置回默认属性?

javascript - 重新触发 Javascript 事件是否安全?

php - 由 PHP 处理 DST 的时区

php - 数据库条目与输出数据不同

php - PHP的MySQL的 undefined index 错误

javascript - Ajax 表单 : new reset state