0

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

6
  • 1
    add your HTML form Commented May 31, 2016 at 4:14
  • 1
    what error showing ? Commented May 31, 2016 at 4:14
  • 1
    Also add mysql error which you see Commented May 31, 2016 at 4:14
  • did you using AUTO INCREMENT ID ? Commented May 31, 2016 at 4:19
  • no need to change your table name just use backticks in your table name like this $sql = "INSERT INTO release (id, producers, company, title) VALUES ('null', '$producers', '$company', '$title')"; Commented May 31, 2016 at 4:22

4 Answers 4

2

release is a MySQL keyword, and should be enclosed in backticks: `release`

Sign up to request clarification or add additional context in comments.

1 Comment

it should be in comment area
1

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')";

Comments

0

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

2 Comments

I personally prefer simple undecorated names, to reduce clutter in my code.
yaah it is not so bad, but i advise you to start with db_ or tb_
0

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']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.