[英]PHP MySQL Check if ID exists
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. 我想检查数据库中的ID是否存在,如果是,请插入数据,如果不再尝试。
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. 我输入正确或不正确它进入accepted.html页面,它不会在我的时间表中插入数据。 I want it to check if the ID is true insert the data in the time sheet.
我希望它检查ID是否为真在时间表中插入数据。
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
你有
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查询是使用错误的变量
$$player
应该是$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
: 你传入
$sql
而不是$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. 第三,你的
header
是在错误的地方。 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. 我发现3个错误... 1个$ sql查询,2个,标头加载,3个,放置标题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. ---编辑第四:$ result2应该使用$ sql2作为查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.