2013-03-04 60 views
-1

我尝试将表单的数据存储到数据库中。我无法弄清楚为什么这段代码不工作......没有任何反应。 谢谢你的帮助。 这里是我的代码:PHP:使用准备工作,不工作

<?php 
// Connexion à la base de données 
try 
{ 
$bdd = new PDO('mysql:host=localhost;dbname=mydb', 'root', ''); 
} 
catch(Exception $e) 
{ 
die('Erreur : '.$e->getMessage()); 
} 
// Insertion du message à l'aide d'une requête préparée 

$auteur="Henri"; 

$req = $bdd->prepare('INSERT INTO factures (projet, fournisseur, montant, ref, in_out, commentaires, auteur, input_date, maturity) VALUES(:projet, :fournisseur, :montant, :ref, :in_out, :commentaires, :auteur, CURDATE(), :maturity'); 
$req->execute(array(
        'projet'=>$_POST['projet'], 
        'fournisseur'=>$_POST['fournisseur'], 
        'montant'=>$_POST['montant'], 
        'ref'=>$_POST['ref'], 
        'in_out'=>$_POST['in_out'], 
        'commentaires'=>$_POST['commentaires'], 
        'auteur'=>$auteur, 
        'maturity'=>$_POST['maturity'] 
        )); 

header('Location: index.php'); 
?> 

正确的代码: - '谟'=> $ _ POST [ '谟']必须是 ':谟'=> $ _ POST [ '谟'], - 有在VALUES SQL查询结束时是缺失的)。

回答

3

正如您从Documentation中看到的那样,您置入准备好的SQL语句中的值必须与您在execute()上传递的值完全匹配。

IE:

'projet'=>$_POST['projet'],应该':projet'=>$_POST['projet'],

之所以代码什么也没做,是因为的$_POST['projet']的值被映射到“谟”。因为'项目'没有出现在你的SQL语句中,所以它没有被映射。然而,在这种情况下,你在你的SQL语句中缺少一个“)”。

但是,使用PDO而不是my_sql函数的赞誉。

+0

我做了你的建议,但数据库仍然是空的... – Henri 2013-03-04 22:05:49

+0

没有任何反应。 – Henri 2013-03-04 22:09:34

+0

优秀!试着围绕你的字段名称用' – christopher 2013-03-04 22:18:07