gpt4 book ai didi

用于将数据添加到数据库的 PHP 脚本不起作用

转载 作者:搜寻专家 更新时间:2023-10-30 20:27:51 25 4
gpt4 key购买 nike

我有一个 php 脚本,用于将数据输入到表 listdata 中的数据库 list

这是我的 View 代码。

<form action="../add.php" method="post">

<label for="title">Title:</label>
<br>
<input id="title" type="text" name="title"/>

<br>

<label for="body">Body:</label>
<br>
<textarea name="body" cols="30" rows="10"></textarea>

<br>

<input id="sb" type="submit" values="Add">
</form>

这是我的 Controller (没有框架)

<?php
include 'config.php';
include 'views/add.view.php';

if ($_SERVER['REQUEST_METHOD'] === 'post') {
$conn = new PDO('mysql:host=localhost;dbname=list', $config['username'], $config['password']);
$title = $_POST['title'];
$body = $_POST['body'];
if (empty($title) or empty($body)) {
$status = "<h3>Enter Values</h3>";
echo $status;
} else {
$stmt = $conn->prepare('INSERT INTO listdata(title,body) VALUES(:title,:body)');
$stmt->bindParam(':title',$title);
$stmt->bindParam(':body',$body);
$stmt->execute();
$status = "<h3 message='id'>Added<h3>";
echo $status;
}



}



?>

当我在浏览器中运行它时,在发布值之前或之后,没有错误,但是

$status
变量未被回显,数据库未更新。

最佳答案

尝试将您的 Controller 更改为:

<?php
include 'config.php';
include 'views/add.view.php';

if ($_POST) {
$conn = new PDO('mysql:host=localhost;dbname=list', $config['username'], $config['password']);
$title = $_POST['title'];
$body = $_POST['body'];
if (empty($title) || empty($body))
{
$status = "<h3>Enter Values</h3>";
}
else
{
$stmt = $conn->prepare('INSERT INTO listdata(title, body) VALUES(:title, :body)');
$stmt->execute(array(':title'=>$title, ':body'=>$body));
$status = "<h3 message='id'>Added<h3>";
}
echo $status;
}

?>

关于用于将数据添加到数据库的 PHP 脚本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22447139/

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