从html表单php pdo将数据插入数据库

时间:2014-01-12 20:00:51

标签: php mysql pdo

好吧所以昨天我问了一个类似的问题并被告知要学习pdo,然后我查找了如何使用pdo将数据插入数据库,并按照教程,我遇到了同样的问题。页面变为空白,没有任何内容添加到数据库..这是我的代码

register.php

    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="style2.css">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <body>
    <center><h1>Register</h2></center>
    <?php
    if ( empty( $_POST ) ){
    ?>
    <form name='registration' action="register.php" method="post">

     <label for 'user'>Desired Username:</label><br>
     <input type="text" name="User" />
     <br>
      <label for 'pass'>Password:</label><br>
     <input type="text" name="User" />
     <br>

     <label for 'birth'>Birthday (ex: 1992-11-11):</label><br>

     <input type="text" name="Birth" />
     <br>
     <label for 'email'>Email:</label><br>

     <input type="text" name="email" />
     <br>
     <label for 'catch'>Catch Phrase:</label><br>

      <textarea rows="2" cols="25" name="Catch" maxlength="10"></textarea>
     <br>
        <input type="submit" name="submit" value="submit">
 </form>
 <?php
  } else {
  $db_user = 'a3410999_dom';
 $db_pass = '*****';
 $db = new PDO( 'mysql:host=mysql4.000webhost.com;dbname=a3410999_members', $db_user,      $db_pass );

 $form = $_POST;
 $username = $form[ 'user' ];
 $password = $form[ 'pass' ];
 $birth = $form[ 'birth' ];
 $email = $form[ 'email' ];
 $catch = $form[ 'catch' ];

 $sql = "INSERT INTO members ( user, pass, birth, email, catch ) VALUES ( :username,   :password, :birth, :email, :catch )";

 $query = $db->prepare( $sql );
 $query->execute( array( ':username'=>$username, ':password'=>$password, ':birth'=>$birth,  ':email'=>$email, ':catch'=>$catch ) );

 } 
  ?>

  </body>
     </html>

2 个答案:

答案 0 :(得分:0)

首先,你有错误

 <label for 'user'>Desired Username:</label><br>
 <input type="text" name="User" />
 <br>
  <label for 'pass'>Password:</label><br>
 <input type="text" name="User" />
 <br>

您为“用户名”=名称设置名称,并为“密码”

设置相同的名称

答案 1 :(得分:0)

   // function for INSERTING DATA IN TABLES
   public function insert($t, $v, $r)
   {
      if (($t != null) && ($r != null) && ($v != null) && (count($v) == count($r)))
      {
         $instruction = 'INSERT INTO `' . DBNAME . '`.`' . $t . '` (';

         for ($i = 0; $i < count($r); $i++)
            $_r[$i] = '`' . $r[$i] . '`';
         $_r = implode(',', $_r);

         $instruction .= $_r . ') VALUES (';

         for ($i = 0; $i < count($r); $i++)
            $r[$i] = ':' . $r[$i];
         $r = implode(',', $r);

         $instruction .= $r . ');';

         $r = explode(',', $r);

         try {
            $statement = $this->PDO->prepare($instruction);

            for ($i = 0; $i < count($r); $i++)
               $statement->bindParam($r[$i], $v[$i]);

            if ($statement->execute())
               return true;
            else {
               echo("Error: " . $statement->errorCode());
               return false;
            }
         } catch (PDOException $e) {
            echo("Error: " . $e->getMessage());
            return false;
         }
      } else
         return false;
   }

$t = 'groups';
$v = array( your data );
$r = array( your fields );

if (insert($t, $v, $r))
   echo("ko");
else
   echo("ko");

这应该有用。