I did something like that but it doesn't work.
I want to check if the ID exisit in the data base if yes INSERT the data if not try again.
I use this code to check but doesn't seems to work.
I enter a correct or not correct it goes to the accepted.html page and it doesn't insert the data in my time sheet. I want it to check if the ID is true insert the data in the time sheet.
if ($player == null || $witness == null) {
echo "Do not use the same player and witness or maybe you forgot to add it...";
echo '<META HTTP-EQUIV="Refresh" Content="2; URL=index.php">';
} else {
$sql = "SELECT (`id`) FROM player WHERE id={$player} ";
$result = mysql_query($sql);
if ($result == true) {
$sql2 = "INSERT INTO `members` (`player`, `hours`, `minutes`, `witness`, `type`,`date`,`comments`) VALUES ('$player', '$hours', '$minutes', '$witness', '$type', '$date','$comments')";
$result2 = mysql_query($sql2);
if ($result2)
{
header('Location: accepted.html');
}
else {
echo mysql_error();
}
}else{
header('Location: contactAdmin.html');
}
}
$sql = "SELECT (`id`) FROM $tbl_name WHERE id=$$player ";
should be
$sql = "SELECT `id` FROM $tbl_name WHERE id= $player ";
you had id=$$player
There are a few things wrong in your code.
One thing is the first sql query is using the wrong variable $$player
should be $player
:
$sql = "SELECT `id` FROM $tbl_name WHERE id= $player ";
Secondly, your insert query is never executed because you pass in the wrong variable. You passed in $sql
instead of $sql2
:
$sql2 = "INSERT INTO `members` (`player`, `hours`, `minutes`, `witness`, `type`,`date`,`comments`) VALUES ('$player', '$hours', '$minutes', '$witness', '$type', '$date','$comments')";
$result2 = mysql_query($sql2);
Thirdly, your header
is in the wrong place. It should be place after the opening {
:
if ($result2) {
header('Location: accepted.html');
} else {
echo mysql_error();
}
I found 3 mistakes... 1 in $sql query, 2, header loading, 3, placement header vs mysql_error.
if ($player == null || $witness == null) {
echo "Do not use the same player and witness or maybe you forgot to add it...";
echo '<META HTTP-EQUIV="Refresh" Content="2; URL=index.php">';
} else {
$sql = "SELECT (`id`) FROM $tbl_name WHERE id='{$player}' ";
$result = mysql_query($sql);
if (mysql_num_rows($result) <= 0) {
$sql2 = "INSERT INTO `members` (`player`, `hours`, `minutes`, `witness`, `type`,`date`,`comments`) VALUES ('$player', '$hours', '$minutes', '$witness', '$type', '$date','$comments')";
$result2 = mysql_query($sql2);
if ($result2)
{
header('Location: accepted.html');
}
else {
echo mysql_error();
}
}else{
header('Location: contactAdmin.html');
}
}
--- EDIT fourth: $result2 should use $sql2 as query.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.