PHP没有错误但数据库中没有条目?

时间:2016-10-10 16:21:12

标签: php mysql

我有一篇关于信息的帖子。 php页面。 我们的想法是将表单内容发送到我的数据库。现在提交php返回该文件已被添加但是当我进入数据库时​​,我看不到新条目。尽管几年前我在学校有这件事,但我似乎无法将其付诸实践,如果能帮助我,我会非常感激。

这是表格:

<div>
    <form action="info.php" method="POST">
    <p> file name + extension </p>  <input type="text" name="source"/>
    <p> File title on hover </p> <input type="text" name="title"/>
    <p> Alt </p> <input type="text" name="alt"/>
    <p> Tag print or web </p> <input type="text" name="tag"/>
    <p>Hover </p> <input type="text" name="cover"/>
    <p>Content </p> <input type="text" name"content"/>
    <p> </p><input type="submit"/>
    </form>
</diV>

这里是php文件:

echo $_POST['title'];

$title = $_POST['title'];
echo $title;

$bdd = new PDO('mysql:host=localhost;dbname=BennyWater;charset=utf8', 'root', 'root');


$req = $bdd->prepare('INSERT INTO Gal
                            (Title, Alt, Tag, Cover, Hover, 
                             Content, Name, Source) 
                      VALUES(:Title, :Alt, :Tag, :Cover, :Hover, 
                             :Content, :Name, :Source)');
$req->execute(array(
  'Title' => $_POST['title'],
  'Alt' => $_POST['alt'],
  'Tag' => $_POST['tag'],
  'Cover' => $_POST['title'],
  'Hover' => $_POST['content'],
  'Content' => $_POST['content'],
  'Name' => $_POST['title'],
  'Source' => $_POST['source']
  ));

echo 'Ajouté';

谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

由于短语“发生了事情”,因此您需要检查stuff happening

实际上,您甚至没有检查连接是否已成功建立到数据库。

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 1);

echo $_POST['title'];

$title = $_POST['title'];
echo $title;
try {
    $bdd = new PDO('mysql:host=localhost;dbname=BennyWater;charset=utf8', 'root', 'root');
    $bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // personally I would add this as well, 
    // so a SQL syntax error will be reported from the prepare
    // rather than waiting till the execute is run to report it
    $bdd->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);


    $req = $bdd->prepare('INSERT INTO Gal
                            (Title, Alt, Tag, Cover, Hover, 
                             Content, Name, Source) 
                      VALUES(:Title, :Alt, :Tag, :Cover, :Hover, 
                             :Content, :Name, :Source)');
    $req->execute(array(
      'Title' => $_POST['title'],
      'Alt' => $_POST['alt'],
      'Tag' => $_POST['tag'],
      'Cover' => $_POST['title'],
      'Hover' => $_POST['content'],
      'Content' => $_POST['content'],
      'Name' => $_POST['title'],
      'Source' => $_POST['source']
      ));

    echo 'Ajouté';

} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

这不会修复你的错误,但它应该至少报告它们,以便你知道它们发生的地点和时间。

  

PDO手册http://php.net/manual/fr/class.pdo.php