使用PDO插入数据库

时间:2015-10-09 10:45:34

标签: php mysql pdo

请我尝试插入数据库,但我没有把它弄好。我收到此错误

  

致命错误:带有消息'SQLSTATE [42000]的未捕获异常'PDOException':语法错误或访问冲突:1064 SQL语法中有错误;查看与您的MySQL服务器版本相对应的手册,以便在第28行的C:\ wamp \ www \ livingwordmedia \ admin \ upload-script.php第1行''''''附近使用正确的语法

请问我做错了什么。

这是我的代码:

    <?php require 'header.php'; require 'upload-script.php'; ?>

<body>
    <div id="wrapper">
    <!-- Navigation -->
    <?php require 'nav.php'; ?>
    <div id="page-wrapper">
      <div class="container-fluid">
      <!-- Page Heading -->
      <div class="row"> 
        <div class="col-lg-12">
          <h1 class="page-header">Upload <small>audio/video/pdf</small></h1>      
        </div>
      </div>

      <div class="row">

        <form action="" method="POST">
        <div class="col-lg-6">
            <div class="form-group">
              <label>Title</label>
              <input type="text" class="form-control" name="title" placeholder="Enter Title">
            </div>

            <div class="form-group">
              <label>Description</label>
              <input type="text" class="form-control" name="description" placeholder="Short Description....">
            </div>
            <div class="form-group">
              <label>Download Link</label>
              <input type="text" class="form-control" name="download_link" placeholder="Download link">
            </div>
          </div>  

          <div class="col-lg-6">
            <div class="form-group">
              <label>Category</label>
              <select name="category" class="form-control">
                <option value="category 1">Category 1</option>
                <option value="category 2">Category 2</option>
                <option value="category 3">Category 3</option>
                <option value="category 4">Category 4</option>
              </select>  
            </div>

            <div class="form-group">
              <label>Format</label>
              <select name="format" class="form-control">
                <option value="mp3">Mp3</option>
                <option value="mp4">Mp4</option>
                <option value="pdf">PDF</option>
              </select>  
            </div>

          <button type="submit" name="submit" class="btn btn-default">Submit</button>
          </div>
        </form>
        </div>
      </div>



      </div>
    </div>

<?php require 'footer.php'; ?>

这是我的上传脚本:

    <?php

require 'functions.php';

if(isset($_POST['title']) && isset($_POST['description']) && isset($_POST['download_link']) && isset($_POST['category']) && isset($_POST['format'])){
    $title = trim($_POST['title']);
    $description = trim($_POST['description']);
    $download_link = trim($_POST['download_link']);
    $category = ($_POST['category']);
    $format = ($_POST['format']);



    if(!empty($title) && !empty($description) && !empty($download_link) && !empty($category) && !empty($format)){

            $query = $conn->prepare("SELECT title FROM libraries WHERE title = :title");
            $query->bindParam(':title', $title);
            $query->execute();



            if($query->rowCount() > 0){
                echo 'Title already exist';
            }
        else{
        $query = "INSERT INTO libraries (id, title, description, download_link, category, format) VALUES ('','".mysql_real_escape_string($title)."','".mysql_real_escape_string($description)."','".mysql_real_escape_string($download_link)."','".mysql_real_escape_string($category)."', ,'".mysql_real_escape_string($format)."')";

        if($conn->query($query)){
            echo "upload completed";
        }else{
            echo "Upload not completed";
        }
    }
}
}
?>

请别人帮忙。

1 个答案:

答案 0 :(得分:0)

&#34;类别&#34;之间还有一个逗号。和&#34;格式&#34; SQL字符串的VALUES部分中的字段。