<?php
$conn = mysqli_connect ("localhost", "B00657633", "Jpr3EjPw")
or die ("could not connect: " . mysqli_error($conn));
print "successful connection<br>";
mysqli_select_db($conn, 'B00657633') or die ('db will not open');
header('Location: sql5_2.php');
$query = "UPDATE patient3 SET AGE=68 WHERE POST_CODE='bt667ed'";
$result = mysqli_query($conn, $query) or die ("Query is invalid");
$rownum = mysqli_num_rows($conn)
$col = mysqli_num_fields($conn);
echo "<table border='2'><tr><th>POST_CODE</th>";
for($i=0; $i<$rownum; $i++){
$rows = mysqli_fetch_row($result);
echo "<tr>";
for($j=0; $j<$col; $j++){
echo "<td>" . $rows[$j] . "</td>";
}
echo "</tr>";
}
echo "</table>";
$rownum = mysqli_affected_rows($conn);
echo $rownum . " Records have been updated.";
mysqli_close($conn);
?>
这是我网页的第一部分,第二部分如下:
<?php
$conn = mysqli_connect ("localhost", "B00657633", "Jpr3EjPw")
or die ("could not connect: " . mysqli_error($conn));
print "successful connection<br>";
mysqli_select_db($conn, 'B00657633') or die ('db will not open');
$query="SELECT * FROM patient3";
$result = mysqli_query($conn, $query) or die ("Query is invalid");
$num = mysqli_num_rows ($result);
$col = mysqli_num_fields ($result);
echo "<table border='2'><tr><th>PATNUM</th><th>PAT_FORENAME</th><th>PAT_SURNAME</th><th>STREET_ADDRESS</th><th>TOWN</th><th>POST_CODE</th><th>AGE</th>";
for($j=0; $j<$num; $j++){
$rows = mysqli_fetch_row($result);
echo "<tr>";
for($i=0; $i<$col; $i++){
echo "<td>" . $rows[$i] . "</td>";
}
echo "</tr>";
}
echo "</table>";
echo " Records have been updated.";
mysqli_close($conn);
?>
无论我尝试什么,它都不允许我更新我的SQL中的任何信息。真的不确定我哪里出错了,请帮帮我。第二部分是显示我的表格。连接到服务器很好,但是一旦我点击刷新
,表格中没有任何变化答案 0 :(得分:2)
编辑:现在我确实找到了测试/查看代码的时间,我注意到了一些事情。
这是更新文件。
<?php
$conn = mysqli_connect ("localhost", "B00657633", "Jpr3EjPw")
or die ("could not connect: " . mysqli_error($conn));
print "successful connection<br>";
mysqli_select_db($conn, 'B00657633') or die ('db will not open');
$query = "UPDATE patient3 SET AGE=68 WHERE POST_CODE='bt667ed'";
$result = mysqli_query($conn, $query) or die ("Query is invalid");
$rownum = mysqli_affected_rows($conn);
echo $rownum . " Records have been1 updated.";
header('Location: sql5_2.php');
?>
这是sql5_2.php文件。
<?php
$conn = mysqli_connect ("localhost", "B00657633", "Jpr3EjPw")
or die ("could not connect: " . mysqli_error($conn));
print "successful connection<br>";
mysqli_select_db($conn, 'B00657633') or die ('db will not open');
$query="SELECT * FROM patient3";
$result = mysqli_query($conn, $query) or die ("Query is invalid");
$num = mysqli_num_rows ($result);
$col = mysqli_num_fields ($result);
echo "<table border='2'><tr><th>PATNUM</th><th>PAT_FORENAME</th><th>PAT_SURNAME</th><th>STREET_ADDRESS</th><th>TOWN</th><th>POST_CODE</th><th>AGE</th>";
for($j=0; $j<$num; $j++){
$rows = mysqli_fetch_row($result);
echo "<tr>";
for($i=0; $i<$col; $i++){
echo "<td>" . $rows[$i] . "</td>";
}
echo "</tr>";
}
echo "</table>";
echo " Records have been updated.";
mysqli_close($conn);
?>
在你的代码中,你做了一些事情。首先,您尝试通过连接作为num_rows / num_fields方法的结果集。这些期望实际包含数据的返回值,例如来自select查询。更新查询将返回true或false,以及它影响的行数。所以即使在更新它以获取结果变量之后,它仍然试图通过布尔值传递给方法。
您会注意到我将其更改为
mysqli_affected_rows()
将检查执行查询的mysqli_connect对象受影响的行数。从那里,我们可以使用该变量来输出受影响的行数。
此外,您试图从更新查询中输出字段的值,该查询不会返回字段。在我的脑海中,您必须运行具有相同参数的选择查询以获取将要更改的数据集,然后更新该集。这样,您可以使用第一个选择查询中的数据集来输出要更改的行。
请注意,使用header('Location: ')
您将重定向用户。因此,第一个文件中的任何输出都不会被看到,因为它们被发送到sql5_2.php。