2

I'm trying to insert new record to SQL database using PHP from a HTML form.

I made a form using Post method

<form name="CreatNewMCQ" action="create.php" method="POST">

with a button to submit

<button type="submit" form="CreateNewMCQ">CREATE</button>

what I want to do is when I press the button, it will call create.php which is

<?php
$servername = "localhost";
$user = "admin";
$pass = "admin";
$dbname = "examples";

// Create connection
$conn = new mysqli($servername, $user, $pass, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$id = $_POST['id'];
$name = $_POST['name'];
$year = $_POST['year'];

$sql = "INSERT INTO cars (id, name, year)
VALUES ($id, $name, $year)";

if ($conn->query($sql) === TRUE) {
    echo "Tạo mới thành công";
} else {
    echo "Lỗi: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

then insert data from form to SQL database (id, name, year from the form). I got some errors in SQL syntax. What mistake did I make?

8
  • If you got any error message, You can share the error messages with your question. Commented Nov 15, 2018 at 5:42
  • Start by posting the error you get, this will help us to help you. I will assume your error is at your $sql line, you might want to have some single quotes there... $sql = "INSERT INTO cars (id, name, year) VALUES ($id, '$name', '$year')"; if you have spaces and other some other characters it will break your query... Further on you should really learn about prepared statements since you are already using mysqli, to avoid MySQL Injections. Commented Nov 15, 2018 at 5:42
  • Some error: Notice: Undefined index: id in C:\xampp\htdocs\create.php on line 14 Notice: Undefined index: name in C:\xampp\htdocs\create.php on line 15 Notice: Undefined index: year in C:\xampp\htdocs\create.php on line 16 Commented Nov 15, 2018 at 5:44
  • @TrầnVũAnhDũng have you included those fields in your form? It looks to me they are either empty or non-existent from your form submit. Commented Nov 15, 2018 at 5:45
  • Yes I have. I used this: <input type="text" name="name" class="form-control" id="Name" placeholder="Enter brand"> Commented Nov 15, 2018 at 5:52

3 Answers 3

3

Make sure all post values are getting correctly. You should make a condition check before inserting the data, For ex:

$id = isset($_POST['id']) ? $_POST['id'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$year = isset($_POST['year']) ? $_POST['year'] : '';

if($id && $name && $year){
$sql = "INSERT INTO cars (id, name, year)
VALUES ($id, '$name', '$year')";
}else{
  return "required fields are missing";
}

NB: Please post your html if possible.

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

Comments

2

try this:

<?php
/* Attempt MySQL server connection.*/

$servername = "localhost";
$user = "admin";
$pass = "admin";
$dbname = "examples";

$link = mysqli_connect($servername, $user, $pass, $dbname);

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Attempt insert query execution
$id = $_POST['id'];
$name = $_POST['name'];
$year = $_POST['year'];

$sql = "INSERT INTO cars (id, name, year)
VALUES ($id, '$name', '$year')";

if(mysqli_query($link, $sql)){
    echo "Records inserted successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>

11 Comments

thank you for replying, but i still got the same errors as I did. "Undefined index: id in C:\xampp\htdocs\create.php on line 18"
can you post your form inputs?
your id may be autoincrement, so it is not needed to get from form and insert
My form : <input type="text" name="id" class="form-control" id="id" placeholder="Input ID">
|
-2
HTML Form :
<html>
<form name="test" method="post">
Enter name:<input type="text" name="name"/> <br>
Enter year :<input type="text" name="year"/><br>
<input type="submit" name="save" value="save" />
</form>
</html>
php code  :

<?php
$conn=mysql_connect("localhost","root","passward");
$select_db=mysql_select_db("Atul",$conn);
if($conn)
{
    echo "connected";
}
else
{
    echo "Please try again";
}

if(isset($_POST['save']))
{
    $name=$_POST['name'];
    $year=$_POST['year'];
    $insert_record="insert into test (name,year) values("$name","$year");
    $result=mysql_query($insert_record);
    if($result)
    {
        echo "Record inserted successfully";
    }
    else
    {
    echo "please try again";
    }
}
?>

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.