Page 1 of 1

Need help

Posted: Tue Aug 20, 2013 7:47 am
by xbear1982
Hi Guys
Thanks for the great help previouse, Can anyone kindly explain to me what wrong with this code please?, it directly display error only.

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$ID=$_POST['ID'];
$FirstName=$_POST['FirstName'];
$LastName=$_POST['LastName'];
$Age=$_POST['Age'];
$Contact_no=$_POST['Contact_no'];
$Birth=$POST['Date_of_Birth'];
$Contact_email=$POST['Contact_email'];
$Sex=$POST['Sex'];
$Roster_Time=$POST['ROSTER_TIME'];

// Insert data into mysql
$sql="INSERT INTO $tbl_name($ID,$FirstName,$LastName,$Age,$Contact_no,$Birth,$Contact_email,$sex,$Roster_Time)VALUES('ID','$FirstName','$LastName','$Age',
'$Contact_no','$Birth','$Contact_email','$Sex','$Roster_Time')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='employee.html'>Back to main page</a>";
}

else {
echo "ERROR";
}


?>

Re: Need help

Posted: Tue Aug 20, 2013 8:21 am
by Celauran
$result is evaluating to false, so there's an error in your MySQL query. Looks like you're trying to pass values as column names. Check mysql_error(). Speaking of mysql_ functions, they have been deprecated; don't use them. Take a look at MySQLi or, better, PDO.

Re: Need help

Posted: Thu Aug 22, 2013 2:30 am
by akhilesh1010
You Should use this structure for query
$sql="INSERT INTO TABLENAME(`FIELD1`,`FIELD2`,`FIELD3`)VALUES($var1,$var2,$var3)";
replace tablename with your tablename fields with your field names and use variable without single quote. :D

Re: Need help

Posted: Thu Aug 22, 2013 2:50 am
by requinix
akhilesh1010 wrote:You Should use this structure for query
$sql="INSERT INTO TABLENAME(`FIELD1`,`FIELD2`,`FIELD3`)VALUES($var1,$var2,$var3)";
replace tablename with your tablename fields with your field names and use variable without single quote. :D
The quotes are necessary. Try again.