我现在遇到了最棘手的问题。为了查看联系人是否已经在数据库中,我检查它。让我看到我回应:“在数据库中”或“不在数据库中”。为了让用户看到我用同样的按摩给他回来了。奇怪的是,echo始终是正确的,但字符串总是相同的“不在数据库中”,即使联系人在数据库中也是如此。例如:我加我的妈妈。我的妈妈在数据库中,所以回声是正确的,并说“在数据库中”。但我收到的字符串总是说“不在数据库中”。
完整代码
<?php
$DB_HostName = "localhost";
$DB_Name = "db";
$DB_User = "user";
$DB_Pass = "pw";
$DB_Table = "contacts";
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die (mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());
$fnumber = mysql_real_escape_string($_GET["fnumber"]);
$result = mysql_query("SELECT * FROM $DB_Table WHERE User = '$fnumber'", $con);
if ($result) {
if (mysql_num_rows($result) > 0) {
echo "This user is already in database";
$number = mysql_real_escape_string($_GET["number"]);
mysql_query("DELETE FROM Echo WHERE Number=('$number')");
mysql_query("INSERT INTO Echo (Number,Answer)
VALUES ('$number','This user is already in database.')");
}
else echo
"This user isn't in the database";
$number = mysql_real_escape_string($_GET["number"]);
mysql_query("DELETE FROM Echo WHERE Number=('$number')");
mysql_query("INSERT INTO Echo (Number,Answer)
VALUES ('$number','this user isn't in the database.')");
}
mysql_close($con);
?>
在您提出之前:为了不占用我的数据库,我总是删除该号码,然后写入该条目。
答案 0 :(得分:2)
我想这是正确的代码:
if ($result) {
if (mysql_num_rows($result) > 0) {
echo "This user is already in database";
$number = mysql_real_escape_string($_GET["number"]);
mysql_query("DELETE FROM Echo WHERE Number=('$number')");
mysql_query("INSERT INTO Echo (Number,Answer)
VALUES ('$number','This user is already in database.')");
} else {
echo "This user isn't in the database";
$number = mysql_real_escape_string($_GET["number"]);
mysql_query("DELETE FROM Echo WHERE Number=('$number')");
mysql_query("INSERT INTO Echo (Number,Answer)
VALUES ('$number','this user isn't in the database.')");
}
}
答案 1 :(得分:1)
在
之前需要第二次关闭}else echo "This user isn't in the database";
答案 2 :(得分:1)
else echo
"This user isn't in the database";
$number = mysql_real_escape_string($_GET["number"]);
mysql_query("DELETE FROM Echo WHERE Number=('$number')");
mysql_query("INSERT INTO Echo (Number,Answer)
VALUES ('$number','this user isn't in the database.')");
由于else
之后没有括号,因此将始终执行较低的三行。第二个mysql_query
将始终失败,因为'未转义。
答案 3 :(得分:1)
您没有将整个else
分支括在括号中。那样,`else echo'之后的所有内容这个用户不在数据库“;”中执行时会覆盖您的上述数据。
else
分支应如下所示:
else
{ // <-- NEW
echo "This user isn't in the database";
$number = mysql_real_escape_string($_GET["number"]);
mysql_query("DELETE FROM Echo WHERE Number=('$number')");
mysql_query("INSERT INTO Echo (Number,Answer)
VALUES ('$number','this user isn't in the database.')");
} // <-- NEW
}