致命错误:未捕获的异常' PDOException' with message' SQLSTATE [42000]:语法错误或访问冲突PHP& PDO

时间:2016-01-21 18:15:52

标签: php pdo

上周工作正常,但今天我收到此错误:

  

致命错误:未捕获的异常' PDOException' with message' SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;查看与您的MariaDB服务器版本对应的手册,以获得正确的语法,以便在附近使用' Mees')'在第1行'在/www/data08/users/i/itsiim.planet.ee/htdocs/progemine/system/lisa.php:47堆栈跟踪:#0 /www/data08/users/i/itsiim.planet.ee/htdocs/progemine /system/lisa.php(47):PDOStatement->执行(数组)#1 {main}抛出在/www/data08/users/i/itsiim.planet.ee/htdocs/progemine/system/lisa.php上第47行

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`

1 个答案:

答案 0 :(得分:3)

您的查询中存在语法错误,exta单引号:

 $sql = "INSERT INTO kliendid (nimi,email,mobiil,sugu) values(?, ?, ?, ?')";
                                                                        ^

将其更改为:

 $sql = "INSERT INTO kliendid (nimi,email,mobiil,sugu) values(?, ?, ?, ?)";