<html>
<body>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
$sql="INSERT INTO users(firstname, lastname)
VALUES
('$_POST[firstname]','$_POST[lastname]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
</body>
</html>
SirMahlon
0
Junior Poster in Training
Recommended Answers
Jump to PostCheck first whether $_POST values exist (they usually do not on the first page load). And also it is very important to escape the values before inserting into the database.
// check if values exist if(isset($_POST[firstname]) and isset($_POST[lastname])) { $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not …
Jump to PostSory, my mistake, forgot the quotes in array indexes. This is correct:
$firstname = mysql_real_escape_string($_POST['firstname']); $lastname = mysql_real_escape_string($_POST['lastname']);
Jump to PostWhat is the code on line 2?
Jump to PostOK, the quotes are missing in this line also. Try:
if(isset($_POST['firstname']) && isset($_POST['lastname'])) {
Jump to PostEcho the query and test it in phpmyadmin (or post it here). Put this temporary debug line somewhere (like line 11 in your original post):
die("INSERT INTO users(firstname, lastname) VALUES ('$firstname','$lastname')");
This will display the constructed query and stop the script. Now copy it to phpmyadmin …
All 22 Replies
broj1
356
Humble servant
Featured Poster
SirMahlon
0
Junior Poster in Training
broj1
356
Humble servant
Featured Poster
SirMahlon
0
Junior Poster in Training
broj1
356
Humble servant
Featured Poster
SirMahlon
0
Junior Poster in Training
broj1
356
Humble servant
Featured Poster
SirMahlon
0
Junior Poster in Training
broj1
356
Humble servant
Featured Poster
SirMahlon
0
Junior Poster in Training
SirMahlon
0
Junior Poster in Training
broj1
356
Humble servant
Featured Poster
SirMahlon
0
Junior Poster in Training
SirMahlon
0
Junior Poster in Training
broj1
356
Humble servant
Featured Poster
SirMahlon
0
Junior Poster in Training
SirMahlon
0
Junior Poster in Training
SirMahlon
0
Junior Poster in Training
SirMahlon
0
Junior Poster in Training
broj1
356
Humble servant
Featured Poster
SirMahlon
0
Junior Poster in Training
broj1
356
Humble servant
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.