【问题标题】:PHP: How to change value on validation in SQL?PHP:如何在 SQL 中更改验证值?
【发布时间】:2015-09-28 23:12:51
【问题描述】:

我有这个代码

<form action="" method="POST">
PIN: 
<input type="text" name="pin"><br />
E-Mail: 
<input type="text" name="email"><br />  
<input type="submit" value="Claim" name="submit" />
</form>


<?php
if(isset($_POST["submit"])){

if(!empty($_POST['pin']) && !empty($_POST['email'])) {
$pin=$_POST['pin'];
$email=$_POST['email'];


$con=mysql_connect('localhost','root','admin') or die(mysql_error());
mysql_select_db('user_codevalidation') or die("cannot select DB");

$sql="INSERT INTO codes(email) VALUES('$email') WHERE pin='".$dbpin."'";
mysql_query($sql);
echo "Successfully claimed!";
{
while($row=mysql_fetch_assoc($query))
{
$dbpin=$row['pin'];
}

if($pin == $dbpin)
{
$sql="INSERT INTO codes(email,claimed) VALUES('$email','1') WHERE pin = $dbpin";
echo "Successfully claimed!";
}
} else {
echo "Invalid pin or it has already been claimed once!";
}

} else {
echo "All fields are required!";
}
}
?>

我希望能够验证pin AND 如果在$pin 行中的数据库中声明= 0,其中pin 等于pin 文本框$pin 中的值,如果相等,则执行$sql 是将 email 中的值插入到 sql $email 是电子邮件文本框,然后将 claimed 的值更改为 '1',在数据库中默认为 0。

一切正常,它显示“已成功认领!”但它不会改变我在数据库中的值。请帮忙!

编辑:我添加了执行 MySQL 查询,它仍然给我相同的结果!

【问题讨论】:

    标签: php mysql sql forms validation


    【解决方案1】:

    没有查询执行命令mysql_query() 在查询后添加这一行

    mysql_query($sql);
    

    【讨论】:

      【解决方案2】:

      您需要确保执行查询:

      mysql_query($sql);
      

      http://php.net/manual/en/function.mysql-query.php

      【讨论】:

        【解决方案3】:

        你需要执行查询

        编辑 我添加了一些行以调试您的查询。如果您发现任何错误,请复制并粘贴并告诉我。

        别针:
        电子邮件:
        <?php
        ini_set('display_startup_errors',1);
        ini_set('display_errors',1);
        error_reporting(-1);
        if(isset($_POST["submit"])){
        
            if(!empty($_POST['pin']) && !empty($_POST['email'])) {
                $pin=$_POST['pin'];
                $email=$_POST['email'];
        
                $con=mysql_connect('localhost','root','admin') or die(mysql_error());
                mysql_select_db('user_codevalidation') or die("cannot select DB");
        
                $query=mysql_query("SELECT * FROM codes WHERE pin='".$pin."'");
                $numrows=mysql_num_rows($query);
                if($numrows!=0)
                {
                    while($row=mysql_fetch_assoc($query))
                    {
                        $dbpin=$row['pin'];
                    }
        
                    if($pin == $dbpin)
                    {
                        $sql="INSERT INTO codes(email,claimed) VALUES('".$email."','1') WHERE pin = '".$dbpin."'";
                        $res = mysql_query($sql) or die(mysql_error());
                        echo "Successfully claimed!";
                    }
                } else {
                    echo "Invalid pin or it has already been claimed once!";
                }
        
            } else {
                echo "All fields are required!";
            }
        }
        ?>
        

        【讨论】:

        • 嗨,我已经改变了它,但它仍然没有执行它,也没有改变我数据库中的值。
        • 这是我的代码现在仍然给我相同的结果$sql="INSERT INTO codes(email) VALUES('$email') WHERE pin='".$dbpin."'"; mysql_query($sql); echo "Successfully claimed!";
        • 嗨,我收到了这个错误,Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\xampp\htdocs\couponreg\login.php on line 27 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE pin = '0000'' at line 1
        • 嗯......你去。解决这个问题!尝试删除单引号 '
        • 嗨,现在没有单引号仍然会出现同样的错误.....Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\xampp\htdocs\couponreg\login.php on line 27 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE pin = 0000' at line 1
        猜你喜欢
        • 2014-12-13
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        • 2017-12-06
        • 1970-01-01
        • 2019-02-26
        相关资源
        最近更新 更多