Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am very close to having my database set up as a relational database. I currently can insert data into both of my tables however when i try to call the most recent id number so i know in the foreign key to relate them i cannot figure out the way to call the most recent entry in the database.
This is what i have.


PHP
if($inputError != true) 
                { 
                    //insert query 
                    $sqlInsertQuery = "INSERT INTO users (address, email, firstName, lastName, password, phone) 
                    VALUES ('$address', '$email', '$fName', '$lName', '$salt', '$phone')"; 
                     
                    $sqlReturn = "SELECT MAX(userID) FROM users"; 
                                                         
                     
                     
                     
                    if ($mysqli->query($sqlInsertQuery) === TRUE) 
                    { 
                        echo 'New employee record added with ID: '  . $mysqli->insert_id; 
                        echo 'Your login credentials were successfully created.';   
                    } 
                    else  
                    { 
                      echo "ERROR: Could not execute query: $sqlInsertQuery. " . $mysqli->error; 
                      echo 'Unable to create your credentials.';  
                    } 
                    //this is trying to get the most recent user id to insert it in my second table 
                    $my_id_array=mysqli_fetch_assoc($sqlInsertQuery); 
                    $my_id=$my_id_array['userid']; 

                    //$id = (int)$mysqli->query($sqlReturn); 
                     
                    $sql2 = "INSERT INTO user2 (userid, phone) VALUES ('$my_id', '$phone')"; 
                     
                    if ($mysqli->query($sql2) === TRUE) 
                    { 
                        echo 'Relation database call';  
                    }  
Posted

1 solution

what is the definition of userID ? I'd be worried about this

$sqlReturn = "SELECT MAX(userID) FROM users"; 


not knowing how you've set it up - you already have

$mysqli->insert_id;

for example - isn't that a better choice ?
 
Share this answer
 
v2
Comments
Garth J Lancaster 17-Oct-14 0:23am    
Thanks @PIEBALDconsult for fixing the broken tag - I didn't notice it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900