gpt4 book ai didi

php - 无法在一个 php 页面上更新数据库

转载 作者:行者123 更新时间:2023-11-29 22:35:07 25 4
gpt4 key购买 nike

当我运行此代码时,它会显示现有数据库的详细信息,但不会使用放入表单中的条目更新数据库。

<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>

但是,我尝试了一个简单的回显来代替插入查询,它按照我的预期运行...在按下提交按钮之前没有任何操作,然后“输入名称:提交表单后的名称。

<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>

我确信我的问题来自这样一个事实:我必须在一个页面上更新所有内容,但我对这一切都太陌生,不知道该怎么办。

最佳答案

感谢 droopy,我更改了以下内容:

添加了“$qry->$con(sql 查询作为方法的属性)”,并将该部分移至打印项目列表的部分上方,以便在用户提交后更新列表。

关于php - 无法在一个 php 页面上更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29574427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com