php - Insert form value into mysql database with pdo -
hi changed mysql querys pdo. problem havent experience pdo. edit how thought nothing works. no errors showing. can find mistake? reading.
<?php session_start(); if(isset($_post["submit"])){ $hostname='localhost'; $username='root'; $password=''; function generaterandomstring($length = 8) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; $characterslength = strlen($characters); $randomstring = ''; ($i = 0; $i < $length; $i++) { $randomstring .= $characters[rand(0, $characterslength - 1)]; } return $randomstring; } $randomstring = generaterandomstring(); if ($_post['password'] != $_post['password_again']) { $message['error'] = 'die eingegebenen passwörter stimmen nicht überein.'; } else { unset($_post['password_again']); $salt = ''; ($i = 0; $i < 22; $i++) { $salt .= substr('./abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', mt_rand(0, 63), 1); } $_post['password'] = crypt( $_post['password'], '$2a$10$' . $salt ); try { $dbh = new pdo("mysql:host=$hostname;dbname=loginsystem",$username,$password); $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); // <== add line $sql = "insert user2 (username, password, email, firstname, lastname, phone, town, street, zip, country, code) values ('".$_post["username"]."','".$_post["password"]."','".$_post["email"]."','".$_post["firstname"]."','".$_post["lastname"]."','".$_post["phone"]."','".$_post["town"]."','".$_post["street"]."','".$_post["zip"]."','".$_post["country"]."','".$_post[$randomstring]."')"; if ($dbh->query($sql)) { echo "new record inserted successfully"; } else{ echo "data not inserted."; } $dbh = null; } catch(pdoexception $e) { echo $e->getmessage(); } $new = rowcount(); if ($new == 1) { $message['success'] = 'neuer benutzer (' . htmlspecialchars($_post['f']['username']) . ') wurde angelegt, <a href="login.php">weiter zur anmeldung</a>.'; header('location: http://' . $_server['http_host'] . '//activation.php'); // $empfaenger = $_post['f']['email']; // $betreff = "registration"; // $from = "from: webmaster <webmaster@somediashout.de>"; // $text = "thank registration. code : " + $randomstring; // mail($empfaenger, $betreff, $text, $from); $_session = array( 'login' => true, 'user' => array( 'username' => $row['username'] ) ); } else { } } } ?>
brabus85,
the first answer checks if error remaining or not. please replace below code on first answer.
this code contains total solution of problem.
<?php session_start(); if(isset($_post["submit"])){ $hostname='localhost'; $username='root'; $password=''; function generaterandomstring($length = 8) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; $characterslength = strlen($characters); $randomstring = ''; ($i = 0; $i < $length; $i++) { $randomstring .= $characters[rand(0, $characterslength - 1)]; } return $randomstring; } $randomstring = generaterandomstring(); if ($_post['password'] != $_post['password_again']) { $message['error'] = 'die eingegebenen passwörter stimmen nicht überein.'; } else { unset($_post['password_again']); $salt = ''; ($i = 0; $i < 22; $i++) { $salt .= substr('./abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', mt_rand(0, 63), 1); } $_post['password'] = crypt( $_post['password'], '$2a$10$' . $salt ); $new = 0; try { $dbh = new pdo("mysql:host=$hostname;dbname=loginsystem",$username,$password); $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); // <== add line $sql = "insert user2 (username, password, email, firstname, lastname, phone, town, street, zip, country, code) values ('".$_post["username"]."','".$_post["password"]."','".$_post["email"]."','".$_post["firstname"]."','".$_post["lastname"]."','".$_post["phone"]."','".$_post["town"]."','".$_post["street"]."','".$_post["zip"]."','".$_post["country"]."','".$randomstring."')"; if ($dbh->query($sql)) { echo "new record inserted successfully"; } else{ echo "data not inserted."; } $new = $dbh->lastinsertid(); $dbh = null; } catch(pdoexception $e) { echo $e->getmessage(); } if ($new > 0) { $message['success'] = 'neuer benutzer (' . htmlspecialchars($_post['f']['username']) . ') wurde angelegt, <a href="login.php">weiter zur anmeldung</a>.'; header('location: http://' . $_server['http_host'] . '//activation.php'); // $empfaenger = $_post['f']['email']; // $betreff = "registration"; // $from = "from: webmaster <webmaster@somediashout.de>"; // $text = "thank registration. code : " + $randomstring; // mail($empfaenger, $betreff, $text, $from); $_session = array( 'login' => true, 'user' => array( 'username' => $row['username'] )); } else { } } } ?>
let me know if face query/concern regarding this.
thanks!
Comments
Post a Comment