PHP Form to Insert data in Database -
i've created form inserts data database. i've been given 2 functions data , display it, these located in file called querydb.php:
function addcustomer($fname, $lname, $address, $phone) { $db = new mydb(); if(!$db){ echo '<script type="text/javascript">alert("'.$db->lasterrormsg().'");</script>'; } else { //echo "opened database successfully\n"; } $sql ='insert customers (firstname, lastname, address, phone) values ("'.$fname.'", "'.$lname.'", "'.$address.'", "'.$phone.'");'; $db->query($sql); }
get function:
function getcustomers($searchterm = null) { $db = new mydb(); if(!$db){ echo '<script type="text/javascript">alert("'.$db->lasterrormsg().'");</script>'; } else { //echo "opened database successfully\n"; } if(!$searchterm) { $sql ='select * customers;'; } else { $sql ='select * customers firstname "'.$searchterm.'" or lastname "'.$searchterm.'" or address "'.$searchterm.'" or phone "'.$searchterm.'"'; } $ret = $db->query($sql); $array = []; if(!$ret){ echo $db->lasterrormsg(); return []; } else { while($row = $ret->fetcharray(sqlite3_assoc) ){ $array[] = $row; } $db->close(); return $array; } }
in reviewsubmit.php have top:
<?php require_once "querydb.php"; $firstname = $_post["firstname"]; $lastname = $_post["lastname"]; $address = $_post["address"]; $phone = $_post["phone"]; addcustomer($firstname, $lastname, $address, $phone); ?>
and form:
<form action="reviewsubmit.php" method="post"> firstname <input type="text" id="firstname" name="firstname"> /> lastname <input type="text" id="lastname" name="lastname""> /> address <input type="text" id="address" name="address"> /> phone <input type="text" id="phone" name="phone"> /> <input type="submit" name="submit" value="submit" /> </form>
the problem when submit information database using form, submits empty values when click submit first time. when hit submit again submits actual values: image
your html mark-up not best , cause anomalies. simultaneously, ideal add required attribute fields ensure necessary fields filled before submitting so:
<!-- guard against accidentally inserting empty values --> <!-- why not make input fields mandatory --> <form action="reviewsubmit.php" method="post"> <label for="firstname">firstname</label> <input type="text" id="firstname" name="firstname" required /> <label for="lastname">lastname</label> <input type="text" id="lastname" name="lastname" required /> <label for="address">address</label> <input type="text" id="address" name="address" required /> <label for="phone">telephone</label> <input type="text" id="phone" name="phone" required/> <input type="submit" name="submit" value="submit" /> </form>
on php side, may go mile of checking submitted values not empty or null before submitting database. way both sure of going on @ front , back-ends. way of doing that:
<?php // file: index.php require_once "querydb.php"; $firstname = isset($_post["firstname"])? htmlspecialchars(trim($_post["firstname"])) : null; $lastname = isset($_post["lastname"]) ? htmlspecialchars(trim($_post["lastname"])) : null; $address = isset($_post["address"]) ? htmlspecialchars(trim($_post["address"])) : null; $phone = isset($_post["phone"]) ? htmlspecialchars(trim($_post["phone"])) : null; // check don't have null or empty field values before inserting database if(!is_null($firstname) && !is_null($lastname) && !is_null($address) && !is_null($phone) ){ addcustomer($firstname, $lastname, $address, $phone); } ?>
Comments
Post a Comment