【问题标题】:Why is my submit button not working in conjunction with UPDATE statement?为什么我的提交按钮不能与 UPDATE 语句一起使用?
【发布时间】:2016-04-12 21:13:31
【问题描述】:

我在一页上有两个表格。一种是删除用户帐户,另一种是UPDATE用户详细信息(准确地说是用户“饮食”)。这些表单使用不同的values 作为提交按钮,因为它们是两个单独的表单,用户只选择一个来填写,而不是两个都填写。

我试图让我的 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>

【问题讨论】:

  • 条件错误$_POST['submit']) == "Change"),因为您的字段名称是name="Change"
  • 语法更新查询也有错误query="UPDATE users SET option= .$option . WHERE ID= $dietopt";
  • 另一句话是:if (trim($_POST['dietopt']) == 1) { 它永远不会是 1..
  • 你的提交按钮是否在&lt;form&gt;标签中??
  • @saty 好的,我要把它改成 $_POST['change']) 吗?我认为 == 意味着它知道要使用哪个提交按钮。我在更新查询中做错了什么语法?我是新手,抱歉。

标签: php mysql forms submit


【解决方案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
    }
}

【讨论】:

  • 谢谢。我添加了这个,但它没有更新 mysql 数据库中的任何内容。
  • 将 WHERE ID=".$dietopt; 更改为 WHERE ID=".$sess_userID;在您的查询中
  • 不,那也没用。感谢您帮助我
  • 您能否回显您的查询并在此处发布,只需键入 echo $query;die(); $query="UPDATE users SET option='".$option."' WHERE ID=".$sess_userID; 之后
  • ok 这出现了:解析错误:语法错误,第 77 行 /home/me14ch/public_html/chefsclaw/account.php 中的意外 '$query' (T_VARIABLE)
猜你喜欢
  • 1970-01-01
  • 2020-05-06
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-29
相关资源
最近更新 更多