简体   繁体   English

使用mysqli数据库检查提交表单输入

[英]Check submit form input with mysqli database

When I type a code into my form I want my PHP code to check on submit that the code exists in the database and then run MySqli query. 当我在表单中键入代码时,我希望我的PHP代码在提交时检查该代码是否存在于数据库中,然后运行MySqli查询。 I have tried to do that but I get error Cannot use isset() on the result of an expression (you can use "null !== expression" instead) I have Googled the problem but not a single one did help me to solve or understand my problem. 我尝试这样做,但出现错误Cannot use isset() on the result of an expression (you can use "null !== expression" instead)我已经用Google搜索了问题,但没有一个确实帮我解决了问题或了解我的问题。

FORM 形成

<p><b>Skriv in din laddkod nedan och tryck på "Ladda"</b></p>
<form action="laddaklar.php" method="post">
<input type="text" name="laddkod"/>
<input type="submit" name="submit" value="Ladda" />
</form>

PHP 的PHP

<?php 

session_start();

    $mysqli = NEW MySQLI ('localhost', 'root', '', 'ph');

    $laddkod = isset($_POST['laddkod']) ? $_POST['laddkod'] : '';



    $kod= "SELECT refill from card_refill"; 
    $result = $mysqli->query($kod);


    if(isset($_POST['submit'] && $laddkod==$result)){


     $resultSet = $mysqli->query ("UPDATE card_credit SET value= value + (select credit from card_refill WHERE refill='" . $_POST['laddkod'] . "') WHERE card_id = '" . $_SESSION['card'] . "' ");

 echo "<b>Ditt kort har laddats!</b>";
}
else
{
    echo "Fel laddkod";
}

The error that you're getting: 您得到的错误:

Cannot use isset() on the result of an expression 无法在表达式的结果上使用isset()

Is caused by what looks like an attempt to use an expression here: 由看起来像在此处尝试使用表达式引起的:

if(isset($_POST['submit'] && $laddkod==$result)){...

You have to close the isset() properly and remove the spurious extra ) : 您必须正确关闭isset()并删除多余的杂物)

if( isset($_POST['submit']) && $laddkod==$row['refill'] ){...
-----------------------add^  --------------------remove^

Furthermore you're not fetching any row results for the first query: 此外,您不会为第一个查询获取任何行结果:

$kod= "SELECT refill from card_refill"; 
$result = $mysqli->query($kod);
$row = $result->fetch_assoc(); // The value will be in the $row array

Then you appear to never execute the UPDATE query. 然后,您似乎永远不会执行UPDATE查询。


Additionally it is not clear where you're setting $_SESSION['card'] , but you will want to make sure it is set before attempting the UPDATE query. 另外,不清楚在哪里设置$_SESSION['card'] ,但是在尝试UPDATE查询之前,您需要确保已设置它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM