SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What occassion inserted data not available in databse ?

    Hi,

    I have recently inserted data into databse successfully. But I when I check in database nothing inserterd.

    Here's the code

    Code PHP:
    $result = mysql_query("INSERT INTO listing (username, catlvl1, catlvl2, catlvl3) VALUES('{$username}', '{$jumpmenu}', '{$jumpmenu2}', '{$jumpmenu3}' )");
    		if(mysql_affected_rows($result)>0)
    		{	
    			header('location:'.$url.'sell_step2.php');
    			exit();
    		}

    I've got directed after that code executed, meaning the insert was successful.

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it on InnoDB tables ?
    And is this the only query to be fired after the user action, or is it one in a serie.

    On a transaction enabled DB, this query could have run without problem.
    But, if something hapenned in another query, later, in the same transaction, the DB can rollback the complete batch, thus leading to the point you have no datas in the tables.

    What is the query exactly? Could you log it, and replay it by hand?

  3. #3
    SitePoint Zealot stuffedbuggy's Avatar
    Join Date
    Sep 2008
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try removing the curly braces and see if that helps.
    You know you cooler than me...

  4. #4
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tripy View Post
    Is it on InnoDB tables ?
    And is this the only query to be fired after the user action, or is it one in a serie.

    On a transaction enabled DB, this query could have run without problem.
    But, if something hapenned in another query, later, in the same transaction, the DB can rollback the complete batch, thus leading to the point you have no datas in the tables.

    What is the query exactly? Could you log it, and replay it by hand?
    It's InnoDB

    Its the only query

    Not sure if transaction enabled

    The query is to insert three values of 3 html menus, and a cookie vlaue

    How can I log it and replay it by hand ?

  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How can I log it and replay it by hand ?
    by doing:
    PHP Code:
    function log($str){
      
    error_log($str3'query.log');
    }

    $query="INSERT INTO listing (username, catlvl1, catlvl2, catlvl3) VALUES('{$username}', '{$jumpmenu}', '{$jumpmenu2}', '{$jumpmenu3}' )";
    log($query);
    $result mysql_query($query); 
    This will create a file query.log in the same directory than your script, and append the query that is sent to the db at each call.
    http://www.php.net/error_log

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You arent feeding the correct argument to mysql_affected_rows(). See the documentation.

    Standard debugging is to also echo out the actual query text, as well as check mysql_error(). You really should be doing this every single time like second nature, when you have a problem.

  7. #7
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys for your reply, it all comes down to a mistake in html form

    I put the wrong value for action attribute in form field, no wonder the code process some other random page then returns that random page.

    Now the code works as expected.

    One thing I want to know why, just now, I echo the $result, the output is 111

    What does that mean ?

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where ever the problem is in your case for now, the strongly suggested that you always use at least mysql_error() after some query execution specially when you are in development phase.
    PHP Code:
    mysql_query("INSERT INTO tblename (field1,field2) VALUES('value1','value2')") or die(mysql_error()); 
    So that mysql_error() function will terminate (the function die()/exit() will terminate the script) and will give the reasonable error message to you and you can understand what is happening.

    If the error is Fatal then it will automatically terminate the flow, otherwise if you don't want to terminate the script and just want to see the error/warning messages then simply you can echo the error/warning message.
    PHP Code:
    $result mysql_query("INSERT INTO tblename (field1,field2) VALUES('value1','value2')");
    if(!
    $result){
        echo 
    mysql_error();

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by runrunforest View Post
    One thing I want to know why, just now, I echo the $result, the output is 111
    The documentation sais mysql_query() will return a resource, or boolean false. A resource is a special data type that can't really be displayed in a textual format. php is probably just showing you the id# of the resource. You can use var_dump() to find out more info on the contents of a variable or value.

    But I'm thinking you misunderstood what you should echo out. I meant the query text.
    PHP Code:
    $sql "select from foo...";
    echo 
    $sql;
    $result mysql_query($sql); 
    The purpose is so that you can see how your variables have expanded in the string and what the final result looks like which you're sending to the database as a query.


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
  •