php - how to insert form data into a mysql database -
i'm trying insert data that's in form database made. keep getting error when check if data in database:
error code: 1064. have error in sql syntax; check manual corresponds mysql server version right syntax use near 'user_comments' @ line 1
this php:
<?php $db_connection = mysqli_connect('localhost','root','',"project_online_planner"); if (!$db_connection){ die('failed connect mysql:'.mysql_error()); }else{ echo "it worked"; } if(isset($_post['insertcomments'])){ $name=$_post['name']; $comment=$_post['comment']; mysqli_query($db_connection,"insert user_comments (name, comment) values ($name, $comment)"); print "your information has been added database."; } ?> this html:
<div id="uploadcomments"> <form id="insertcomments" name="insertcomments" method="post"> <label for="name">name: </label><input type="text" id="name" name="name"><br/> <label for="comment">comments: </label><textarea name="comment" id="comment"></textarea> <input type="submit" value="submit"> </form> </div>
as pervious people have commented should construct "query" separately, makes easier handle , change. additionally when passing variable query need bring them out of string php can process them correctly.
here how suggest structure code:
....
$name=$_post['name']; $comment=$_post['comment']; $sql="insert user_comments (name, comment)values('{$name}','{$comment}')"; if (!mysqli_query($db_connection,$sql)) { die('error: ' . mysqli_error($con)); } echo "1 record added";
Comments
Post a Comment