html - PHP and mySQL not working -
php code :
<?php // create connection $con=mysqli_connect("localhost","root","root","demo1"); echo "connection successful"; // check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } mysql_select_db("demo1",$con); $sqli="insert employee (employee id,name,date hired,position,salary,department code,can hire,bossid) values('$_post[empid]','$_post[name]','$_post[datehired]','$_post[position]','$_post[salary]','$_post[d eptcode]','$_post[canhire]','$_post[bossid]')"; if (!mysqli_query($con,$sqli)) { die('error: ' . mysqli_error($con)); } echo "1 record added"; mysqli_close($con); ?> html code
<!doctype html> <html> <body> <h1> employee </h1><br> <form action="lab5.php" method="post"> employee id: <input type="text" name="empid" ><br> name: <input type="text" name="name" ><br> date hired <input type="text" name="datehired" ><br> position: <input type="text" name="position" ><br> salary: <input type="text" name="salary" ><br> department code: <input type="text" name="deptcode" ><br> can hire <input type="text" name="canhire" ><br> bossid: <input type="text" name="bossid" ><br> <input type="image" src="submit.gif" alt="submit" width="100" height="50"><br> </body> </html> error message:
connection successfulerror: have error in sql syntax; check manual corresponds mysql server version right syntax use near 'id, name, date hired, position, salary, department code, can hire, bossid) valu' @ line 1
table screenshot : http://tinypic.com/r/vni4bc/8
you've mixed code mysql , mysqli. i've made code mysqli only. , please avoid using (spaces) in column name. , can prevent sql injection using mysqli_real_escape_string:
lab5.php:
<?php /* check connection */ $connection=mysqli_connect("localhost","root","root","demo1"); if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } $empid=mysqli_real_escape_string($connection,$_post['empid']); $name=mysqli_real_escape_string($connection,$_post['name']); $datehired=mysqli_real_escape_string($connection,$_post['datehired']); $position=mysqli_real_escape_string($connection,$_post['position']); $salary=mysqli_real_escape_string($connection,$_post['salary']); $deptcode=mysqli_real_escape_string($connection,$_post['deptcode']); $canhire=mysqli_real_escape_string($connection,$_post['canhire']); $bossid=mysqli_real_escape_string($connection,$_post['bossid']); /* mysqli real escape string prevent bit of sql injection */ mysqli_query($connection,"insert employee (empid, name, datehired, position, salary, deptcode, canhire, bossid) /* double check column name */ values('$empid','$name','$datehired','$position','$salary','$deptcode','$canhire','$bossid')"; mysqli_close($con); ?> your html code:
<html> <body> <h1> employee </h1><br> <form action="lab5.php" method="post"> employee id: <input type="text" name="empid" ><br> name: <input type="text" name="name" ><br> date hired <input type="date" name="datehired" ><br> position: <input type="text" name="position" ><br> salary: <input type="number" name="salary" ><br> department code: <input type="text" name="deptcode" ><br> can hire <input type="text" name="canhire" ><br> bossid: <input type="text" name="bossid" ><br> <input type='submit'> </form> </body> </html>
Comments
Post a Comment