-4

Error: 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 '1' at line 1

I got this above error while running super easy code that any idiot can build it, so I don't know what and where or is my machine needs to be turned on and off or my luck is not working. Please help me or I'll start crying.

<?php
$con=mysqli_connect("localhost","root","some password","test");
// Check connection
if (mysqli_connect_errno())
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql=mysqli_query($con,"INSERT INTO users(first_name, last_name, username,password,email)
                        VALUES('$_POST[fname]', '$_POST[lname]', '$_POST[username]', '$_POST[password]','$_POST[email]')");
if (!mysqli_query($con,$sql))
{
   die('Error: ' . mysqli_error($con));
}
header("Location:reg.php?remarks=success");
echo " record added";
mysqli_close($con);
?>
Farzad
  • 842
  • 2
  • 9
  • 26
simpleProgrammer
  • 107
  • 3
  • 12

2 Answers2

1

Try wthout the database in con and select db separately.

$con=mysqli_connect("localhost","root","some password");
mysqli_select_db("dbname here",$con);
Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
TAS
  • 331
  • 3
  • 10
-1

This $_POST value are wrong. Try with:

$sql=mysqli_query($con,"INSERT INTO users(first_name, last_name, username,password,email)VALUES('{$_POST['fname']}', '{$_POST['lname']}'...
Christian
  • 484
  • 3
  • 15