Reading data from MYSQLAsk Question

问题:

If you input in text_username is same in username -values in my table- the browser will echo Correct but if the text_username is not the same values in -values in my table- the browser will echo Wrong. That will be shown in browser but the code that will read is the first row in my table but in the second one will not read. Thanks in advance

For example I have values in my table that the Username is dave, then if I click the button, the browser will enter correct, but I have username in my table that spencer, then if i input that browser it will echo wrong.

PS: I edit this question.

Here's the structure:

$reservation = mysqli_query($db, "SELECT * from reservation");
//qwe is the button
    $username = $_POST['text_username'];
    if(isset($_POST['qwe']) && $row = mysqli_fetch_array($reservation))
    {
        if($row['username'] == $username) 
        {
            echo "Correct";
        }
        else
        {
            echo "Wrong";
        }
      }
     ?>

回答1:


You are doing it Wrong You forget to add 'where' keyword in the query Where keyword is used find particular row with particular value in particular column

if(isset($_POST['qwe'])){

        if(!empty($_POST['text_username']))
        {
$username = $_POST['text_username'];
$reservation = mysqli_query($db, "SELECT * from reservation where username='$username'");     
            if(mysqli_num_rows($reservation)) 
            {
                echo "Correct";
            }
            else
            {
                echo "Wrong";
            }
          }
}
         ?>
标签: mysql
© 2014 TuiCode, Inc.