PHP 代码看起来不错,但没有更新

标签 php mysql insert content-management-system

我目前正在创建一个cms,除了add.php页面之外一切都很好。

此页面的代码是这样的:

<?php

session_start();

include_once('../include/connection.php');

if (isset($_SESSION['logged_in'])){
      if (isset($_POST['title'], $_POST['content'])) {
             $title = $_POST['title'];
             $content = nl2br($_POST['content']);
             $image = $_POST['Image URL'];
             $link = $_POST['Link'];
             $price = $_POST['Price'];

if (empty($title) or empty($content)) {
             $error = 'All Fields Are Required!';
}else{
 $query = $pdo->prepare('INSERT INTO `apps`(`app_id`, `app_title`, `app_content`, `app_img`, `app_link`, `app_price`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6])');
$query->execute(array(
':title' => $title,
':content' => $content,
':image' => $image,
':link' => $link,
':price' => $price
));

     $query->execute();
}if($result){
        echo("<br>Input data is successful");
    } else{
        echo("<br>Input data failed");
    }

}
          ?>

<html>
<head>
<title>testing</title>
<link rel="stylesheet" href="../style.css" />
</head>

<body>
<div class="container">
<a href="index.php" id="logo">CMS</a>

<br />


<h4>Add Article</h4>

<?php if (isset($error)) { ?>
     <small style="color:#aa0000;"><?php echo $error; ?></small><br /><br />
<?php } ?>


<form name = "myform" action="add.php" method="post" autocomplete="off">


<input type="text" name="title" placeholder="Title" /><br /><br />
<textarea rows="15" cols="50" placeholder="Content" name="content"></textarea><br /><br />
<input type="text" name="Image URL" placeholder="Image URL" /><br /><br />
<input type="text" name="Link" placeholder="Link" /><br /><br />
<input type="text" name="Price" placeholder="Price" /><br /><br />
 <input type="submit" name="submit" value="Add Article" />

</form>

</div>
</body>
</html>


<?php
}else{
       header('location: index.php');
}
error_reporting(E_ALL);

?>

我的问题是。我的代码在错误日志中没有显示任何错误,人们告诉我这很好。但它没有添加到数据库中。有没有一种方法可以分解每一部分代码并找出发生了什么?

或者有没有办法显示可能出现的错误?我的错误报告已使用 E_ALL | 打开E_STRICT 还是什么都没有。

请帮忙

谢谢。

最佳答案

您需要将 PDO 查询更改为

 $query = $pdo->prepare('INSERT INTO `apps`(`app_id`, `app_title`, `app_content`, `app_img`, `app_link`, `app_price`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6])');

变成这样

$query = $pdo->prepare('INSERT INTO `apps`(`app_title`, `app_content`, `app_img`, `app_link`, `app_price`) VALUES (:title,:content,:img,:link,:price)');

您应该查看 PDO::prepare 方法如何使用占位符。此外,如果您的 app_id 是自动递增字段。您不需要将其包含在插入查询中。

关于PHP 代码看起来不错,但没有更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17617706/

相关文章:

php - 如何在php标签后列出所有带有空格的文件

PHP 更改数组值以创建新数组

php - mysqli prepare and insert statements using variables插入值和变量语法错误

javascript - 保存Web应用程序的动态图结构

java - com.mysql.jdbc.Driver 的 ClassNotFoundException - 不使用 IDE

java - 插入已排序的双向链表

php - 使用 WordPress Super Cache 从缓存中排除动态值

php - 显示 mysql 表中的数据

SQL Server : insert next available int

mysql更新抛出错误代码: 1062.重复条目