【问题标题】:PDO Insert doesn't work, but other yesPDO 插入不起作用,但其他是
【发布时间】:2015-03-13 21:21:06
【问题描述】:

我的 INSERT PDO 语句有问题:

在同一个 PHP 页面中,我有这个第一个 INSERT :

//Creation d'un entrainement
if (isset($_POST) && isset($_POST['intitule_ent']) && isset($_POST['start_ent'])) {
    if(get_magic_quotes_gpc()){
        $_POST['intitule_ent'] = stripslashes(trim($_POST['intitule_ent']));
    }

    $intitule = $_POST['intitule_ent'];

    if($i = $bdd->prepare("INSERT INTO entrainements (intitule_entrainement,id_membre) VALUES (:intitule_entrainement,:id_membre)")){
        $i->bindParam(':intitule_entrainement', $intitule);
        $i->bindParam(':id_membre', $user_id);
        $i->execute();
        echo '<span class="label label-success">Entrainement créé avec succès!</span>';
        $entrainement_commence = true;
    }
    else {
        echo '<span class="label label-danger">Erreur lors de la création de l\'entraînement.</span>';
    }
}

这个 INSERT 效果很好!

但问题是这个 INSERT 不起作用:

//Verifications d'usage du formulaire et securite
if (isset($_POST['exm']) && isset($_POST['series']) && isset($_POST['repetitions']) && isset($_POST['valider'])) {

    //On securise les chaines de caracteres pour eviter les injections
    if(get_magic_quotes_gpc()){
        $_POST['repetitions'] = stripslashes(trim($_POST['repetitions']));
        $_POST['poids'] = stripslashes(trim($_POST['poids']));
        $_POST['temps'] = stripslashes(trim($_POST['temps']));
        $_POST['force'] = stripslashes(trim($_POST['force']));
        $_POST['vitesse'] = stripslashes(trim($_POST['vitesse']));
    }

    $id_exom = $_POST['exm'];
    $s = $_POST['series'];
    $r = $_POST['repetitions'];
    $p = $_POST['poids'];
    $t = $_POST['temps'];
    $f = $_POST['force'];
    $v = $_POST['vitesse'];


    if($i = $bdd->prepare("
        INSERT INTO exercices (id_entrainement,id_exercice_muscle,membre,series,reps,poids,temps,force_exo,vitesse)
        VALUES (:id_entrainement,:id_exercice_muscle,:membre,:series,:reps,:poids,:temps,:force_exo,:vitesse)")
        ){

        $i->bindParam(':id_entrainement', $id_ent_en_cours);
    $i->bindParam(':id_exercice_muscle', $id_exom);
    $i->bindParam(':membre', $user_id);
    $i->bindParam(':series', $s);
    $i->bindParam(':reps', $r);
    $i->bindParam(':poids', $p);
    $i->bindParam(':temps', $t);
    $i->bindParam(':force_exo', $f);
    $i->bindParam(':vitesse', $v);
    $i->execute();
    $envoi_succes = '<span class="label label-success">Enregistrement réalisé avec succès !</span>';
}
else {
    $envoi_erreur = '<span class="label label-warning">Erreur lors de l\'enregistrement de l\'exercice.</span>';
}

根据我的表架构,我真的不知道为什么它不起作用:

这是我的数据库:

我在这里给你我的表格,但我认为它不是很有用......:

<form name="envoi_exo" action="send.php" method="post">
    <input type="text" name="exm" disabled value="">
    <input type="text" name="series" disabled value="">
    <input type="text" name="repetitions" disabled value="">
    <input type="text" name="poids" disabled value="">
    <input type="text" name="temps" disabled value="">
    <input type="text" name="force" disabled value="">
    <input type="text" name="vitesse" disabled value="">
    <button type="submit" name="valider" class="btn btn-success">Enregistrer l\'exercice</button>
</form>

最后,我在 StackOverflow 上找到了一些关于 PDO 中的 INSERT 问题的主题,我尝试使用 try catch(不推荐)找到解决方案并打开我的 MYSQL 日志文件。以下是最新日志:

2015-03-13 21:36:01 6916 [Warning] InnoDB: Cannot open table phpmyadmin/pma_usergroups from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for how you can resolve the problem. 2015-03-13 21:52:57 6916 [Warning] InnoDB: Cannot open table phpmyadmin/pma_usergroups from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for how you can resolve the problem. 2015-03-13 21:52:57 6916 [Warning] InnoDB: Cannot open table phpmyadmin/pma_usergroups from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for how you can resolve the problem.

我真的需要帮助来纠正这个问题,因为我一次又一次地搜索解决方案,但我没有发现任何真正有用的...

当我单击按钮发送表单时,会出现注释并且页面只是重新加载(我认为这是操作属性),并且我没有在数据库中看到插入。

【问题讨论】:

  • 大部分表单输入都有disabled。禁用的输入不会在表单提交时发送,所以if (isset($_POST['exm']) &amp;&amp; isset($_POST['series']) &amp;&amp; isset($_POST['repetitions']) &amp;&amp; isset($_POST['valider'])) 将是错误的。

标签: php mysql pdo insert


【解决方案1】:

您禁用了 Web 表单中的大部分输入字段,当您单击提交按钮时,您的脚本将尝试在数据库中插入空值。 但是您的某些字段设置为NOT NULL(例如id_entrainement)并且您的插入语句不会被执行。

开头的 isset() 检查也应该失败:

if (isset($_POST['exm']) && isset($_POST['series']) && isset($_POST['repetitions']) && isset($_POST['valider'])) {

【讨论】:

  • 感谢您的回答bladerz,我想我开始明白了。但我想要的是隐藏这些输入(我将它们显示为调试)。但是在第一次插入时,我没有在插入中给出id_entrainement,因为它是自动递增的,通常我可以在插入中不提及它。你建议什么而不是我的 isset() 检查?我没有提到的是,这些字段是自动填充 jQuery 的,它在 中取值
  • 我认为 isset 是要走的路。问题出在你的表格上。从我在您的表架构中可以看到,只有 id_exercice 是自动递增的。对于其余的 NOT NULL 字段,您需要传递一个值。要隐藏表单中的字段,您只需使用 。只需确保“your_value”不为空即可。
  • 好的,我做了更改。我将 NULL 值放入未使用的输入中,但它不再起作用。我不知道该怎么做...我将$i-&gt;bindParam(....,....); 更改为$i-&gt;bindValue(....,....);,但它并没有改变任何东西。我什至重新安装了 xampp,但它没有任何改变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
  • 2013-07-10
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多