php - Cannot connect html5 forms to my sql database? -
i have html5 document requesting input user form(saved employee.php).
also have created php document (sqlconnectionprocess.php) connecting forms local database. using xampp , phpmyadmin.
employee.php:
<html> <body> <form name="employeedatabase" action="sqlconnectionprocess.php" method="post"> <link rel="stylesheet" href="css.css"> <h1>employee database</h1> employe card no: <input type="text" name="cardno" ><br><br> employee no: <input type="text" name="employeeno" ><br><br> employee name: <input type="text" name="employeename"><br><br> nationality: <input type="text" name="nationality"><br><br> profession: <input type="text" name="profession"><br><br> dob: <input type="text" name="dob"><br><br> doj: <input type="text" name="doj"><br><br> doa(visitvisa): <input type="text" name="doa"><br><br> company code: <input type="text" name="companycode"><br><br> sponsor code: <input type="text" name="sponsorcode"><br><br> visa type: <input type="text" name="visatype"><br><br> status: <input type="text" name="status"><br><br> <input type="submit" name="formsubmit" value="submit"> <?php ?> </form> </body> </html>
sqlconnectionprocess.php:
<?php $con = mysql_connect('localhost','root','mysql'); mysql_select_db('employee_info',$con); if(isset($_post['formsubmit'])){ $cardno= isset($_post['cardno']) ? $_post['cardno'] : 0; $employeeno= isset($_post['employeeno']) ? $_post['employeeno'] : 0; $employeename= isset($_post['employeename']) ? $_post['employeename'] : ""; $nationality= isset($_post['nationality']) ? $_post['nationality'] : ""; $profession= isset($_post['profession']) ? $_post['profession'] : ""; $dob= isset($_post['dob']) ? $_post['dob'] : ""; $doj= isset($_post['doj']) ? $_post['doj'] : ""; $doa= isset($_post['doa']) ? $_post['doa'] : ""; $companycode = isset($_post['companycode']) ? $_post['companycode'] : 0; $sponsorcode= isset($_post['sponsorcode']) ? $_post['sponsorcode'] : 0; $visatype= isset($_post['visatype']) ? $_post['visatype'] : ""; $status= isset($_post['status']) ? $_post['status'] : ""; $sql = "insert employee_info info(employeecardno,employeeno,employeename,nationality,profession,dob,doj,doa,companycode,sponsorcode,visatype,status) values ($cardno,$employeeno,$employeename,$nationality,$profession,$dob,$doj,$doa,$companycode,$sponsorcode,$visatype,$status)"; mysql_query($sql); } ?>
phpmyadmin password "mysql".
when submit forms getting following error:
"fatal error: uncaught error: call undefined function mysql_connect() in c:\xampp\htdocs\test1\sqlconnectionprocess.php:3 stack trace: #0 {main} thrown in c:\xampp\htdocs\test1\sqlconnectionprocess.php on line 3"
kindly me. thank you....
please use pdo or mysqli. mysql depreciated , should not used on new code
http://php.net/manual/en/function.mysql-query.php
try link, helped me lot: phpdelusions.net/pdo @your-common-sense.
i code way:
1: db connection file:
<?php $db = new pdo('mysql:host=yourhost;dbname=dbname', 'username', 'password', array(pdo::mysql_attr_init_command => "set names 'utf8'")); $db->setattribute(pdo::attr_emulate_prepares, false); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); ?>
your sqlconnectionprocess.php should this:
<?php require ('path/to/db/con.php'); if(isset($_post['formsubmit'])){ $cardno = isset($_post['cardno']) ? $_post['cardno'] : 0; $employeeno = isset($_post['employeeno']) ? $_post['employeeno'] : 0; $employeename = isset($_post['employeename']) ? $_post['employeename'] : ""; $nationality = isset($_post['nationality']) ? $_post['nationality'] : ""; $profession = isset($_post['profession']) ? $_post['profession'] : ""; $dob = isset($_post['dob']) ? $_post['dob'] : ""; $doj = isset($_post['doj']) ? $_post['doj'] : ""; $doa = isset($_post['doa']) ? $_post['doa'] : ""; $companycode = isset($_post['companycode']) ? $_post['companycode'] : 0; $sponsorcode = isset($_post['sponsorcode']) ? $_post['sponsorcode'] : 0; $visatype = isset($_post['visatype']) ? $_post['visatype'] : ""; $status = isset($_post['status']) ? $_post['status'] : ""; $stmt = $db->prepare("insert employee_info (employeecardno, employeeno, employeename, nationality, profession, dob, doj, doa, companycode, sponsorcode, visatype, status) values ($cardno, $employeeno, $employeename, $nationality, $profession, $dob, $doj, $doa, $companycode, $sponsorcode, $visatype, $status)" ); $stmt->execute(); } else{ //something went wrong } ?>
you have lot of isset()
conditions. if 1 fails whole code fails. try starting point.
thanks.
Comments
Post a Comment