【问题标题】:Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;致命错误:未捕获的 PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;
【发布时间】:2022-01-06 15:12:54
【问题描述】:

我正在尝试创建一个表单以在数据库中插入值,但它不起作用。 事实上,我曾经使用过一个现在已经死掉的虚拟机。当我切换到 Xammp 时,我的程序不再工作了。

  $titre = $_POST["titre"];
  $categorie = $_POST["categorie"];
  $portion = $_POST["portion"];
  $heure_cuiss = $_POST["heure_cuiss"];
  $minute_cuiss = $_POST["minute_cuiss"];
  $heure_prepa = $_POST["heure_prepa"];
  $minute_prepa = $_POST["minute_prepa"];
  $heure_rep = $_POST["heure_rep"];
  $minute_rep = $_POST["minute_rep"];
  $cuiss = $_POST["cuiss"];
  $cost = $_POST["cost"];
  $dif = $_POST["dif"];
  $histoire = $_POST["histoire"];
  $region = $_POST["region"];

  

  $temps = intval($heure_cuiss) + intval($minute_cuiss)/60 + intval($heure_prepa) + intval($minute_prepa)/60 + intval($heure_rep) + intval($minute_rep)/60;

  $query = $bdd -> prepare('INSERT INTO recette (titre, categorie, portion, heure_cuiss, minute_cuiss, heure_prepa, minute_prepa, heure_rep, minute_rep , cuiss, cost, dif, histoire, region, temps) 
                            VALUES(:titre, :categorie, :portion, :heure_cuiss, :minute_cuiss, :heure_prepa, :minute_prepa, :heure_rep, :minute_rep, :cuiss, :cost, :dif, :histoire, :region, :temps)');
  $query -> execute(array('titre'=>$titre, 'categorie'=>$categorie, 'portion'=>$portion, 'heure_cuiss'=>$heure_cuiss, 'minute_cuiss'=>$minute_cuiss, 'heure_prepa'=>$heure_prepa, 'minute_prepa'=>$minute_prepa, 'heure_rep'=>$heure_rep, 'minute_rep'=>$minute_rep, 'cuiss'=>$cuiss, 'cost'=>$cost, 'dif'=>$dif, 'histoire'=>$histoire, 'region'=>$region, 'temps'=>intval($temps)));

我收到此错误

致命错误:未捕获的 PDOException:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在 C:\xampp\htdocs\ptut\upload\ 的第 1 行的“portion, heure_cuiss, minute_cuiss, heure_prepa, minute_prepa, heure_rep, min...”附近使用正确的语法back-index.php:46 堆栈跟踪:#0 C:\xampp\htdocs\ptut\upload\back-index.php(46): PDOStatement->execute(Array) #1 {main} 在 C:\xampp 中抛出\htdocs\ptut\upload\back-index.php 在第 46 行

我试图重写我的数据库,用'?'写我的插入。但没有任何效果。 我已经在这个问题上工作了 5 个小时。我真的需要你的帮助 ! 谢谢,托马斯

【问题讨论】:

标签: php sql pdo insert mariadb


【解决方案1】:

确保您的密码为空,如下所示: $bdd = new PDO('mysql:host=localhost;dbname=yourDataBase', 'root', '');

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • 我试过了,但它不起作用。奇怪的是,我可以做一个选择请求。表示与数据库的连接正常。
猜你喜欢
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-02
  • 2019-08-08
  • 2020-02-09
相关资源
最近更新 更多