SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $var can 'echo' but cant do 'sql_query'

    Hi everyone!

    I have a 3 variables that can echo, but the third makes the mysql_query fail.

    Here is the code:
    _______________________________________________________________
    echo $uage,"<br />";
    echo $usex,"<br />";
    echo $uloc,"<br />";

    mysql_query("INSERT INTO main (age,sex,loc) VALUES ($uage,$usex,$uloc)");
    printf("Last inserted record has id %d\n", mysql_insert_id());

    echo "<br />", $uloc;
    _______________________________________________________________

    The database table is:

    AGE:tinyint(4) NOT NULL
    SEX:tinyint|(1) NOT NULL
    LOC:varchar(20) NOT NULL

    The result of the above code is:

    age: 1955-07-12
    sex: 1
    location: new zeland
    Last inserted record has id 0
    location: new zeland

    If i replace "$uloc" in the "mysql_query" line with "$uage/$sex" or " 'ABCDEF' "

    then it updates the DB okay and sais

    Last inserted record has id 23

    please help! i have only programmed for 1 week! what can it be?

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    location is a string, so it needs to be enclosed in single quotes inside the SQL query.
    echo your SQL query to debug, you can also make use of the mysql_error function.

    Don't forget too that you need to escape (mysql_real_escape_string) data before putting it in a query.

  3. #3
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    mysql_query("INSERT INTO main (age,sex,loc) VALUES ($uage,$usex,'$uloc')"); or die("Cannot execute:"mysql_error()); 
    The above uses the mysql_error() function and it is used for debugging as cranial mentioned. Through this function you can make a accurate diagnose of the problems and issues in your queries.

  4. #4
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_insert_id returns the
    ID generated for an AUTO_INCREMENT column by the previous query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established.
    Do you have an auto incremented column in your database table?
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou so much! that was really snagging my cranium!

    the single quotes solution worked.

    that is brilliant advice abotu debug methodology.

    thanks!

    yes i have a primary auto increment key, i am not sure of all the config in the table but for the most part its going very well! thankyou!!!

  6. #6
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you add mysql_real_escape_string to your code?


Bookmarks

Posting Permissions

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