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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Help wiht Mysql_insert_ID. its nots not work

    Hi everyone.

    i am new to PHP and designing my first database. i realize that this question has been asked many time and i have studied everything, but nothing seems to help

    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$r );
        
        
                    
                
                
            
            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
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,332
    Thanks
    60
    Thanked 526 Times in 513 Posts
    Blog Entries
    4
    Two issues:

    You're using both mysql and mysqli functions.

    Secondly:
    PHP Code:
    $r mysqli_query ($dbc$q); // Run the query.

    //This should be a mysql resource - $dbc not $r
    $id mysql_insert_id$r ); 
    Some interesting links on php.net:
    Mysql_insert_id

    Mysqli->insert_id
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

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