简体   繁体   中英

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. 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.

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 

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

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 ) :

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.


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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