Rkboss Rkboss - 4 months ago 16
SQL Question

How do I insert records in a Database

I am trying to insert a record from my form into a database but after click on submit it shows me, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax" error....
please help me

<?php
include('data_conn.php');

if(isset($_POST['subm'])){

$email = mysql_real_escape_string( $_POST['email']);
$query = "SELECT * FROM login WHERE email='$email'";
$result = mysql_query($query) or die(mysql_error());

if (mysql_num_rows($result) ) {
echo '<script language="javascript">';
echo 'alert("Email is Already Exist...."); location.href="signup.php"';
echo '</script>';
}
else {
$f_name = $_POST['f_name'];

$c_name = $_POST['c_name'];
$c_add = $_POST['c_add'];
$mob = $_POST['mob'];
$email = $_POST['email'];
$password = $_POST['password'];



$query = "INSERT INTO login (first_name,company_name,company_add,mob,email,password) VALUES ('$f_name,'$c_name','$c_add','$mob','$email','$password')";


$result = mysql_query($query) or die(mysql_error());


if($result==1)
{
echo '<script language="javascript">';
echo 'alert("successfully registered!!!"); location.href="signup.php"';
echo '</script>';
}
else
{
echo '<script language="javascript">';
echo 'alert("Something Went Wrong!!! :("); location.href="signup.php"';
echo '</script>';
}
}
}
?>

Answer

Change the below line

$query = "INSERT INTO login (first_name,company_name,company_add,mob,email,password) VALUES ('$f_name,'$c_name','$c_add','$mob','$email','$password')";

To this

$query = "INSERT INTO login (first_name,company_name,company_add,mob,email,password) VALUES ('$f_name','$c_name','$c_add','$mob','$email','$password')";

The issue is, you added only one ' to the variable $f_name. Just make it like '$f_name' and it will work