A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: [RESOLVED] Errors with php query to mysql db

  1. #1
    Member
    Join Date
    Dec 2002
    Location
    Lawrence, Kansas
    Posts
    53

    resolved [RESOLVED] Errors with php query to mysql db

    Hi All,

    I'm really about to lose it on this one. I have a set of scripts that retrieve and send data to a mySQL DB, and they work fine on my server, but on my client's server, they are not functioning, and I've been at it all night for several nights just trying to get ONE single query to work, but I just can't!! I'm hoping someone can see something I can't.

    mysql version is 5.0.21 and PHP Version is 5.1.4.

    Here's part of the file (the queries):
    PHP Code:
            $thedate gmdate("Y-m-d"$timestamp);
        
    $thetime gmdate("g:i:s"$timestamp);

        
    $connection mysql_connect ("localhost"$username$userpass) or die ('I cannot connect to the database because: ' mysql_error());
        
    mysql_select_db ($myDB);

        
    $myQuery "INSERT INTO clock (`id`, `eid`, `date`, `in`, `out`, `notes`) VALUES ('', '0',\'$thedate\', \'$thetime\', '0', '$_POST[notes]')";
        
        
    //$myQuery2 = 'INSERT INTO `ngenmedi_employees`.`clock` (`id`, `eid`, `date`, `in`, `out`, `notes`) VALUES (NULL, \'0\', '$thedate', '$thetime', \'00:00:00\', \'\')';

        
    $success mysql_query ($myQuery) or die("can't execute query " mysql_error()); 
    myQuery outputs this in php:
    can't execute query You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '\'2007-10-29\', \'5:07:15\', '0', '')' at line 1
    myQuery2 outputs this:
    Parse error: syntax error, unexpected T_VARIABLE in /home/ngenmedi/public_html/employees/scripts/clockin.php on line 49
    Where line 49 is the line myQuery = ....

    And I have no idea what's going wrong, and I believe I've tried every single variation of these queries, adding and removing \'s, '', all sorts of things, I just can't figure it out!!! I even once got the script to execute, but instead of my date and time strings defined in php, it just inserted "0000-00-00".

    Let me know if you come up with anything please!

    -csean

  2. #2
    Official FK nice guy and MOD 3PRIMATES's Avatar
    Join Date
    Apr 2002
    Location
    Portland Oregon
    Posts
    1,645
    Errors are commented and fixed in code:

    PHP Code:
    $thedate gmdate("Y-m-d"$timestamp);
        
    $thetime gmdate("g:i:s"$timestamp);
        
    // make sure to secure data before using in within a query.
    //This will help to prevent a SQL injection
    $notes mysql_real_escape_string($_POST['notes']);

        
    $connection mysql_connect ("localhost"$username$userpass) or die ('I cannot connect to the database because: ' mysql_error());
        
    mysql_select_db ($myDB);

    // You were using backticks ` here instead of  single quotes
    // thedate and thetime did not need to be escaped, and I used a more desirable format for the variable inclusion
        
    $myQuery "INSERT INTO clock ('id', 'eid', 'date', 'in', 'out', 'notes') VALUES ('', '0','".$thedate."', '".$thetime."', '0', '".$notes."')";
        
        
    $success mysql_query ($myQuery) or die("can't execute query " mysql_error()); 
    Hope it helps,

    3P

  3. #3
    Member
    Join Date
    Dec 2002
    Location
    Lawrence, Kansas
    Posts
    53
    Hi 3P,

    Thanks a lot for the response! I'm still getting an error I'm afraid:
    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user: 'root@localhost' (Using password: NO) in /home/ngenmedi/public_html/employees/scripts/clockin.php on line 61

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/ngenmedi/public_html/employees/scripts/clockin.php on line 61

    can't execute query You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ''id', 'eid', 'date', 'in', 'out', 'notes') VALUES ('', '0','200
    The last one is the one I'm really worried about. To get rid of the warnings I just changed the line as follows:
    PHP Code:
    //from:
    //$notes = mysql_real_escape_string($_POST['notes']);
    //to:
    $notes $_POST['notes']; 
    I figure I'll worry about one problem at a time and try to fix that real_escape issue after I figure out why the query won't execute.

    Any ideas?

  4. #4
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    The username and password you are passing needs to be set according to the client's server database username/password. They are not all the same.

  5. #5
    Member
    Join Date
    Dec 2002
    Location
    Lawrence, Kansas
    Posts
    53
    Hi sstalder,

    The code I've posted isn't the entire file, I'm able to connect without problems to the server and database, and I have the login details stored at the beginning of my script as strings, so I don't think that could be causing a problem.

    Is that what you meant?

  6. #6
    Official FK nice guy and MOD 3PRIMATES's Avatar
    Join Date
    Apr 2002
    Location
    Portland Oregon
    Posts
    1,645
    Hey there, sorry there was an error in my last code.
    Its been awhile and made the same mistake you did, LOL

    replace the line below.
    You you should not have quotes around the row names.
    PHP Code:
    $myQuery "INSERT INTO clock (id, eid, date, in, out, notes) VALUES ('', '0','".$thedate."', '".$thetime."', '0', '".$notes."')"
    3P

  7. #7
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    The query should be this:
    PHP Code:
    $myQuery "INSERT INTO clock (`id`, `eid`, `date`, `in`, `out`, `notes`) VALUES (NULL, 0,'".$thedate."', '".$thetime."', 0, '".$notes."')"
    Auto-increment fields should return null.
    Number fields should insert as numbers, not strings.
    Hopefully that helps.

  8. #8
    Official FK nice guy and MOD 3PRIMATES's Avatar
    Join Date
    Apr 2002
    Location
    Portland Oregon
    Posts
    1,645
    Hi, in the above I would remove the back ticks

    3P

  9. #9
    Member
    Join Date
    Dec 2002
    Location
    Lawrence, Kansas
    Posts
    53
    Okay, the first query from 3P still returned errors, but the last one from sstalder did actually work!!! I'm not sure if its because the back ticks are necessary, or if it was because I was attempting to insert strings into an integer type, but the working code is:
    PHP Code:
    $myQuery "INSERT INTO clock (`id`, `eid`, `date`, `in`, `out`, `notes`) VALUES (NULL, 0,'".$thedate."', '".$thetime."', 0, '".$notes."')"
    Thank you both very very much!

  10. #10
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    "back ticks" are actually recommended in querys with PHP/SQL, other languages that use SQL such as ASP, etc does not matter.


  11. #11
    Official FK nice guy and MOD 3PRIMATES's Avatar
    Join Date
    Apr 2002
    Location
    Portland Oregon
    Posts
    1,645
    Odd, I have never seen them used outside of an actual command line query to a database. As in running a query straight through the sql admin console.

  12. #12
    Member
    Join Date
    Dec 2002
    Location
    Lawrence, Kansas
    Posts
    53
    I was under the same impression 3P, but this new version must have changed something, it's so frustrating having to keep 3 different versions of these scripts now!

  13. #13
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    Well trust me they change daily The best thing to do is just read up on standards and try your best to follow them (that day) because they change so much and are very different depending on the server/os/php version/mysql version, etc as you will run into that in your future projects.

    Im glad it works for you and feel free to pm me at anytime with questions.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center