Execute PHP Script using HTML Form -
hello dear why fail learning php, grateful if can me. :'( followed step step here :
http://www.w3schools.com/php/php_mysql_insert.asp
but when click button submit query nothing happen, show blank white screen , dont see new data on database?
<html> <body> <form action="insert.php" method="post"> firstname: <input type="text" name="firstname"> lastname: <input type="text" name="lastname"> age: <input type="text" name="age"> <input type="submit"> </form> </body> </html>
<?php $con=mysqli_connect("localhost","root","","garutexpress"); // check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } $sql="insert persons (firstname, lastname, age) values ('$_post[firstname]','$_post[lastname]','$_post[age]')"; if (!mysqli_query($con,$sql)) { die('error: ' . mysqli_error($con)); } echo "1 record added"; mysqli_close($con); ?>
if data successfull added should give echo "1 record added"; never see message.
your table name "persons", not "persons"
when make query, table name has same in database. if in phpmyadmin , table "persons" lowercase
edited according : @i can has cheezburger
please change name of table in code , make sure wrap quotes accordingly :
<?php $con=mysqli_connect("example.com","peter","abc123","my_db"); // check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } $sql='insert persons (firstname, lastname, age) values ("'.$_post['firstname'].'","'.$_post['lastname'].'","'.$_post['age'].'"); if (!mysqli_query($con,$sql)) { die('error: ' . mysqli_error($con)); } echo "1 record added"; mysqli_close($con); ?>
Comments
Post a Comment