提交表单时MYSQL错误

时间:2016-05-31 04:11:46

标签: php mysql forms

我在尝试提交我的某种形式时遇到了一些问题,一切看起来似乎都很好但我不太确定为什么它还没有任何帮助将不胜感激。

的config.php

<?php
$servername = "localhost";
$username = "release";
$password = "";
$dbname = "release";

// Create connection
$con = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


?> 

submit.php

<?php
include('config.php');

$producers = $_POST['producers'];
$company = $_POST['company'];
$title = $_POST['title'];

if(!$producers or !$company or !$title) {
    echo 'Please make sure to fill out all required feilds.';
} else {
// Insert into DB
$sql = "INSERT INTO release (id, producers, company, title)
VALUES ('null', '$producers', '$company', '$title')";
}

if ($con->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $con->error;
}$con->close();
?>

的index.php

<html>
<head>
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<style>
input[type="text"] {
    height: 30px;
    }
</style>
<title>RRP &raquo; Welcome!</title>
</head>

<body>
<div style="width: 1080px; margin-top: 50px;">
<h3>Welcome!</h3>
<h4>You can edit the basic release form info below. <br /> Once done hit the "Submit" button to carry on to the new form!</h4>

<div class="container">
      <form class="contact-us form-horizontal" action="submit.php" method="post">      
        <div class="control-group">
            <label class="control-label">Producers</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-user"></i></span>
                    <input type="text" class="input-xlarge" name="producers" placeholder="Producers(seperate by commas)">
                </div>
            </div>
        </div>

            <div class="control-group">
            <label class="control-label">Production Company</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-globe"></i></span>
                    <input type="text" class="input-xlarge" name="company" placeholder="Rolling Ridges Productions">
                </div>
            </div>
        </div>

            <div class="control-group">
            <label class="control-label">Title</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-pencil"></i></span>
                    <input type="text" class="input-xlarge" name="title" placeholder="Desperate Measures">
                </div>
            </div>
        </div>

         <div class="control-group">
          <div class="controls">
            <button type="submit" class="btn btn-primary">Submit</button>
            <button type="button" class="btn">Cancel</button>
          </div>    
        </div>
      </form>
</body>
</html>

错误

Error: INSERT INTO release (id, producers, company, title) VALUES ('null', 'lol', 'lol', 'lol')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'release (id, producers, company, title) VALUES ('null', 'lol', 'lol', 'lol')' at line 1

已解决:就像向release添加刻度一样简单

4 个答案:

答案 0 :(得分:2)

release是MySQL keyword,应该用反引号括起来:`release`

答案 1 :(得分:1)

如果是关键字 release

,请尝试在表名中使用反引号
  $sql = "INSERT INTO `release` (id, producers, company, title)
          VALUES ('null', '$producers', '$company', '$title')";

答案 2 :(得分:0)

在我看来,不应为 // Create Keystore containing servers certificate KeyStore keyStore = KeyStore.getInstance("BKS"); // Different versions of Android use different BKSs // Need to export both versions using Portacle (1.9 worked for me) int bks_version; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { bks_version = R.raw.keystore; //The BKS file } else { bks_version = R.raw.keystorev1; //The BKS (v-1) file } keyStore.load(activity.getResources().openRawResource(bks_version), "password"); 分配字符串值id。通常'null'列是自动递增的,在这种情况下,您应该省略列:

id

$sql = "INSERT INTO `release` (producers, company, title) VALUES ('".addslashes($producers)."', '".addslashes($company)."', '".addslashes($title)."'"; 将再次保护SQL injection。您还应该清理您的输入:

addslashes

答案 3 :(得分:0)

最好以下列格式编写数据库 数据库名称 - &gt; db_name 例如 db_release ,以及 表名 - &gt; tb_name 例如 tb_release

以避免关键字错误