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";
}
?>
Need help
Moderator: General Moderators
Re: Need help
$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.
-
akhilesh1010
- Forum Newbie
- Posts: 15
- Joined: Thu Aug 22, 2013 1:56 am
Re: Need help
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.
$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.
Re: Need help
The quotes are necessary. Try again.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.