php - Registration form update to MySQL PDO -
i'm new php , mysql. have following registration form asks user put in name, password , email. validation works , i'm able connect database cannot insert table because i'm having hard time updating code old mysql mysql pdo. here code:
form:
<form name="registration" method="post" action="registration.php"> <table width="400" border="5" align="center"> <tr> <td colspan="5"><h1>registration form</h1></td> </tr> <tr> <td>user name:</td> <td><input type="text" name="name" /></td> </tr> <tr> <td>user password:</td> <td><input type="password" name="pass" /></td> </tr> <tr> <td>user email:</td> <td><input type="text" name="email" /></td> </tr> <tr> <td colspan="5" align="center"><input type="submit" name="submit" value="sign up" /></td> </tr> </table> </form>
here php , mysql part:
<?php // pdo connect $servername = "localhost"; $username = "root"; $password = ""; $dbname = "users_db"; try { $conn = new pdo("mysql:host=$servername;dbname=$dbname", $username, $password); // set pdo error mode exception $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); //echo "connected successfully"; } catch(pdoexception $e) { echo "connection failed: " . $e->getmessage(); } if(isset($_post['submit'])){ $user_name = $_post['name']; $user_pass = $_post['pass']; $user_email = $_post['email']; if($user_name==''){ echo "<script>alert('please enter name!')</script>"; exit(); } if($user_name==''){ echo "<script>alert('please enter password!')</script>"; exit(); } if($user_name==''){ echo "<script>alert('please enter email!')</script>"; exit(); } // validation , field insertion $check_email = "select * users user_name='$user_email'"; $run = mysql_query($check_email); if(mysql_num_rows($run)>0){ echo <script>alert('email $user_email exist!')</script>"; exit(); } $query = "insert users(user_name,user_pass,user_email) values ('$user_name','$user_name','$user_name') "; if(mysql_query($query)){ echo <script>window.open('welcome.php','_self')</script>"; exit(); } } ?>
please let me know how can update after "// validation , field insertion" part.
its here need read more pdo:
<?php // pdo connect $servername = "localhost"; $username = "root"; $password = ""; $dbname = "users_db"; try { $conn = new pdo("mysql:host=$servername;dbname=$dbname", $username, $password); // set pdo error mode exception $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); //echo "connected successfully"; } catch(pdoexception $e) { echo "connection failed: " . $e->getmessage(); } if(isset($_post['submit'])){ $user_name = $_post['name']; $user_pass = $_post['pass']; $user_email = $_post['email']; if($user_name==''){ echo "<script>alert('please enter name!')</script>"; exit(); } if($user_name==''){ echo "<script>alert('please enter password!')</script>"; exit(); } if($user_name==''){ echo "<script>alert('please enter email!')</script>"; exit(); } // validation , field insertion $check_email = "select * users user_email = :email"; $check_email = $conn->prepare($check_email); $check_email->execute(array(':email'=>$user_email)); if($check_email->rowcount() >0){ echo "<script>alert('email $user_email exist!')</script>"; exit(); } $query = "insert users(user_name,user_pass,user_email) values (?, ?, ?)"; $query = $conn->prepare($query); $query->bindparam('1', $user_name); $query->bindparam('2', $user_pass); $query->bindparam('3', $user_email); $query->execute(); if($query->rowcount() > 0) { echo "<script>window.open('welcome.php','_self')</script>"; exit(); } } ?>
Comments
Post a Comment