[英]PHP MySQL Check if ID exists
我做了類似的事情,但它不起作用。
我想檢查數據庫中的ID是否存在,如果是,請插入數據,如果不再嘗試。
我使用此代碼進行檢查,但似乎不起作用。
我輸入正確或不正確它進入accepted.html頁面,它不會在我的時間表中插入數據。 我希望它檢查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 ";
應該
$sql = "SELECT `id` FROM $tbl_name WHERE id= $player ";
你有id=$$player
您的代碼中存在一些問題。
有一點是第一個sql查詢是使用錯誤的變量$$player
應該是$player
:
$sql = "SELECT `id` FROM $tbl_name WHERE id= $player ";
其次,您的插入查詢永遠不會執行,因為您傳入了錯誤的變量。 你傳入$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);
第三,你的header
是在錯誤的地方。 它應該在開幕后放置{
:
if ($result2) {
header('Location: accepted.html');
} else {
echo 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');
}
}
---編輯第四:$ result2應該使用$ sql2作為查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.