【发布时间】:2015-04-11 05:23:41
【问题描述】:
当我运行此代码时,它会显示现有数据库中的详细信息,但不会使用放入表单中的条目来更新数据库。
<html>
<body>
<center>
<h2>Insert Product Details</h2>
<hr />
</center>
<?php
include('db_conn.php'); //db connection
if($result = $con->query("SELECT * FROM dbName")){
if($result->num_rows){
$rows = $result->num_rows;
if(isset($_POST['updated'])){
$qry = "INSERT INTO dbName (ID, Name, Price, Description)
VALUES (trim('$_POST[inID]'), trim('$_POST[inName]'), trim('$_POST[inPrice]'), trim('$_POST[inDescription]') )";
}
echo '<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
</tr>
';
while($row = $result->fetch_object()) {
echo '<tr>',
'<td>', $row->ID, '</td>',
'<td>', $row->Name, '</td>',
'<td>', $row->Price, '</td>',
'<td>', $row->Description, '</td> </tr>
';
}
echo '
<form method="POST" >
</table>
<hr />
<table>
<tr>
<td>ID:</td>
<td>
<input type = "text"
name = "inID"
value = " "
size = "3">
</td>
</tr>
<tr>
<td>Name:</td>
<td>
<input type = "text"
name = "inName"
value = " "
size = "30">
</td>
</tr>
<tr>
<td>Price:</td>
<td>
<input type = "text"
name = "inPrice"
value = " "
size = "7">
</td>
</tr>
<tr>
<td>Description:</td>
<td>
<input type = "text"
name = "inDescription"
value = " "
size = "60">
</td>
</tr>
</table>
<input type = "submit" value = "insert" name="updated">
</form>
';
}
}
/* close connection */
$con->close();
?>
</body>
但是,我尝试了一个简单的 echo 来代替插入查询,它按我的预期运行...在按下提交按钮之前没有任何内容,然后“输入的名称:提交表单后的名称。” p>
<html>
<body>
<center>
<h2>Insert Product Details</h2>
<hr />
</center>
<?php
include('db_conn.php'); //db connection
if($result = $con->query("SELECT * FROM dbName")){
if($result->num_rows){
$rows = $result->num_rows;
if(isset($_POST['updated'])){
echo "Name entered: " , $_POST[inName];
}
echo '<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
</tr>
';
while($row = $result->fetch_object()) {
echo '<tr>',
'<td>', $row->ID, '</td>',
'<td>', $row->Name, '</td>',
'<td>', $row->Price, '</td>',
'<td>', $row->Description, '</td> </tr>
';
}
echo '
<form method="POST" >
</table>
<hr />
<table>
<tr>
<td>ID:</td>
<td>
<input type = "text"
name = "inID"
value = " "
size = "3">
</td>
</tr>
<tr>
<td>Name:</td>
<td>
<input type = "text"
name = "inName"
value = " "
size = "30">
</td>
</tr>
<tr>
<td>Price:</td>
<td>
<input type = "text"
name = "inPrice"
value = " "
size = "7">
</td>
</tr>
<tr>
<td>Description:</td>
<td>
<input type = "text"
name = "inDescription"
value = " "
size = "60">
</td>
</tr>
</table>
<input type = "submit" value = "insert" name="updated">
</form>
';
}
}
/* close connection */
$con->close();
?>
</body>
我还在另一个表单处理 php 文件中测试了替换的查询,该文件从另一个带有表单的 html 页面接收 $POST[],并且它可以很好地更新数据库。
<html>
<body>
<?php
$con = new mysqli("localhost", "me", "mypassword","dbName");
// check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n" , mysqli_connect_error());
exit();
}
// insert values
$qry = "INSERT INTO dbName (ID, Name, Price, Description)
VALUES (trim('$_POST[inID]'), trim('$_POST[inName]'), trim('$_POST[inPrice]'), trim('$_POST[inDescription]') )";
// print out
if ($con->query($qry) === TRUE) {
echo "It Worked"
} else {
echo "Error: " . $qry . "<br>" . $con->error;
}
// close connection
$con->close();
?>
</body>
我确信我的问题来自于我必须在一个页面上更新所有内容,但我对这一切都太陌生了,不知道该怎么做。
【问题讨论】:
-
更新查询在哪里
-
第一个 sn-p 未命中
$con->query($qry)声明 -
对不起,我缺乏知识,这不是查询:$qry = "INSERT INTO dbName (ID, Name, Price, Description) VALUES (trim('$_POST[inID]') , 修剪('$_POST[inName]'), 修剪('$_POST[inPrice]'), 修剪('$_POST[inDescription]') )";
-
@MarkD 是插入查询,而不是更新查询。
-
对不起,我以为它们都被称为查询。对这一切都非常陌生。