INSERT MySQL query always causing an error.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dgourd
    New Member
    • Apr 2010
    • 25

    INSERT MySQL query always causing an error.

    I am trying to create a registering script, but everytime I try to insert the data into my database i get an error.

    Code:
    $query = "INSERT INTO members (username, pass, email, first_name, last_name) VALUES ($username, $password, $email, $f_name , $l_name);";
    $q = mysqli_real_query($dbc, $query);
    if($q) {
        // My code goes here
    } else {
        // Display an error
    }
    Everytime, $q returns false and goes to the else clause which displays the error. I am sure the database is set up properly, but I can't figure out why it is always returning false.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    What is the error?

    I would guess it has to do with the fact that the string values in your VALUES clause are not quoted. All strings inside MySQL queries need to be enclosed in single-quotes, or they will be parsed as SQL commands rather than string values.
    [code=php]<?php
    // INCORRECT
    $sql = "INSERT ... VALUES($name, $password)";

    // Correct
    $sql = "INSERT ... VALUES('$name', '$password')";
    ?>[/code]

    Comment

    • dgourd
      New Member
      • Apr 2010
      • 25

      #3
      Thank you very much. I was putting the single quotes on the column names instead of the values.

      Comment

      Working...