Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2013
    Location
    Finland/Estonia
    Posts
    53
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Question PHP Testing shows query was successful but nothing shows up in the database

    Hello. All my PHP side checks are telling me the query was successful but no information shows up in the database.
    PHP Code:
    $user_id $_SESSION['user_id'];

    $newTitle mysqli_real_escape_string($db$_POST['newEventTitle']);
    $newDesc mysqli_real_escape_string($db$_POST['newEventDescTxt']);
    $user_id mysqli_real_escape_string($db$_POST['user_id']);
    $postDate $_POST['datepicker'];
    $newTime $_POST['newTime'];
    date_default_timezone_set('UTC');
    $theDateTime strtotime($postDate.' '.$newTime);

    if(isset(
    $newTitle) && isset($newDesc) && isset($user_id) && isset($theDateTime) && isset($postDate))
    $sql "INSERT INTO events (title, desc, datetime, user_id, clock) VALUES ('$newTitle', '$newDesc', '$theDateTime', '$user_id', '$newTime')";
    echo 
    $sql;
    $insertQ $db->query($sql);
    if(
    $db->affected_rows){
    echo 
    "<br>Data submitted.<br>"
    Database:


    thanks.
    Last edited by estXcrew; 02-05-2014 at 03:59 PM.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,077
    Thanks
    2
    Thanked 320 Times in 312 Posts
    What does using: var_dump($db->affected_rows); show?

    A query that fails due to an error will return a -1 for affected rows, which is considered to be a true value.

    Before you can use any of the results from a query (the result set from a select query or affected_rows from an insert/update query), you must test if the query ran without any errors.

    Edit: in looking at your sql, desc is a reserved mysql keyword. Using it as a column name requires special handling, you must enclose it in back-ticks ``. It is best to not use reserved keywords as column names.

    You should still add error checking to your queries so that you know when they are failing.
    Last edited by CFMaBiSmAd; 02-05-2014 at 04:14 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Jul 2013
    Location
    Finland/Estonia
    Posts
    53
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Yes, I fixed it by backticking the desc Field. I knew that it was reserved, but i didn't get it myself.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The reason why it looked-ish like no problem occurred was here:
    PHP Code:
    if($db->affected_rows){
    echo 
    "<br>Data submitted.<br>"
    $db is a connection level object. So as long as your previous insert/delete/update statement changed at least one record, than the value is > 0.
    This here needs to handle the errors:
    PHP Code:
    ;
    $insertQ $db->query($sql); 
    Which would be replaced with:
    PHP Code:
    if ($insertQ $db->query($sql))
    {
        if (
    $db->affected_rows 0)
        {
            
    printf("Successful.");
        }

    Effectively, your query has failed. But since affected rows is a connection level call, there is no way to determine if the previous call was the one that last performed an alteration. Blocking the if branch will fix that.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    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
    •