title: Insert form value into mysql database with pdo
body: <p>Hi I changed my mysql querys into pdo. Now the problem is that I havent any experience with pdo. I edit it how I thought but nothing works. No errors are showing. Can anyone find the mistake? Thanks for reading.</p>
  
  <pre><code>    &lt;?php
       session_start();
  
      if(isset($_POST["submit"])){
  
      $hostname='localhost';
      $username='root';
      $password='';
  
  
  function generateRandomString($length = 8) {
      $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
      $charactersLength = strlen($characters);
      $randomString = '';
      for ($i = 0; $i &lt; $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 = ''; 
              for ($i = 0; $i &lt; 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-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // &lt;== add this line
              $sql = "INSERT INTO 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-&gt;query($sql)) {
              echo "New Record Inserted Successfully";
              }
              else{
              echo "Data not successfully Inserted.";
              }
  
              $dbh = null;
              }
          catch(PDOException $e)
                      {
              echo $e-&gt;getMessage();
              }
  
              $new = rowCount();
              if ($new == 1) {
                  $message['success'] = 'Neuer Benutzer (' . htmlspecialchars($_POST['f']['username']) . ') wurde angelegt, &lt;a href="login.php"&gt;weiter zur Anmeldung&lt;/a&gt;.';
                  header('Location: http://' . $_SERVER['HTTP_HOST'] . '//activation.php');
  
                  // $empfaenger = $_POST['f']['email'];
                  // $betreff = "Registration";
                  // $from = "From: Webmaster &lt;webmaster@somediashout.de&gt;";
                  // $text = "Thank you for your registration. Your code is : " + $randomString;
  
                  // mail($empfaenger, $betreff, $text, $from);
  
  
  
                      $_SESSION = array(
                          'login' =&gt; true,
                          'user'  =&gt; array(
                              'username'  =&gt; $row['username']
                          )
                      );
  
              } else {
  
              }
  
          }
      } 
  ?&gt;
  </code></pre>