我试图从表单获取输入并删除表数据,然后显示更新后的表,但是我得到了一个空白页,我不知道这可能是什么问题,任何帮助都将适用于此,这是我的代码:
<html>
<body>
<?php
$mysqli = new mysqli("xxxxx", "xxxxxx", "xxxxx", "xxxxxx");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//----------------------------------------------------------------------------------//
$name = $_POST['Car_ID'];
if ($stmt = $mysqli->prepare("delete from CARS where name=?")) {
// Bind the variable to the parameter as a string.
$stmt->bind_param("s", $name);
// Execute the statement.
$stmt->execute();
echo "Deleted data successfully\n";
// Close the prepared statement.
$mysqli->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $db->prepare("SELECT id, Doors, TRANSMISSION, Fuel_type, Engine_Size, Total FROM CARS");
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
$doors=$row["Doors"];
$engine=$row["Engine_Size"];
$total=$row["Total"];
$trans=$row["Transmission"];
}
?>
<table>
<tr>
<td><?php echo $doors; ?></td>
<td><?php echo $engine; ?></td>
<td><?php echo $total; ?></td>
<td><?php echo $trans; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
您正在将mysqli
与PDO混合使用
$mysqli->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
并且您正在传递$db
( 理论上应为$mysqli
$result = $db->prepare("SELECT id, Doors, TRANSMISSION, Fuel_type, Engine_Size, Total, DATE_INITIATED, AGE, PARTNO, QTY, DESCRIPTION, LOC FROM CARS");
理论上应该在哪里
$result = $mysqli->prepare("SELECT id, Doors, TRANSMISSION, Fuel_type, Engine_Size, Total, DATE_INITIATED, AGE, PARTNO, QTY, DESCRIPTION, LOC FROM CARS");
因为您的数据库连接是:
$mysqli = new mysqli("xxxxx", "xxxxxx", "xxxxx", "xxxxxx");
但是,您的数据库连接应该类似于:
$mysql_hostname = 'xxx';
$mysql_username = 'xxx';
$mysql_password = 'xxx';
$mysql_dbname = 'xxx';
$db = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
您不能这样做:
$stmt->bind_param("s", $name);
您使用PDO还记得吗? ( 还是您? )请选择,是mysqli
还是PDO?
您最有可能想要做:
// $stmt = $db->prepare("delete from CARS where name=:value");
if ($stmt = $db->prepare("delete from CARS where name=:value")) {
// Bind variables to your statement
$stmt->bindParam(':value', $name);
... }
您启用了error_reporting吗?
error_reporting(E_ALL);
ini_set("display_errors","on");
将此添加到您的PHP文件的头部。
@fred的要点 ,当您最终决定要使用的数据库扩展名时,您仍然必须修复输出表数据的方式,应该将表标签置于循环之外,并且应该进行打印循环内的表行,请注意是否有多余的花括号。
这样的事情会工作:
<html>
<body>
<table>
<?php
//Fetch your data
//.....
//....
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
echo"<tr>
<td>$row['Doors']</td>
<td>$row['Engine_Size']</td>
<td>$row['Total']</td>
<td>$row['Transmission']</td>
</tr>";
}
?>
</table>
</body>
</html>