I have a MySQL table called tennis where one of the fields is a primary key called "id".
I also have a form which allows the user to add thereself to that table IF they are not already on that table, i.e. if there user id isn't already present. I am having trouble getting the check to work. I am almost 100% sure that my logic in the way I have done it is correct but I pretty sure I am making a newbie mistake somewhere along the way. Here's the part of code where it all goes on:
PHP Code:
<?php
session_name('YourVisitID');
ini_set('session.use_cookies', 0);
session_start();
?>
<?php
$self = $_SERVER['PHP_SELF'];
$username = $_POST['username'];
$id = $_SESSION['id'];
$leaderboard = $_POST['leaderboard'];
?>
Please select a leaderboard to add
yourself onto.
<?php
echo("$id"); #DEBUG result=2
if((!$leaderboard))
{
$form ="<form action=\"$self\" method=\"post\">";
$form.="<select name=\"leaderboard\" size=\"1\" value=\"$leaderboard\">";
$form.="<option value =\"squash\">Squash</option>";
$form.="<option value =\"badminton\">Badminton</option>";
$form.="<option value =\"tennis\">Tennis</option>";
$form.="<option value =\"snooker\">Snooker</option>";
$form.="<option value =\"golf\">Golf</option></select>";
$form.="<br><br><br><input type=\"submit\" value=\"Submit\"></form>";
echo($form);
}
else
{
require_once('../mysql_connect.php');
$sql = "select id from $leaderboard where id=\"$id\"";
$rs = mysql_query( $sql)
or die( "Could not execute query" );
#get number of rows that match
$num = mysql_numrows( $rs );
if( $num > 0 )
{ echo("You are already on that leaderboard.");
}
else{
echo("You will be added");
echo(" id =$id"); #DEBUG output=nothing
echo(" leaderboard = $leaderboard");#DEBUG output=tennis
echo(" num = $num"); #DEBUG output=nothing
}
}
?>
To make it clearer, the user I am using has an id of 2 and is on the tennis leaderboard. So basically when tennis is selected you would expect the message to be output "You are already on that leaderboard. The #DEBUG are in there to demonstrate what output I am getting. I know the $id is working until the IF statement because it outputs 2, same with the $leaderboard, that outputs "tennis".
I will give more detail if anyone requires. Like I said though I'm sure it is the way I am putting $id in the SQL statement!?
Thanks for any help.
Roger.
I also have a form which allows the user to add thereself to that table IF they are not already on that table, i.e. if there user id isn't already present. I am having trouble getting the check to work. I am almost 100% sure that my logic in the way I have done it is correct but I pretty sure I am making a newbie mistake somewhere along the way. Here's the part of code where it all goes on:
PHP Code:
<?php
session_name('YourVisitID');
ini_set('session.use_cookies', 0);
session_start();
?>
<?php
$self = $_SERVER['PHP_SELF'];
$username = $_POST['username'];
$id = $_SESSION['id'];
$leaderboard = $_POST['leaderboard'];
?>
Please select a leaderboard to add
yourself onto.
<?php
echo("$id"); #DEBUG result=2
if((!$leaderboard))
{
$form ="<form action=\"$self\" method=\"post\">";
$form.="<select name=\"leaderboard\" size=\"1\" value=\"$leaderboard\">";
$form.="<option value =\"squash\">Squash</option>";
$form.="<option value =\"badminton\">Badminton</option>";
$form.="<option value =\"tennis\">Tennis</option>";
$form.="<option value =\"snooker\">Snooker</option>";
$form.="<option value =\"golf\">Golf</option></select>";
$form.="<br><br><br><input type=\"submit\" value=\"Submit\"></form>";
echo($form);
}
else
{
require_once('../mysql_connect.php');
$sql = "select id from $leaderboard where id=\"$id\"";
$rs = mysql_query( $sql)
or die( "Could not execute query" );
#get number of rows that match
$num = mysql_numrows( $rs );
if( $num > 0 )
{ echo("You are already on that leaderboard.");
}
else{
echo("You will be added");
echo(" id =$id"); #DEBUG output=nothing
echo(" leaderboard = $leaderboard");#DEBUG output=tennis
echo(" num = $num"); #DEBUG output=nothing
}
}
?>
To make it clearer, the user I am using has an id of 2 and is on the tennis leaderboard. So basically when tennis is selected you would expect the message to be output "You are already on that leaderboard. The #DEBUG are in there to demonstrate what output I am getting. I know the $id is working until the IF statement because it outputs 2, same with the $leaderboard, that outputs "tennis".
I will give more detail if anyone requires. Like I said though I'm sure it is the way I am putting $id in the SQL statement!?
Thanks for any help.
Roger.