Hi everyone could any one tell me what is wrong with this code,when I run it, dose not insert data into the database. i did create the database and the table . Thanks a lot <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con) or die("can not connected to selected " . mysql_error()); $sql="INSERT INTO person (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> PHP:
$con = mysql_connect("localhost","",""); Are you using: $con = mysql_connect("localhost","username","password"); And: mysql_select_db("your_databas_name", $con)
Your arrays are wrong. Fix it to this $sql="INSERT INTO person (FirstName, LastName, Age) VALUES ('{$_POST['firstname']}','{$_POST['lastname']}','{$_POST['age']}')"; PHP: And this is very vulnerable to sql injections, since you are directly querying from raw post data.
How can you connect without using a username in the connection? $con = mysql_connect("localhost","",""); PHP: You should have at least 1 username and the user should have proper privillege in the database to insert/select/update etc. Try using the default one, if you don't create an user. $con = mysql_connect("localhost","root",""); PHP: