Insert Data to MySQL db from HTML form using PHP
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?
php html mysql mysqli
|
show 3 more comments
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?
php html mysql mysqli
If you got any error message, You can share the error messages with your question.
– ashanrupasinghe
Nov 15 '18 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.
– Prix
Nov 15 '18 at 5:42
Some error: Notice: Undefined index: id in C:xampphtdocscreate.php on line 14 Notice: Undefined index: name in C:xampphtdocscreate.php on line 15 Notice: Undefined index: year in C:xampphtdocscreate.php on line 16
– Trần Vũ Anh Dũng
Nov 15 '18 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.
– Prix
Nov 15 '18 at 5:45
Yes I have. I used this: <input type="text" name="name" class="form-control" id="Name" placeholder="Enter brand">
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:52
|
show 3 more comments
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?
php html mysql mysqli
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?
php html mysql mysqli
php html mysql mysqli
asked Nov 15 '18 at 5:30
Trần Vũ Anh DũngTrần Vũ Anh Dũng
83
83
If you got any error message, You can share the error messages with your question.
– ashanrupasinghe
Nov 15 '18 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.
– Prix
Nov 15 '18 at 5:42
Some error: Notice: Undefined index: id in C:xampphtdocscreate.php on line 14 Notice: Undefined index: name in C:xampphtdocscreate.php on line 15 Notice: Undefined index: year in C:xampphtdocscreate.php on line 16
– Trần Vũ Anh Dũng
Nov 15 '18 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.
– Prix
Nov 15 '18 at 5:45
Yes I have. I used this: <input type="text" name="name" class="form-control" id="Name" placeholder="Enter brand">
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:52
|
show 3 more comments
If you got any error message, You can share the error messages with your question.
– ashanrupasinghe
Nov 15 '18 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.
– Prix
Nov 15 '18 at 5:42
Some error: Notice: Undefined index: id in C:xampphtdocscreate.php on line 14 Notice: Undefined index: name in C:xampphtdocscreate.php on line 15 Notice: Undefined index: year in C:xampphtdocscreate.php on line 16
– Trần Vũ Anh Dũng
Nov 15 '18 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.
– Prix
Nov 15 '18 at 5:45
Yes I have. I used this: <input type="text" name="name" class="form-control" id="Name" placeholder="Enter brand">
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:52
If you got any error message, You can share the error messages with your question.
– ashanrupasinghe
Nov 15 '18 at 5:42
If you got any error message, You can share the error messages with your question.
– ashanrupasinghe
Nov 15 '18 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.– Prix
Nov 15 '18 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.– Prix
Nov 15 '18 at 5:42
Some error: Notice: Undefined index: id in C:xampphtdocscreate.php on line 14 Notice: Undefined index: name in C:xampphtdocscreate.php on line 15 Notice: Undefined index: year in C:xampphtdocscreate.php on line 16
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:44
Some error: Notice: Undefined index: id in C:xampphtdocscreate.php on line 14 Notice: Undefined index: name in C:xampphtdocscreate.php on line 15 Notice: Undefined index: year in C:xampphtdocscreate.php on line 16
– Trần Vũ Anh Dũng
Nov 15 '18 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.
– Prix
Nov 15 '18 at 5:45
@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.
– Prix
Nov 15 '18 at 5:45
Yes I have. I used this: <input type="text" name="name" class="form-control" id="Name" placeholder="Enter brand">
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:52
Yes I have. I used this: <input type="text" name="name" class="form-control" id="Name" placeholder="Enter brand">
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:52
|
show 3 more comments
3 Answers
3
active
oldest
votes
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.
thank for replying. I did solve the problem.
– Trần Vũ Anh Dũng
Nov 15 '18 at 7:29
You can mark this anwer if it worked for you :)
– Jestin Sebastian
Nov 17 '18 at 18:51
add a comment |
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);
?>
new mysqlialready does the connect within.
– Prix
Nov 15 '18 at 5:43
thank you for replying, but i still got the same errors as I did. "Undefined index: id in C:xampphtdocscreate.php on line 18"
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:48
can you post your form inputs?
– Khalifa Nikzad
Nov 15 '18 at 5:52
your id may be autoincrement, so it is not needed to get from form and insert
– Khalifa Nikzad
Nov 15 '18 at 5:55
My form : <input type="text" name="id" class="form-control" id="id" placeholder="Input ID">
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:57
|
show 9 more comments
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";
?>
add a comment |
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312997%2finsert-data-to-mysql-db-from-html-form-using-php%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
thank for replying. I did solve the problem.
– Trần Vũ Anh Dũng
Nov 15 '18 at 7:29
You can mark this anwer if it worked for you :)
– Jestin Sebastian
Nov 17 '18 at 18:51
add a comment |
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.
thank for replying. I did solve the problem.
– Trần Vũ Anh Dũng
Nov 15 '18 at 7:29
You can mark this anwer if it worked for you :)
– Jestin Sebastian
Nov 17 '18 at 18:51
add a comment |
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.
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.
answered Nov 15 '18 at 6:25
Jestin SebastianJestin Sebastian
363110
363110
thank for replying. I did solve the problem.
– Trần Vũ Anh Dũng
Nov 15 '18 at 7:29
You can mark this anwer if it worked for you :)
– Jestin Sebastian
Nov 17 '18 at 18:51
add a comment |
thank for replying. I did solve the problem.
– Trần Vũ Anh Dũng
Nov 15 '18 at 7:29
You can mark this anwer if it worked for you :)
– Jestin Sebastian
Nov 17 '18 at 18:51
thank for replying. I did solve the problem.
– Trần Vũ Anh Dũng
Nov 15 '18 at 7:29
thank for replying. I did solve the problem.
– Trần Vũ Anh Dũng
Nov 15 '18 at 7:29
You can mark this anwer if it worked for you :)
– Jestin Sebastian
Nov 17 '18 at 18:51
You can mark this anwer if it worked for you :)
– Jestin Sebastian
Nov 17 '18 at 18:51
add a comment |
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);
?>
new mysqlialready does the connect within.
– Prix
Nov 15 '18 at 5:43
thank you for replying, but i still got the same errors as I did. "Undefined index: id in C:xampphtdocscreate.php on line 18"
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:48
can you post your form inputs?
– Khalifa Nikzad
Nov 15 '18 at 5:52
your id may be autoincrement, so it is not needed to get from form and insert
– Khalifa Nikzad
Nov 15 '18 at 5:55
My form : <input type="text" name="id" class="form-control" id="id" placeholder="Input ID">
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:57
|
show 9 more comments
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);
?>
new mysqlialready does the connect within.
– Prix
Nov 15 '18 at 5:43
thank you for replying, but i still got the same errors as I did. "Undefined index: id in C:xampphtdocscreate.php on line 18"
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:48
can you post your form inputs?
– Khalifa Nikzad
Nov 15 '18 at 5:52
your id may be autoincrement, so it is not needed to get from form and insert
– Khalifa Nikzad
Nov 15 '18 at 5:55
My form : <input type="text" name="id" class="form-control" id="id" placeholder="Input ID">
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:57
|
show 9 more comments
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);
?>
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);
?>
edited Nov 15 '18 at 6:23
answered Nov 15 '18 at 5:42
Khalifa NikzadKhalifa Nikzad
84914
84914
new mysqlialready does the connect within.
– Prix
Nov 15 '18 at 5:43
thank you for replying, but i still got the same errors as I did. "Undefined index: id in C:xampphtdocscreate.php on line 18"
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:48
can you post your form inputs?
– Khalifa Nikzad
Nov 15 '18 at 5:52
your id may be autoincrement, so it is not needed to get from form and insert
– Khalifa Nikzad
Nov 15 '18 at 5:55
My form : <input type="text" name="id" class="form-control" id="id" placeholder="Input ID">
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:57
|
show 9 more comments
new mysqlialready does the connect within.
– Prix
Nov 15 '18 at 5:43
thank you for replying, but i still got the same errors as I did. "Undefined index: id in C:xampphtdocscreate.php on line 18"
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:48
can you post your form inputs?
– Khalifa Nikzad
Nov 15 '18 at 5:52
your id may be autoincrement, so it is not needed to get from form and insert
– Khalifa Nikzad
Nov 15 '18 at 5:55
My form : <input type="text" name="id" class="form-control" id="id" placeholder="Input ID">
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:57
new mysqli already does the connect within.– Prix
Nov 15 '18 at 5:43
new mysqli already does the connect within.– Prix
Nov 15 '18 at 5:43
thank you for replying, but i still got the same errors as I did. "Undefined index: id in C:xampphtdocscreate.php on line 18"
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:48
thank you for replying, but i still got the same errors as I did. "Undefined index: id in C:xampphtdocscreate.php on line 18"
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:48
can you post your form inputs?
– Khalifa Nikzad
Nov 15 '18 at 5:52
can you post your form inputs?
– Khalifa Nikzad
Nov 15 '18 at 5:52
your id may be autoincrement, so it is not needed to get from form and insert
– Khalifa Nikzad
Nov 15 '18 at 5:55
your id may be autoincrement, so it is not needed to get from form and insert
– Khalifa Nikzad
Nov 15 '18 at 5:55
My form : <input type="text" name="id" class="form-control" id="id" placeholder="Input ID">
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:57
My form : <input type="text" name="id" class="form-control" id="id" placeholder="Input ID">
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:57
|
show 9 more comments
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";
?>
add a comment |
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";
?>
add a comment |
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";
?>
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";
?>
answered Nov 15 '18 at 5:45
Atul AkabariAtul Akabari
954
954
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312997%2finsert-data-to-mysql-db-from-html-form-using-php%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown

If you got any error message, You can share the error messages with your question.
– ashanrupasinghe
Nov 15 '18 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.– Prix
Nov 15 '18 at 5:42
Some error: Notice: Undefined index: id in C:xampphtdocscreate.php on line 14 Notice: Undefined index: name in C:xampphtdocscreate.php on line 15 Notice: Undefined index: year in C:xampphtdocscreate.php on line 16
– Trần Vũ Anh Dũng
Nov 15 '18 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.
– Prix
Nov 15 '18 at 5:45
Yes I have. I used this: <input type="text" name="name" class="form-control" id="Name" placeholder="Enter brand">
– Trần Vũ Anh Dũng
Nov 15 '18 at 5:52