Need help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xbear1982
Forum Newbie
Posts: 9
Joined: Sun Aug 18, 2013 10:50 pm

Need help

Post 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";
}


?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help

Post 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.
akhilesh1010
Forum Newbie
Posts: 15
Joined: Thu Aug 22, 2013 1:56 am

Re: Need help

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need help

Post 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.
Post Reply