SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Help wiht Mysql_insert_ID. its nots not work

    Hi everyone.

    sorry to disturb everyone again.

    i have been stuck on this problem for 2 days now .

    i have created a database and i am able to populate the database. however the mysql_insert_id does not seem to collect the ID . i am at a complete loss where i am going wrong.

    any help will be appreciated.

    i enclose below the SQL for the database and the PHP script for my insertion.


    USE global;
    CREATE TABLE users(
    user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
    email VARCHAR (60) NOT NULL,
    pass CHAR(40) NOT NULL,
    reg_date DATETIME NOT NULL,
    first_name VARCHAR(20) NOT NULL,
    surname VARCHAR(20) NOT NULL,
    nationality TINYTEXT NOT NULL ,
    PRIMARY KEY (user_id));


    PHP Code:
        
            
    // Make the query:
            //$q = "INSERT INTO users (first_name, last_name, email, pass, reg_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW() )";        
            
            
    $q "INSERT INTO users
            ( email, pass,  first_name, last_name , country_loc, reg_date) VALUES ('
    $EA',  SHA1('$PW'), '$FN', '$LN', '$CL', NOW() )";

            
            
    $r mysqli_query ($dbc$q); // Run the query.
        
                    
        
          
    $id mysql_insert_id( ); 


            
                
                
            
            if (
    $r) { // If it ran OK.
            

            
                
                
                // Print a message:
                
    echo "$id"
                echo 
    '<h1>Thank you!</h1>
            <p>You are now registered. In Chapter 11 you will actually be able to log in!</p><p><br /></p>'
    ;    
            
            } else { 
    // If it did not run OK.
                
                // Public message:
                
    echo '<h1>System Error</h1>
                <p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>'

                
                
    // Debugging message:
                
    echo '<p>' mysqli_error($dbc) . '<br /><br />Query: ' $q '</p>';
                            
            } 
    // End of if ($r) IF. 

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Notice the issue if I simply give you the two opposing lines?

    PHP Code:
    $r mysqli_query ($dbc$q); // Run the query.      
    $id mysql_insert_id( ); 
    You're using MySQLi to interact with the database, but you're trying to use a MySQL function to get the inserted ID. MySQLi and MySQL are different libraries.

    Solution?
    PHP Code:
    $id mysqli_insert_id($dbc); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow. that was amazing.
    what a silly mistake on my part.

    thank you so much for your help. i just tried it and it work.

    i am eternally grateful

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You're using mysql_insert_id, but you're using the mysqli set of functions. Change it to mysqli_insert_id, and you should be good to go.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ah, beaten to the post by Jake. Cheers Jake.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heh, and i beat all of you on codingforums.com :P
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Traitor :P
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heh
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi Tangoforce

    thank for the comment regarding
    if (isset($_POST['submit'])) ?

    yes, i have used it. i am still writing the script now. the form is 3 pages long so its taking me a long time to draft the script. but i am getting there.

    thank you everyone for all your help and support. its such a scary period (writing the first PHP script)

    regards

    Andreea

  10. #10
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by andreea115 View Post
    thank for the comment regarding
    if (isset($_POST['submit'])) ?
    No problem. You should test for another field in your form instead though because IE has a bug with this. If your cursor is in a text box and you hit the enter key then IE will not send the button. If you click it with the mouse it will. Daft eh?.. Only a silly thing but it can make a lot of difference.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  11. #11
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I prefer to test for a form submission with:
    PHP Code:
    if('POST' === $_SERVER['REQUEST_METHOD']){
      

    Of course, this isn't always suitable depending on what you're looking for; but should suffice for most of the time.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •