SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 26
  1. #1
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cannot Post Data to mysql Database

    I am running into trouble posting data into my mysql database. I thoroughly looked over the code, and I can't seem to find the matter. Here is my code below, please let me know what I am doing wrong.

    $query = "INSERT INTO contacts ('firstname','lastname','phone','email','address','category','private','yearofbirth')
    VALUES ('$firstname','$lastname','$phone','$email','$address','$category','$private','$yearofbirth')"

    or die(mysql_error());

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by ikilledafly View Post
    Here is my code below, please let me know what I am doing wrong.
    you're trying to insert values into strings

    $query = "INSERT INTO contacts ('firstname','lastname','phone','email','address','category','private','yearofbirth')

    see those quotes? removes dem!!

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    or make them into backticks. ` != '
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For safety when it comes to restricted keywords (incase you have them as column names) always add `` around a column name and '' around your values. E.g:
    $query = "INSERT INTO contacts (`firstname`,`lastname`...) VALUES('$firstname','$lastname'...)
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by StarLion View Post
    or make them into backticks.
    NOOOOOOOOOOOOOOOOOOOooooo.......
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Too much time on your hands today, Rudy ;P
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by smftre View Post
    ...always add `` around a column name and '' around your values.
    also NO

    especially not "always"

    add backticks only if you've been unfortunate enough to inherit a database design from a clueless noob who declared table or column names that are reserved words or contain special characters

    far better is not to declare problematic table or column names in the first place, and don't use backticks at all
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  8. #8
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Original code that isn't functioning correctly

    thanks for the quick replies. I have tried the suggestions given to me, but with no luck. I changed the '$private' variable to '$if_private', just to see if that would help, and to no surprise, it didn't. Here is below the original code, and it doesn't work correctly.

    $query = "INSERT INTO contacts (firstname,lastname,phone,email,address,category,if_private,yearofbirth) VALUES ('".$firstname."','".$lastname."','".$phone."','".$email."','".$address."','".$category."','".$if_private."','".$yearofbirth."')";

  9. #9
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Before you run mysql_query($query) try doing: die($query); to see what it outputs, you should also make sure you've escaped you variables, e.g. before your $query, do: $firstname = mysql_real_escape_string($firstname); etc..
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  10. #10
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    before you do anything, test your query directly in mysql (i.e. not via php)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  11. #11
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,065
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Can you please post the value of $query (using sample data for the insert)?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  12. #12
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is the $query I use in my program, below

    $query = "INSERT INTO contacts ('firstname') VALUES ($firstname)";


    this is output to the screen, with 'joe' being the data I used

    INSERT INTO contacts ('firstname') VALUES (joe)


    But after I look at my database, nothing in there

  13. #13
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is the modified code, after listening to all the suggestions; nothing is found inside the database. Here is the code, can anybody spot anything wrong with it?

    // run this only, once the user has hit the "Add Contact" button

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

    // assign form inputs

    $firstname = $_POST['firstname'];


    // validate inputs

    if ( !empty($firstname) ) {

    // add member to database

    $query = "INSERT INTO contacts ('firstname') VALUES ($firstname)";

    $result = mysql_query($query);

    $message = "\"".$firstname." \" has been successfully added.";

  14. #14
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    I think you misinterpreted Rudy in post #2

    Code PHP:
    // run this only, once the user has hit the "Add Contact" button
     
     
    if (isset($_POST['addcontact'])) {
     
     
    // assign form inputs
     
     
    $firstname = $_POST['firstname'];
     
     
     
     
    // validate inputs
     
     
    if ( !empty($firstname) ) { 
     
     
    // add member to database
     
     
    $query = "INSERT INTO contacts (firstname) VALUES ('$firstname')";
     
     
    $result = mysql_query($query);
     
     
    $message = "\"".$firstname." \" has been successfully added.";

    note the '
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  15. #15
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    misinterpreted? more like ignored
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  16. #16
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried and tried every combination; with single quotes, double quotes, etc.. Here is the code with all the apostrophe and quotation marks removed like asked to do.




    // run this only, once the user has hit the "Add Contact" button

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

    // assign form inputs

    $firstname = $_POST['firstname'];


    // validate inputs

    if ( !empty($firstname) ) {

    // add member to database

    $query = "INSERT INTO contacts (firstname) VALUES ($firstname)";

    $result = mysql_query($query);

    $message = "\"".$firstname." \" has been successfully added.";

  17. #17
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    $query = "INSERT INTO contacts (firstname) VALUES ($firstname)";
    well, you've finally removed the quotes from the column name

    now all you have to do is put quotes around the value, so that it is interpreted as a string


    this would all have gone so much more smoothly if you had tested the sql statement outside of php first
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  18. #18
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php code not entering database

    This is the output I receive after correcting the code to work with help push data into the database. Again nothing is found inside the database.

    INSERT INTO contacts (firstname) VALUES (John) <<output
    'John' is the data I entered.

    if this is not the way the code should look like, please inform.


    // run this only, once the user has hit the "Add Contact" button

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

    // assign form inputs

    $firstname = $_POST['firstname'];

    // validate inputs

    if ( !empty($firstname) ) {

    // add member to database

    $query = "INSERT INTO contacts (firstname) VALUES ('$firstname')";

    $result = mysql_query($query);

    $message = "\"".$firstname." \" has been successfully added.";

    Header("Location: listcontacts.php");

  19. #19
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    before you do anything, test your query directly in mysql (i.e. not via php)
    How do I test out my query in mysql, is there a function or command I can use that can get me there.

  20. #20
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    a lot of people use phpmyadmin, but i don't like it

    download heidisql and give it a spin

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  21. #21
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    a lot of people use phpmyadmin, but i don't like it

    download heidisql and give it a spin

    I appreciate all of the assistance you have thus far provided. heidisql is for Windows, and I'm running on a Mac. all my bootcamp files are corrupt, so I can't access Windows.


    this nut is harder to crack than I had thought.

  22. #22
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    what's a Mac?






    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  23. #23
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    just kidding

    try the mysql query browser

    http://dev.mysql.com/doc/query-brows...ation-osx.html
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  24. #24
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Can anyone point out a solution...

    Quote Originally Posted by r937 View Post
    just kidding

    try the mysql query browser

    http://dev.mysql.com/doc/query-brows...ation-osx.html
    No luck there either. Can you look over the code in my most recent post and see what I'm doing wrong. This 'problem' has paralyzed me for 2 days.

  25. #25
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by ikilledafly View Post
    Can you look over the code in my most recent post and see what I'm doing wrong.
    you mean this?
    $query = "INSERT INTO contacts (firstname) VALUES ('$firstname')";
    the SQL looks fine, i cannot help you with the php, sorry
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Tags for this Thread

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
  •