php - Contact form not uploading to database (MySQL) -
i have contact form trying input information website designated table in database. know need code run correctly? can input information in textfields on website , hit submit, reason, first name, last name, email, etc not show in database @ all. when click submit, "thank you" message show up. include contact form, php, , database like, perhaps help.
contact form contactus.php
<!doctype html> <html> <head> <meta charset = "utf-8"> <title>contact</title> <style type = "text/css"> em { font-weight: bold; color: black; } p { font-size: 12pt; font-family: times new roman, sans-serif; color: black; } </style> </head> <body> <?php include ('menu.html'); ?> <? include ("form_process.php"); ?> <br> please fill out form below contact information.<br> <form action= "form_process.php" method= "post" name= "contact_form"><br> <p>first name: <input name= "first_name" type= "text" id= "first_name"/> </p><br> <p>last name: <input name= "last_name" type= "text" id= "last_name"/> </p><br> <p>email: <input name= "email" type= "text" id= "email"/> </p><br> <p>phone: <input name= "phone" type= "text" id= "phone"/> </p><br> <p>result: <div> <select name= "result" id= "result"> <option value="">choose</option> <option value="a">a</option> <option value="b">b</option> <option value="c">c</option> <option value="d">d</option> </select> </div> </p><br> <p>comments: <textarea name= "comments" id= "comments" placeholder= "comment here" cols="25", rows="3"></textarea> </p><br> <p>thank visiting our site!</p><br> <p> <input type= "submit" name= "submit" id= "submit" value= "submit" /> <input type= "reset" name= "reset" id= "reset" value= "reset" /> </p> </form> <div class="footer"><?php include('footer.html');?></div> </body> </html>
database name of database project table wish information input called contactform & empty except variables have created. within table of contactform have variables:
id int(10) auto_increment first_name varchar(60) last_name varchar(60) email varchar(60) phone int(15) result varchar(15) comments varchar(255)
**update: when change php (i tried follow @davecartwright suggestions):
<?php $user = 'root'; $pass = ''; $db = 'project'; $db = new mysqli('localhost', $user, $pass, $db) or die("unable connect database"); /*i not know mean this: mysqli_real_escape_string($db, $escapestring);*/ $first_name = $_post['first_name']; $last_name = $_post['last_name']; $email = $_post['email']; $phone = $_post['phone']; $result = $_post['result']; $comments = $_post['comments']; $first_name = mysql_real_escape_string($first_name); $last_name = mysql_real_escape_string($last_name); $email = mysql_real_escape_string($email); $phone = mysql_real_escape_string($phone); $result = mysql_real_escape_string($result); $comments = mysql_real_escape_string($comments); $query= "insert `project`.`contactform` (`id`,`first_name`, `last_name`, `email`, `phone`, `result`, `comments`) values (null, '$first_name', '$last_name', '$email', '$phone', '$result', '$comments');"; mysqli_query($db, $query) or die(mysqli_error($db)); echo "<h2>thanks, information has been submitted!</h2>"; ?>
i several notices of undefined variables escapestring (line 8), undefined index for: first_name (line 10), last_name (line 11), email (line 12), phone (line 13), result (line 14), comments (line 15). however, news: when in database table have input there. id fills in correctly, variable phone thing listed 0 & other variables empty each entry. @ least progress - have not seen entries show before now. how can further fix input show up?
ok, main problem see mixing mysql , mysqli commands.
first, should definitly use
mysqli_real_escape_string($db, $escapestring).
secondly, query should use mysqli like:
$query= "insert `project`.`contactform` (`id`,`first_name`, `last_name`, `email`, `phone`, `result`, `comments`) values (null, '$first_name', '$last_name', '$email', '$phone', '$result', '$comments');"; mysqli_query($db, $query) or die(mysqli_error($db));
i'm not sure if enclosing variables necessary...
update:
ok, because seems such issue understand mysql_real_escape_string() , mysqli_real_escape_string() different functions... please change:
$first_name = mysql_real_escape_string($first_name); $last_name = mysql_real_escape_string($last_name); $email = mysql_real_escape_string($email); $phone = mysql_real_escape_string($phone); $result = mysql_real_escape_string($result); $comments = mysql_real_escape_string($comments);
to
$first_name = mysqli_real_escape_string($db, $first_name); $last_name = mysqli_real_escape_string($db, $last_name); $email = mysqli_real_escape_string($db, $email); $phone = mysqli_real_escape_string($db, $phone); $result = mysqli_real_escape_string($db, $result); $comments = mysqli_real_escape_string($db, $comments);
Comments
Post a Comment