我试图从表单中获取输入并删除表格数据,然后显示更新的表格,但我得到一个空白页面,我不知道这可能是什么问题,任何帮助都会在这里得到关注#39;我的代码:
<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>
答案 0 :(得分:5)
您正在将mysqli
与PDO混合
$mysqli->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
并且您正在通过$db
({em>理论上)应该$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);
... }
答案 1 :(得分:2)
你启用了error_reporting吗?
error_reporting(E_ALL);
ini_set("display_errors","on");
将其添加到PHP文件的头部。
答案 2 :(得分:0)
很棒points made by @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>