Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing primary key from one table accross to another table?

Status
Not open for further replies.

rogwilco

Programmer
Mar 21, 2005
21
GB
Hello people.

I am struggling in getting the user ID (PK) from my user table into my leaderboard_1 table as a FK. I am confused on how to pass this information accross. Below is my code. At the moment a new leaderboard_1 record is created but it gives each record a value of 0 for id!?

?>
<?php

$self = $_SERVER['PHP_SELF'];
$country = $_POST['country'];
$username = $_POST['username'];

if( (!$country) or (!$username))
{
$form ="Please enter all the following fields to add yourself onto a leaderboard.";
$form.="<form action=\"$self\"";
$form.=" method=\"post\">Please enter your username: ";
$form.="<input type=\"text\" name=\"username\"";
$form.=" value=\"$username\"><br>Your country: ";
$form.="<input type=\"text\" name=\"country\"";
$form.=" value=\"$country\"><br>";
$form.="<input type=\"submit\" value=\"Submit\">";
$form.="</form>";
echo($form);
}
else
{
require_once('../mysql_connect.php');

#create the query
$sql = "select id from users where username=\"$username\"";

$uid = mysql_query( $sql)
or die( "Could not execute query" );

if( $uid != 0 )
{
$sql = "insert into leaderboard_1 (id,country,registration_date) values (\"$uid\",\"$country\", NOW())";
#execute the query
$result = @mysql_query($sql)
or die("Username or password incorrect");

if($result)
{ echo("You have been added to the leaderboard.");
}
}
else
{
header( "Location:$referer" ); exit();
}
}
?>
<?php
include ('./footer.inc'); // Include the HTML footer.
?>

Thanks for any help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top