Using a variable in the WHERE Clause

后端 未结 3 413
深忆病人
深忆病人 2021-01-27 01:22

I am a newbie in MySQL and PHP. I have a HTML form where I would like to pass 1 variable from to my PHP code and then run a query on my database for the record that holds that v

相关标签:
3条回答
  • 2021-01-27 01:39

    I agree its a quote issue, but here is how my code would look.

      $sql = 'SELECT * FROM SiteInfo WHERE Serial = "' . $serial . '"';
    

    or

      $sql = "SELECT * FROM 'SiteInfo; WHERE 'Serial' = \"$serial\"";      
    
    0 讨论(0)
  • 2021-01-27 01:49

    Looks like a quote issue:

     $sql = 'SELECT * FROM `SiteInfo` WHERE `Serial` ='.$serial.'; 
    

    should be

     $sql = "SELECT * FROM `SiteInfo` WHERE `Serial` ='".$serial."'"; 
    
    0 讨论(0)
  • 2021-01-27 01:52

    It means your variable:

    $_POST['Serial']
    

    is coming empty. You need to run your code if it isn't empty by checking it via isset like this:

    if (isset( $_POST['Serial'])) {
      $serial= $_POST['Serial'];
    
      // your rest of the code
    }
    

    Also if Serial is string and not a number, you need to put it in quotes, use below query:

    $sql = "SELECT * FROM `SiteInfo` WHERE `Serial` = '$serial'"; 
    

    You can also check out what does your query come up like:

    $sql = "SELECT * FROM `SiteInfo` WHERE `Serial` = '$serial'"; 
    echo $sql;
    exit;
    
    0 讨论(0)
提交回复
热议问题