致命错误:未捕获的异常“PDOException”,带有消息“SQLSTATE [42000]:语法错误或访问冲突 PHP 和 PDO
Posted
技术标签:
【中文标题】致命错误:未捕获的异常“PDOException”,带有消息“SQLSTATE [42000]:语法错误或访问冲突 PHP 和 PDO【英文标题】:Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation PHP & PDO 【发布时间】:2016-01-21 18:15:51 【问题描述】:上周一切正常,但今天我收到此错误:
致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在 /www/data08/users/i/itsiim.planet.ee/htdocs/progemine/system/ 中的第 1 行的“Mees”)附近使用正确的语法lisa.php:47 堆栈跟踪:#0 /www/data08/users/i/itsiim.planet.ee/htdocs/progemine/system/lisa.php(47): PDOStatement->execute(Array) #1 main在第 47 行扔进 /www/data08/users/i/itsiim.planet.ee/htdocs/progemine/system/lisa.php
这里是 PHP 代码
<?php
require 'conf/db.php';
if ( !empty($_POST))
// keep track validation errors
$nimiError = null;
$emailError = null;
$mobiilError = null;
$suguError = null;
// keep track post values
$nimi = $_POST['nimi'];
$email = $_POST['email'];
$mobiil = $_POST['mobiil'];
$sugu = $_POST['sugu'];
// validate input
$valid = true;
if (empty($nimi))
$nimiError = 'Palun sisesta nimi';
$valid = false;
if (empty($email))
$emailError = 'Palun sisesta e-mail';
$valid = false;
else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) )
$emailError = 'Palun sisesta korrektne e-mail';
$valid = false;
if (empty($mobiil))
$mobiilError = 'Palun sisesta mobiili number';
$valid = false;
if (empty($sugu))
$suguError = 'Palun vali sugu';
$valid = false;
// insert data
if ($valid)
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO kliendid (nimi,email,mobiil,sugu) values(?, ?, ?, ?')";
$q = $pdo->prepare($sql);
$q->execute(array($nimi,$email,$mobiil,$sugu));
Database::disconnect();
header("Location: index.php");
?>
<!DOCTYPE html>
<html lang="et">
<head>
<meta charset="utf-8">
<title>Klientide andmed by Siim Aarmaa IS-13</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="col-md-6 col-md-offset-3">
<div class="row">
<h3>Lisa uus klient</h3>
</div>
<form class="form-horizontal" action="lisa.php" method="post">
<div class="form-group <?php echo !empty($nimiError)?'error':'';?>">
<label class="col-sm-2 control-label">Nimi</label>
<div class="controls">
<input name="nimi" type="text" placeholder="Nimi" value="<?php echo !empty($nimi)?$nimi:'';?>">
<?php if (!empty($nimiError)): ?>
<span class="help-block"><?php echo $nimiError;?></span>
<?php endif; ?>
</div>
</div>
<div class="form-group <?php echo !empty($emailError)?'error':'';?>">
<label class="col-sm-2 control-label">E-mail</label>
<div class="controls">
<input name="email" type="text" placeholder="E-mail" value="<?php echo !empty($email)?$email:'';?>">
<?php if (!empty($emailError)): ?>
<span class="help-block"><?php echo $emailError;?></span>
<?php endif;?>
</div>
</div>
<div class="form-group <?php echo !empty($mobiilError)?'error':'';?>">
<label class="col-sm-2 control-label">Mobiili number</label>
<div class="controls">
<input name="mobiil" type="text" placeholder="Mobiili number" value="<?php echo !empty($mobiil)?$mobiil:'';?>">
<?php if (!empty($mobiilError)): ?>
<span class="help-block"><?php echo $mobiilError;?></span>
<?php endif;?>
</div>
</div>
<div class="form-group <?php echo !empty($suguError)?'error':'';?>">
<label class="col-sm-2 control-label">Sugu</label>
<div class="controls">
<input name="sugu" type="radio" value="<?php echo !empty($mees)?$mees:'Mees';?>">Mees
<input name="sugu" type="radio" value="<?php echo !empty($naine)?$naine:'Naine';?>">Naine
<?php if (!empty($suguError)): ?>
<span class="help-block"><?php echo $suguError;?></span>
<?php endif;?>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Lisa klient</button>
<a class="btn btn-default" href="index.php">Tagasi</a>
</div>
</form>
</div>
</div> <!-- /container -->
</body>
</html>`enter code here`
【问题讨论】:
...values(?, ?, ?, ?')
看起来你有一个不应该存在的单引号。
【参考方案1】:
你有一个语法错误,你的查询中有一个额外的单引号:
$sql = "INSERT INTO kliendid (nimi,email,mobiil,sugu) values(?, ?, ?, ?')";
^
改成:
$sql = "INSERT INTO kliendid (nimi,email,mobiil,sugu) values(?, ?, ?, ?)";
【讨论】:
以上是关于致命错误:未捕获的异常“PDOException”,带有消息“SQLSTATE [42000]:语法错误或访问冲突 PHP 和 PDO的主要内容,如果未能解决你的问题,请参考以下文章
致命错误:第 19 行的 C:\xampp\htdocs\register.php 中抛出未捕获的异常“PDOException”
带有消息“SQLSTATE [42000]”的未捕获异常“PDOException”:语法错误或访问冲突:
未捕获的异常“PDOException”消息“无效的数据源名称”
致命错误:未捕获的 PDOException:SQLSTATE[42000]:? [复制]
致命错误:未捕获的 PDOException:SQLSTATE[42000] 语法错误或访问冲突
PHP 致命错误:未捕获的 PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误