0

我目前正在创建一个 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 仍然没有。

请帮忙

谢谢你。

4

2 回答 2

1

您需要将您的 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是自动增量字段。您无需将其包含在插入查询中。

于 2013-07-12T14:59:58.243 回答
0

我不确定这是如何工作的,因为占位符没有使用正确的符号并且没有正确命名。

您的查询应如下所示:

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

此外,您似乎缺少该:app_id参数。

于 2013-07-12T14:59:37.567 回答