www.webdeveloper.com
Results 1 to 9 of 9

Thread: What's wrong with my MySQL Statement?

  1. #1
    Join Date
    Sep 2010
    Posts
    8

    Exclamation What's wrong with my MySQL Statement?

    I'm trying to add some info to my database via a form. I've done this int he past with no issues, but for some reason I'm having a major brain fart and it stinks.

    Here's my PHP:

    PHP Code:
    //Connect to Database
        
    $con mysql_connect("localhost:8888","root","root");
        if (!
    $con)
          {
              die(
    'Could not connect: ' mysql_error());
          }
        
    mysql_select_db("toronto_****burns"$con);
        
        
    //Add Variables Condition
        
    if ($_GET['formUpload'] == 1)  {
            
    //Gather variables from Form
            
    $firstName $_POST['first_name'];
            
    $secondName $_POST['second_name'];
            
    $lastName $_POST['last_name'];
            
    $birthDate $_POST['birth'];
            
    $deathDate $_POST['death'];
            
    $genNum "";
            
    $picURL "";
            
    $email "";
            
            
    //Add variables to Database    
            
    $addMember mysql_query("INSERT INTO family (first_name, second_name, last_name, birth_date, death_date) 
                                          VALUES ('
    $firstName', '$secondName', '$lastName', '$birthDate', '$deathDate'");
            if (!
    mysql_query($addMember,$con))  {
                    die(
    'Error: ' mysql_error());
                }
            echo 
    "1 record added";
            
            
    //echo "" . $firstName . " " . $secondName . " " . $lastName . " was born in " . $birthDate . " and died in " . $deathDate;
        

    For some reason I keep getting "Error: Query was empty". I've tried using $_POST['first_name'], etc instead of straight variables and nothing changes.

    Any ideas? Need to see more? let me know.

    Thanks!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,634
    Looks like you want to use mysql_affected_rows() instead of the 2nd instance of mysql_query().
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Sep 2010
    Posts
    8
    Like this?

    PHP Code:
    $addMember mysql_query("INSERT INTO family (first_name, second_name, last_name, birth_date, death_date)
                                          VALUES ('
    $firstName', '$secondName', '$lastName', '$birthDate', '$deathDate'"); 
            if (!
    mysql_affected_rows($addMember,$con))  { 
                    die(
    'Error: ' mysql_error()); 
                } 
            echo 
    "1 record added"
             
            
    //echo "" . $firstName . " " . $secondName . " " . $lastName . " was born in " . $birthDate . " and died in " . $deathDate; 
        

    or

    PHP Code:

    if (mysql_affected_rows() == 0)  {
        ....

    Thanks!

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,634
    Either way. Or you could assign the results to a variable, in case you want to see if for some unexplained reason it inserts more than one row. However, note that if the result is 0, mysql_error() won't say anything, as that is not a DB error. If there were an error, it would show up by the mysql_query() failing.
    PHP Code:
    $sql "
    INSERT INTO family (
       first_name,
       second_name,
       last_name,
       birth_date,
       death_date)
    VALUES (
       '
    $firstName',
       '
    $secondName',
       '
    $lastName',
       '
    $birthDate',
       '
    $deathDate'
    "
    ;
    $addMember mysql_query($sql$con) or die(mysql_error()."<br />\n$sql");
    if ((
    $rows mysql_affected_rows($addMember$con)) == 0)  {
      die(
    'Error: Unknown DB error, no row added');
    }
    else {
      echo 
    "$rows record added";

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Sep 2010
    Posts
    8
    Crud... I have a DB error. Looks like I need to review my DB connection. I know the connectiong to my DB is ok, becasue I can display the information to my page. But for some reason I can't add info to my DB..

    Any ideas?

  6. #6
    Join Date
    Sep 2010
    Posts
    8
    Correction:

    The information uploaded to my DB, but it still gave me an error code. I'm assuming it's something to do with how many rows are being affected, I'll have to test it out..

    Thanks!

    (sorry for the double-post..)

  7. #7
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    PHP Code:
    $query ="INSERT INTO family (first_name, second_name, last_name, birth_date, death_date)
                       VALUES ('
    $firstName', '$secondName', '$lastName', '$birthDate', '$deathDate')";

    // you are missing the closing paren of VALUES. 

    echo $query."<br>";

    // try echoing the query to make sure it is filled out like you think it should be. 

  8. #8
    Join Date
    Sep 2010
    Posts
    8
    Hi Dasher,

    I'm sorry, I should have added that I...added the parenthesis at the and of the query. Even then, the info is added to my DB, but the error message is still relayed. I'm thinking I'll get rid of the error message and replace it with a congrats message, Lol.

  9. #9
    Join Date
    Apr 2007
    Posts
    1,664
    Don't forget to sanitize the user input.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles