Forum Moderators: coopster

Message Too Old, No Replies

Sharing field data or variables between DB tables?

Using 2 DB tables with one common column/field

         

freshrod

6:46 pm on May 9, 2006 (gmt 0)

10+ Year Member



I haven't tried this before, but I understand people do it all the time. I just can't find / figure out how exactly. What I have is a users table and an alumInfo table. (This is for a High School Reunion website.) The first is the basic info; names, address, email, etc. It also assigns a userId with auto-increment. This is working fine.
The other is just text fields where they can tell more in depth about themselves. I want to join the 2 with the userId from the first table. But it isn't working when I try and submit the form to add the alumInfo data.

The alumInfo table is set up like this:
Create Table alumInfo (
userId int (11) NOT NULL,
pic_url varchar (50) default NULL,
living text,
spouse text,
kids text,
etc...
PRIMARY KEY (userId));

The error I get is: "Column count doesn't match value count at row 1"
I'm not sure if the problem is in using the $_SESSION['userId'] or in the query.

Here's what the code the form POST's to looks like:

session_start();
include 'db.php';

// Define post fields into simple variables
$_SESSION['userId'] = $userId;
$pic_url = $_POST['pic_url'];
$living = $_POST['living'];
$spouse = $_POST['spouse'];
$kids = $_POST['kids'];
$job = $_POST['job'];
$life = $_POST['life'];
$bestMHS = $_POST['bestMHS'];
$worstMHS = $_POST['worstMHS'];
$embarrassMHS = $_POST['embarrassMHS'];
$message = $_POST['message'];

$sql = mysql_query("INSERT INTO alumInfo (userId, pic_url, living, spouse,
kids, job, life, bestMHS, worstMHS, embarrassMHS, message)
VALUES('$userId, $pic_url', '$living', '$spouse', '$kids', '$job', '$life','$bestMHS', '$worstMHS', '$embarrassMHS', '$message')")
or die (mysql_error());

Any ideas?

jatar_k

6:51 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



missing single quotes

VALUES('$userId, $pic_url',

should be

VALUES('$userId', '$pic_url',

freshrod

7:13 pm on May 9, 2006 (gmt 0)

10+ Year Member



duh...

OK, I fixed that, but now it says this:

Out of range value adjusted for column 'userId' at row 1

Sorry.

jatar_k

7:15 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what value are you trying to insert there?

you don't really need the quotes around an int, could just do

VALUES($userId, '$pic_url',

freshrod

7:44 pm on May 9, 2006 (gmt 0)

10+ Year Member



Alright, I changed it, but now I get this:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' '', 'Somewhere nice', 'Yes, Renee, 12', 'Lillian 5, Finn 2', '', 'I am cool','y' at line 3

Not sure why the blank '', and what line 3 they mean...

jatar_k

7:51 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that's coming from mysql so I tend never to look at the line

I would start by figuring out why that is blank

but if you wanted it blank and wanted to use a self incrementing id you would need to change your table to use AUTO INCREMENT on your userId column

freshrod

9:14 pm on May 9, 2006 (gmt 0)

10+ Year Member



well... on this table (alumInfo) I want it to use the userId from the other table (users).

jatar_k

9:15 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



then you need to figure out why that var is empty

is this right?

$_SESSION['userId'] = $userId;

or should it be the other way?

$userId = $_SESSION['userId'];

freshrod

9:46 pm on May 9, 2006 (gmt 0)

10+ Year Member



ok... I think I'm getting closer.

I've changed a bit so here's a reprint of the stuff above only changed:

session_start();
include 'db.php';

// Define post fields into simple variables
$userId = $_SESSION['userId'];
$pic_url = $_POST['pic_url'];
$living = $_POST['living'];
$spouse = $_POST['spouse'];
$kids = $_POST['kids'];
$job = $_POST['job'];
$life = $_POST['life'];
$bestMHS = $_POST['bestMHS'];
$worstMHS = $_POST['worstMHS'];
$embarrassMHS = $_POST['embarrassMHS'];
$message = $_POST['message'];

$sql = mysql_query("INSERT INTO alumInfo (userId, pic_url, living, spouse,
kids, job, life, bestMHS, worstMHS, embarrassMHS, message)
VALUES($userId, '$pic_url', '$living', '$spouse', '$kids', '$job', '$life','$bestMHS', '$worstMHS', '$embarrassMHS', '$message')")
or die (mysql_error());

if(!$sql){
echo 'There has been an error entering your Alumni Information. Please try again.';
} else {
$sql = mysql_query
("UPDATE users SET alumInfo='1' WHERE username='".$_SESSION['username']."' AND password='".$_SESSION['password']."'");
}

Now I am getting this error:

Duplicate entry '1' for key 1

Is that the MySQL key? Is that wrong that I used 'userId' as the PRIMARY KEY for both tables?

jatar_k

11:29 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



as long as they are always different than any other key in the same table, it is telling you that value is already in there

freshrod

2:26 pm on May 10, 2006 (gmt 0)

10+ Year Member



Thanks for all the help jatar_k. It's working!