【发布时间】:2019-02-25 20:07:45
【问题描述】:
我正在从 mysql 表中获取数据并以 HTML 填充表。在每个表行中,我都有删除按钮,该按钮调用 remove.php 并从 mysql 表中删除该行并再次返回到 admin.php。 问题是当我单击删除按钮时,它正在执行 php 脚本从数据库中的表中删除行。然后我再次导航到 admin.php 导航 wamp 服务器时出现 500 内部服务器错误。 我想要的是执行删除查询并再次返回到 admin.php。所以 admin.php 给了我更新的数据。我明白出了什么问题。
这是我记录的错误: [2019 年 2 月 25 日星期一 15:19:30.182141] [http:error] [pid 1852:tid 1232] [client ::1:57588] AH02429:响应标头名称“位置”包含无效字符,正在中止请求,引用者:@987654321 @
这是我的 admin.php
<?php
// if(isset($_SESSION["loggedin"]) && ($_SESSION["loggedin"] == true) && $_SESSION["usertype"] == a){
// }
// else{
// header("location: login.php");
// exit;
// }
echo "<script type='text/javascript'>alert('here');</script>";
require_once "config.php";
$sql = "SELECT userid,username FROM user_login_table";
$stmt = $mysqli->prepare($sql);
if($stmt->execute())
{
echo "executed";
}
else{
echo "not able to execute";
}
$stmt->store_result();
echo $stmt->num_rows;
$stmt->bind_result($id,$name);
?>
<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<h2>HTML Table</h2>
<table>
<tr>
<th>ID</th>
<th>Username</th>
<th>Remove</th>
</tr>
<?php
$rowid=0;
while ($stmt->fetch()) {
$rowid += 1;
echo "<tr>
<td>".
$id."
</td>
<td>".
$name."
</td>
<td>
<form action='remove.php' method='post'>
<input type='hidden' name ='row_id' value = ".$id." >
<input type='submit' value='Remove'>
</form>
</td>
</tr>";
} ?>
</table>
</body>
</html>
这是remove.php
<?php
require_once "config.php" ;
echo "string";
$sql = "DELETE FROM user_login_table where userid=".$_POST['row_id']."";
$stmt = $mysqli->prepare($sql);
if($stmt->execute())
{
header("location : admin.php");
}
else
{
echo "alert('Failed to Remove.Something went wrong')";
echo "failed";
}
//header("location : admin.php");
?>
config.php
<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASSWORD','');
define('DB_NAME','exhibition_database');
$mysqli = new mysqli(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME);
if($mysqli === false )
{
die("Error! Couldn't connect. ". $mysqli->connect_error );
}
?>
【问题讨论】:
-
警告:使用
mysqli时,您应该使用parameterized queries 和bind_param将任何数据添加到您的查询中。 请勿使用字符串插值或连接来完成此操作,因为您创建了严重的SQL injection bug。 切勿将$_POST、$_GET或任何类型的数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。 -
请尝试改掉用
=== false之类的不必要的东西弄乱代码的习惯。许多函数被设计为返回逻辑上判断为真或假的值,因此这是多余的,在某些情况下可能会导致错误。 -
提示:“500 内部服务器错误”意味着您需要检查您的服务器日志以更具体地了解发生了什么。 PHP 通常会非常详细地记录确切的问题,直到出现问题的代码行。
-
@tadman 这是日志 [Mon Feb 25 15:19:30.182141 2019] [http:error] [pid 1852:tid 1232] [client ::1:57588] AH02429: 响应标头名称'location' 包含无效字符,正在中止请求,引用者:localhost/project_exhibition/admin.php
-
标题应该是
"Location:",冒号前没有空格。不确定这是否重要,但以正确的方式表达不会有坏处。
标签: php html mysql mysqli wamp