我不明白为什么我无法从我的数据库中获取任何数据。 它与$ userRow一起工作正常,但是当我使用$ SQL时它将无法正常工作。 我很抱歉,如果它很容易修复,但我找不到我的错误
// check if the form has been submitted and display the results
if (isset($_POST['studentnum'])) {
$conn = mysqli_connect($DBhost, $DBuser, $DBpass, $DBname);
if (!$conn) {
die('Could not connect: ' . mysqli_connect_error());
}
// escape the post data to prevent injection attacks
$studentnum = mysqli_real_escape_string($conn, $_POST['studentnum']);
$sql = "SELECT * FROM `afventer` WHERE `user_id` LIKE '%$studentnum%'";
$result=mysqli_query($conn, $sql);
// check if the query returned a result
if (!$result) {
echo 'Der var ikke nogen resultater på din søgning';
} else {
// result to output the table
echo "Du har aftalt at køre";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo " ";
echo $row['uname'];
echo " ";
echo "hjem";
}
}
当我回显SQL时它显示SELECT * FROM afventer WHERE user_id LIKE '%3%'
,所以当我在数据库中的SQL中运行该字符串时,它会找到该用户,但它仍然不会显示在网站上
它主要是数据库正在做它想做的事情,但是我要求显示在页面上的数据没有显示
答案 0 :(得分:0)
我通过将代码重写到此
来修复它/ check if the form has been submitted and display the results
if (isset($_POST['studentnum'])) {
$conn = mysqli_connect($DBhost, $DBuser, $DBpass, $DBname);
if (!$conn) {
die('Could not connect: ' . mysqli_connect_error());
}
// escape the post data to prevent injection attacks
$studentnum = mysqli_real_escape_string($conn, $_POST['studentnum']);
$sql = "SELECT * FROM `afventer` WHERE `user_id` LIKE '%$studentnum%'";
$result=mysqli_query($conn, $sql);
// check if the query returned a result
if (!$result) {
echo 'Der var ikke nogen resultater på din søgning';
} else {
// result to output the table
echo "Du har aftalt at køre";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo " ";
echo $row['username'];
echo " ";
echo "hjem";
}
$conn = mysqli_connect($DBhost, $DBuser, $DBpass, $DBname);
if (!$conn) {
die('Could not connect: ' . mysqli_connect_error());
}
// escape the post data to prevent injection attacks
$studentnum = mysqli_real_escape_string($conn, $_POST['studentnum']);
$sql = "SELECT * FROM `afventer` WHERE `user_id` LIKE '%$studentnum%'";
$result=mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$parent = $userRow['user_id'];
$parent_name = $userRow ['uname'];
$student_name = $row ['username'];
$mellem = $row['adresse'];
$slut = $userRow['adresse'];
$parent = $DBcon->real_escape_string($parent);
$parent_name = $DBcon->real_escape_string($parent_name);
$student_name = $DBcon->real_escape_string($student_name);
$mellem = $DBcon->real_escape_string($mellem);
$slut = $DBcon->real_escape_string($slut);
if ($count==0) {
$sql = "INSERT INTO aftalt(student_id, student_name, parent_id, parent_name, mellem_adresse, slut_adresse) VALUES('$studentnum', '$student_name', '$parent', '$parent_name', '$mellem', '$slut')";
$sql = "DELETE FROM afventer WHERE username = '$student_name'";
echo "$sql";
if ($DBcon->query($sql)) {
$msg = "";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
}
}