SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem entering globals into database

    For some reason the script iam building doesn't enter globals into it's database.

    PHP Code:
    mysql_select_db("slaapdatabase"$con);
    mysql_query("INSERT INTO gebruikersdata WHERE Gebruiker LIKE '$gebruiker' (A1, A2, A3, A4, A5, A6, A7, A8, A9, A10)
    VALUES ('
    $question1', '$question2', $question3$question4$question5$question6$question7$question8$question9$question10)"); 
    What should happen is that the value of global $question1 will get entered in A1, the value of $question2 in A2 etc, etc WHERE Gebruiker LIKE $gebruiker. So let's say $gebruiker is john, then it should enter the info only to john's A1 record.

    I know this is not a really advanced problem, but iam only learning PHP for two days now, so my appologies if the question is too easy

  2. #2
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1 why single quote only $question1 and $question2?

    2 if they are only integers (numbers) they dont need quoting.

    echo your variables on the page before trying to do the sql, this will help seperate PHP from Mysql errors.

    If its still not working, and you cant work out why - then insert REAL values instead.
    Upgrading to Mysql 5? Auto-increment fields now strict
    use NULL
    Or zero or leave the field name out completely.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help!

    The $question1 & 2 are indeed strings while the other ones are whole numbers without dots and comma's. I did echo the variables on the same page before it get's entered into the database. Echo-ing works but entering them into the database won't. I'll try to single quote everyone of them though.

    I don't know what you meant with REAL values though, i'll try to look that up.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    are you trying to update an exisiting record? or create a new one?

    your sql syntax is incorrect.

    it looks like youre trying to update an existing record, and not add a new one.
    heres an abbreviated update syntax
    PHP Code:
    $sql "UPDATE gebruikersdata
    SET A1 = '
    $question1',
        A2 = '
    $question2'
        etc...
    WHERE Gebruiker = '
    $gebruiker' ";

    // see what we are sending to sql
    echo $sql;

    $result mysql_query($sql) or die(mysql_error()); 

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    are you trying to update an exisiting record? or create a new one?

    your sql syntax is incorrect.

    it looks like youre trying to update an existing record, and not add a new one.
    heres an abbreviated update syntax
    PHP Code:
    $sql "UPDATE gebruikersdata
    SET A1 = '
    $question1',
          A2 = '
    $question2'
          etc...
    WHERE Gebruiker = '
    $gebruiker' ";

    // see what we are sending to sql
    echo $sql;

    $result mysql_query($sql) or die(mysql_error()); 
    Thanks a lot!

    I'll try this asap. Brings me to my next question though. If i set the default value to something, would it be possible to update instead of create a new one?

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can only update a row that already exists.

    for example, there must already be a record for john. the other fields like A1, A2 etc... dont need to have values, but that row for john must exist. otherwise mysql will never find a row where Gebruiker = 'john' because it doesnt exist, so there will be nothing to "update"

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for all your help. I didn't have the time to test it since iam on another PC at this moment but i'll sure do.

    The record & row for John does exist:
    - Databasename
    - Username | Password | A1 | A2 etc.
    - John | Johnspass | x | x etc.

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I kept trying but for some reason it doesn't seem to work. This is the exact code i used, i only changed the db login and password:

    PHP Code:
    $con mysql_connect("localhost","login","password");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }
    mysql_select_db("slaapdatabase"$con);
    $sql "UPDATE gebruikersdata 
    SET A1 = '
    $question1', 
          A2 = '
    $question2',
          A3 = '
    $question3',
          A4 = '
    $question4',
          A5 = '
    $question5',
          A6 = '
    $question6',
          A7 = '
    $question7',
          A8 = '
    $question8',
          A9 = '
    $question9',
          A10 = '
    $question10'
        
    WHERE Gebruiker='
    $gebruiker' "

    echo 
    $sql

    $result mysql_query($sql) or die(mysql_error()); 
    This is the error i get
    PHP Code:
    UPDATE gebruikersdata SET A1 'No'A2 'No'A3 'No'A4 'No'A5 'Yes'A6 'No'A7 'No'A8 'No'A9 'No'A10 'No'WHERE Gebruiker='john' You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE Gebruiker='john'' at line 13 
    Line 13 has nothing to do with the SQL nor the php script. It's not even close to where the PHP located.

    I echoed $gebruiker and all $question$ and they are like they should be. I tried LIKE instead of = but that didn't work either. I even tried replacing $gebruiker with some test account, but that failed as well.

    I have no clue what to do

  9. #9
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the code you posted is not the code you ran.

    in the code you ran, you put in a comma
    A10 = '$question10',

    dont put a comma after the last value.

  10. #10
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry wwctw, when I was learning/debugging instead of trying to chase errors from PHP into Mysql and back I would start off using REAL values....

    So instead of losing your mind wondering if John is John' or even John[space] why not just write John in your sql until you sort it out.

    $sql = "UPDATE gebruikersdata
    SET A1 = '$question1',
    A2 = '$question2',
    A3 = '$question3',
    A4 = '$question4',
    A5 = '$question5',
    A6 = '$question6',
    A7 = '$question7',
    A8 = '$question8',
    A9 = '$question9',
    A10 = '$question10'

    WHERE Gebruiker='John' ";


    I am sorry I didnt pick up that this was an update rather than a new row.

    If your values are integers - then you were quite right not to quote them.

    Keep at it wwctw!
    Upgrading to Mysql 5? Auto-increment fields now strict
    use NULL
    Or zero or leave the field name out completely.

  11. #11
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really appreciate both your time for helping a newbie.

    clamcrusher > You are right i used the , behind the last value, but directly after i posted it here i noticed it and tried it again without the comma. It didn't change anything unfortunatelly.

    fofoonet > I tried a real value like John instead of $gebruiker. John ofcoarse is in the database, but iam getting the same error.

    I really don't understand, iam a newbie, but i've managed to enter data into a database before. That's why John is already in

    Perhaps the error is somewhere in the database, cause for all iam looking, the code seems fine.
    The Database is called slaapdatabase,
    The Table is called gebruikersdata
    I have columns like Gebruiker, A1 etc
    John is in column Gebruiker and it's A1 etc field are empty.

    I want to add info on A1 etc on John's row.

  12. #12
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    YES IT WORKS!

    Damn iam so happy, this really took me hours and hours..

    All i eventually did was remove the space in the last line:
    PHP Code:
    WHERE Gebruiker='$gebruiker' "; 
    It's now
    PHP Code:
    WHERE Gebruiker='$gebruiker'"; 
    Thank you guys so mutch for all the help.. i was nearly at the point to start my (crappy) book all over again..

  13. #13
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I share your joy (even though nothing I said was apparently valid) - still, your not alone, see?
    Upgrading to Mysql 5? Auto-increment fields now strict
    use NULL
    Or zero or leave the field name out completely.

  14. #14
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your answers helped me trough the process just as mutch!

    I don't have the knowledge to help others with PHP yet but iam working for years and years with Photoshop, Illustrator, Flash, HTML, etc. so i can help people on sitepoint with that. These forums are great to share knowledge together.


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
  •