Jump to content

Recommended Posts

Hello,

 

I'm having trouble formulating the Sql query I require for inserting additional information about a user into my website. The the user first registers by filling out a form and the form information is inserted into the User table. The user is then redirected to a page called UserProfile where they enter some more information and this should be inserted into the UserProfile table. I'm having trouble figuring out how to write the Insert query in a way that will link the UserProfile information with the User stored in the User table.

 

Here is my current query:

  mysql_insert_id();      
$query = "INSERT into UserProfile (Age,Dob,Height,Weight) values ('".$Age."','".$DOB."', '".$Height."', '".$Weight."')";     
WHERE User.UserID = UserProfile.UserID";      
$verify = mysql_query($query); 

This query results in no information being stored in the UserProfile table.The structure of the two tables look like this:

 

User

-----------

UserID (PK)(Auto_increment)

Firstname

Surname

Address

Email

 

UserProfile

-------------

ProfileID(PK) (Auto_Increment)

UserID

Age

Dob

Height

Weight

 

Link to comment
https://forums.phpfreaks.com/topic/54101-solved-php-and-mysql-problem/
Share on other sites

Hi,

 

You're structuring your query incorrectly. MySQL has to open all the tables that it uses in the where clause (i.e., they have to be named in a "from" statment), so you can't run joins on inserts.

 

First you have to make a userID column in your userProfile table. This will be the connection between the two tables. Then you have to store the mysql_insert_id() in a variable (and if you're going across pages, you might as well use a $_SESSION['userId'] variable) and enter that in with the rest of the information in your userProfile table:

 

<?php
start_session();

$insertUser = @mysql_query("insert into User set uname = '$userName', upass = '$userPass'");
$userID = @mysql_insert_id();
$_SESSION['userID'] = $userID;

.....

$query = "INSERT into UserProfile (Age,Dob,Height,Weight,userID) values ('$Age','$DOB', '$Height', '$Weight',".$_SESSION['userID'].")";    
$verify = mysql_query($query);

if ($verify) { echo 'Success!'; }
else { echo 'Failed!'; }
?>

 

You join them later by querying as follows:

 

<?php
$userInfo = @mysql_query("select up.age, up.dob, up.height, up.weight, u.uname, u.upass from userProfile up, user u where u.userID = up.userID");
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.