简体   繁体   中英

MYSQL ERROR when submitting form

Hi I'm having some problems when I'm trying to submit a form of mine, everything seems to look fine on my end but im not quite sure why it's still not working any help would be appreciated.

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

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

Resolved: was as simple as adding ticks to release

release是一个MySQL关键字 ,应该放在反引号中:`release`

try to use backticks in table name if it is keyword release

  $sql = "INSERT INTO `release` (id, producers, company, title)
          VALUES ('null', '$producers', '$company', '$title')";

It seems to me that id should not be assigned the string value 'null' . Typically id columns are auto increment, in which case you should simply omit the column:

$sql = "INSERT INTO `release` (producers, company, title)
VALUES ('".addslashes($producers)."', '".addslashes($company)."', '".addslashes($title)."'";

The addslashes is to protect again SQL injection . You should also sanitize your inputs:

$producers = strval($_POST['producers']);
$company = strval($_POST['company']);
$title = strval($_POST['title']);

Also it is better to write your database in the following format database name -> db_name eg db_release , and table name -> tb_name eg tb_release

so as to avoid keywords errors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM