mysql - trying to connect database with php -
i'm making registration page , have phpmyadmin set on xampp, apache's port 8080
, table's name registration
, database's name loginregister
, whenever submit values arent getting transmitted table , don't see errors, help?
this code:
<!doctype html> <html> <head> <title>register</title> </head> <body> <form action="" method="post" name="form1"> <table> <tr> <td>enter first name*</td> <td><input type="text" name="fname" required="yes" pattern="^[a-z1-9]+"></td> <td><p>username takes small letters or numbers, no capital letters</p></td> </tr> <tr> <td>enter last name*</td> <td><input type="text" name="lname" required="yes"></td> </tr> <tr> <td>enter password*</td> <td><input type="password" name="pw" required="yes"></td> </tr> <tr> <td>enter email adress*</td> <td><input type="email" name="email" required="yes"></td> </tr> <tr> <td>enter username*</td> <td><input type="text" name="uname" required="yes"></td> </tr> <tr> <td><input type="submit" value="submit" name="submit1"></input></td> </tr> </table> </form> <?php if(isset($_post['submit1'])) { $link=mysqli_connect('localhost','root','' , "loginregister"); $res= "insert loginregister (fname , lname , pw , email , uname) values('$_post[$fname]','$_post[$lname]','$_post[$pw]','$_post[$email]','$_post[$uname]'))"; } ?> </body> </html>
i forgot explain answer. bad!
in piece of code did not post database. did create connection mysqli_connect
.
here piece of code using pdo save registered user database, safely.
if(isset($_post['submit1'])) { /** * connect database. */ try { $conn = new pdo('mysql:dbname=loginregister;host=localhost', 'root', ''); } catch (pdoexception $e) { /** * catch exceptions in case connection should fail. */ echo 'failed connect: '.$e->getmessage(); die(); } /** * here prepare query. * did in piece of code, never executed. */ $stmt = $conn->prepare('insert `registration` (`fname`, `lname`, `pw`, `email`, `uname`) values (:fname, :lname, :pw, :email, :uname)'); /** * passwords should not stored in plain-text. ever. * , john conde pointed out, password_hash better way this. */ $password = password_hash($_post['pw'], password_default); /** * here assign variables query. */ $stmt->bindvalue(':fname', $_post['fname'], pdo::param_str); $stmt->bindvalue(':lname', $_post['lname'], pdo::param_str); $stmt->bindvalue(':pw', $password, pdo::param_str); $stmt->bindvalue(':email', $_post['email'], pdo::param_str); $stmt->bindvalue(':uname', $_post['uname'], pdo::param_str); /** * , here tell pdo statement execute query. */ $stmt->execute(); }
Comments
Post a Comment