无法将用户注册详细信息插入数据库

时间:2017-06-06 07:29:14

标签: php

我无法在注册后将用户插入数据库,我已经完成了所有工作,但是没有成功,我真的非常需要帮助,请我知道我的脚本中有什么问题。

这是我的注册html脚本:

<span class='details' ><h1><bold>ENTER YOUR DETAILS HERE</bold></h1></span>

  <form action="trotle.php"  method="POST">            


              <div class="form-group" style="margin-left:50px;color:#000099; font-family:times;">
              <input type="hidden" class="form-control" name="id" placeholder="enter your id" required />
                </div><br>


               <div class="form-group" style="margin-left:50px;color:#000099; font-family:times;">

              <label>fullname</label>

                  <input type="text" class="form-control" name="fullname" placeholder="Enter your fullname" required />
                </div><br>

                <div class="form-group" style="margin-left:50px;color:#000099; font-family:times;">

              <label>username</label>

                  <input type="username" class="form-control" name="username" placeholder="Enter your username" required />
                </div><br>



                <div class="form-group" >
                   <label>password</label>

                 <input type="password" class="form-control" name="password"  placeholder="type in your password" required />
                </div>

              <br>
              <div class="form-group" style="margin-left:50px;color:#000099; font-family:times;" />
                <label>email</label>

                  <input type="email" class="form-control" name="email"  placeholder="Enter your email" required />
                </div><br>

                <div class="form-group" style="margin-left:50px;color:#000099; font-family:times;">
                <label>country</label>

                  <input type="text" class="form-control" name="country"  placeholder="Enter your country" required />
                </div><br>


                <div class="form-group" style="margin-left:50px;color:#000099; font-family:times;">
                <label>phone number</label>

                  <input type="number" class="form-control" name="phonenumber"  placeholder="Enter your phonenumber" required />
                </div><br>

                <div class="form-group" style="margin-left:50px;color:#000099; font-family:times;">
                <label>Alternative phone number</label>

                  <input type="number" class="form-control" name="alternativephonenumber"  placeholder="Enter your alternative phonenumber" required />
                </div><br>


              <div class="form-group"  style="margin-left:50px;color:#000099; font-family:times;">
                <label>referrer</label>
                  <input type="text" class="form-control" name="referrer" placeholder="type in who referred you here" required />
                </div><br>


              <div class="form-group"  style="margin-left:50px;color:#000099; font-family:times;"">
                <label>paymentmode</label>

                  <select name="paymentmode" class="form-control" required />
<option value="">Select your paymentmode</option>
<option value="bitcoin">bitcoin</option>
<option value="perfectmoney">perfectmoney</option>
<option value="naira">naira</option>

                  </select>

                </div>
                <br>

           <div class="form-group" style='text-align: center;padding-top: 30px;'>

              <input type="checkbox" style=" vertical-align: sub; margin-left: 120px;margin-bottom: -15px; " required><a href="homepage.php">  I agree with </a><a href='../terms' target="_blank">Terms and conditions</a><br>
              <input type="submit" class="btn btn-success" name="submit_signup" value='REGISTER' style="margin-left:145px;border:none; margin-bottom: -10px; background-color: green; font-weight: bold;border-style: dotted;">
              <a href="trotle.php"><input type="post" class="btn btn-success" value='LOGIN' style="margin-left:140px;background-color: green; font-weight: bold;border-style: dotted; border-color:black ;padding: 5px; size:20px; margin-bottom:-10px;"></a>
              </div>
              <br>
              <?php


              ?>
              </form>

我的用于将用户插入数据库的php脚本如下;

<?php
$id=$fullname=$username=$password=$email=$country= $phonenumber=$alternativephonenumber=$referrer=$paymentmode="";
if(isset($_POST) && array_key_exists('submit_signup',$_POST))   
{  
$id= $_POST['id'];
if(isset($_POST['id'])){
$id= $_POST['id'];  

}
$fullname=$_POST['fullname'];
if(isset($_POST['fullname'])){
$fullname= $_POST['fullname'];  
}
else{
    echo 'please enter your fullname';
}
$username= $_POST['username'];
if(isset($_POST['username'])){
$username= $_POST['username'];  
}
else{
    echo 'enter your username';
}
$password= $_POST['password'];
if(isset($_POST['password'])){
$password= $_POST['password'];  
}
else{
    echo 'enter your password';
}
$email= $_POST['email'];
if(isset($_POST['email'])){
$email= $_POST['email'];    
}
else{
    echo 'enter your email';
}
$country= $_POST['country'];
if(isset($_POST['country'])){
$country= $_POST['country'];    
}
else{
    echo 'enter your country';
}
$phonenumber= $_POST['phonenumber'];
if(isset($_POST['phonenumber'])){
$phonenumber= $_POST['phonenumber'];    
}
else{
    echo 'enter your phonenumber';
}
$alternativephonenumber= $_POST['alternativephonenumber'];
if(isset($_POST['alternativephonenumber'])){
$alternativephonenumber= $_POST['alternativephonenumber'];  
}
else{
    echo 'enter your alternativephonenumber';
}
$referrer= $_POST['referrer'];
if(isset($_POST['referrer'])){
$referrer= $_POST['referrer'];  
}
else{
    echo 'enter your referrer';
}
$paymentmode= $_POST['paymentmode']; 
if(isset($_POST['paymentmode'])){
$paymentmode= $_POST['paymentmode'];    
}
else{var_dump($_POST['id']);
    echo 'enter your paymentmode';
}
}
    else {
        echo 'nah';

    }

    $serverhost='localhost';
$serverdb='charles';
$serveruser='root';
$serverpassword='';




try{ 
    $connect= new PDO("mysql:host=$serverhost;dbname=charles;" , $serveruser, $serverpassword);
 $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     } 
      catch( PDOException $e)
        {
            echo $e->getmessage();
        }

     if($connect){
        echo 'database successful!';
     }


$sql="INSERT INTO users(id, fullname, username, password, email, country, phonenumber, alternativephonenumber, referrer, paymentmode)VALUES(:id, :fullname, :username, :password, :email, :country, :phonenumber, :alternativephonenumber, :referrer, :paymentmode)";

$query=$connect->prepare($sql);
$query->bindParam(':id', $id, PDO::PARAM_INT);
$query->bindParam(':fullname', $fullname, PDO::PARAM_STR);
$query->bindParam(':username', $username, PDO::PARAM_STR);
$query->bindParam(':password', $password, PDO::PARAM_STR);
$query->bindParam(':email', $email, PDO::PARAM_STR);
$query->bindParam(':country', $country, PDO::PARAM_STR);
$query->bindParam(':phonenumber', $phonenumber, PDO::PARAM_INT);
$query->bindParam(':alternativephonenumber', $alternativephonenumber, PDO::PARAM_INT);
$query->bindParam(':referrer', $referrer, PDO::PARAM_STR);
$query->bindParam(':paymentmode', $paymentmode, PDO::PARAM_STR);
$query->execute();





?>

2 个答案:

答案 0 :(得分:0)

像这样更改你的PHP脚本并尝试,如果你没有收到错误,那么你的验证可能是错误的

$id=$fullname=$username=$password=$email=$country= $phonenumber=$alternativephonenumber=$referrer=$paymentmode="";
if(isset($_POST['submit_signup']))  //simplified validation 
{  

if(isset($_POST['id'])){
$id= $_POST['id'];  

}

if(isset($_POST['fullname'])){
$fullname= $_POST['fullname'];  
}
else{
    echo 'please enter your fullname';
}

if(isset($_POST['username'])){
$username= $_POST['username'];  
}
else{
    echo 'enter your username';
}

if(isset($_POST['password'])){
$password= $_POST['password'];  
}
else{
    echo 'enter your password';
}

if(isset($_POST['email'])){
$email= $_POST['email'];    
}
else{
    echo 'enter your email';
}

if(isset($_POST['country'])){
$country= $_POST['country'];    
}
else{
    echo 'enter your country';
}

if(isset($_POST['phonenumber'])){
$phonenumber= $_POST['phonenumber'];    
}
else{
    echo 'enter your phonenumber';
}

if(isset($_POST['alternativephonenumber'])){
$alternativephonenumber= $_POST['alternativephonenumber'];  
}
else{
    echo 'enter your alternativephonenumber';
}

if(isset($_POST['referrer'])){
$referrer= $_POST['referrer'];  
}
else{
    echo 'enter your referrer';
}

if(isset($_POST['paymentmode'])){
$paymentmode= $_POST['paymentmode'];    
}
else{var_dump($_POST['id']);
    echo 'enter your paymentmode';
}
}
    else {
        echo 'nah';

    }

    $serverhost='localhost';
$serverdb='charles';
$serveruser='root';
$serverpassword='';




try{ 
    $connect= new PDO("mysql:host=$serverhost;dbname=$serverdb;" , $serveruser, $serverpassword);
 $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     } 
      catch( PDOException $e)
        {
            echo $e->getmessage();
        }

     if($connect){
        echo 'database successful!';
     }


$sql="INSERT INTO users(id, fullname, username, password, email, country, phonenumber, alternativephonenumber, referrer, paymentmode)VALUES(:id, :fullname, :username, :password, :email, :country, :phonenumber, :alternativephonenumber, :referrer, :paymentmode)";

$query=$connect->prepare($sql);
$query->bindParam(':id', $id, PDO::PARAM_INT);
$query->bindParam(':fullname', $fullname, PDO::PARAM_STR);
$query->bindParam(':username', $username, PDO::PARAM_STR);
$query->bindParam(':password', $password, PDO::PARAM_STR);
$query->bindParam(':email', $email, PDO::PARAM_STR);
$query->bindParam(':country', $country, PDO::PARAM_STR);
$query->bindParam(':phonenumber', $phonenumber, PDO::PARAM_INT);
$query->bindParam(':alternativephonenumber', $alternativephonenumber, PDO::PARAM_INT);
$query->bindParam(':referrer', $referrer, PDO::PARAM_STR);
$query->bindParam(':paymentmode', $paymentmode, PDO::PARAM_STR);
$query->execute();

所做的更改:

  1. 简化了验证
  2. 删除不必要的变量分配,如果您使用的是isset,请不要将其分配到isset
  3. 旁边
  4. 使用db name
  5. 的变量更新数据库名称

    最后建议,如果你回答验证错误,测试它没问题,否则保留一个变量并在适当的地方回显它。并在验证中使用插入查询代码块。

答案 1 :(得分:0)

使用css并使用类,id或父&gt;子选择器等分配样式,而不是不可管理的内联样式。

DIV元素不使用标记末尾的/(请参阅原始代码中的电子邮件部分)

没有输入类型POST<input type="post" class="btn btn-success" value='LOGIN'... />) - 可能按钮会更好?

id字段隐藏在HTML中,但可能是db使用auto-increment字段(主键),因此没有必要发送此值 - 用户之前不会知道这一点提交表格,没有什么可以阻止他们使用他们想要的任何号码,

<style>
    .form-group{margin-left:50px;color:#000099; font-family:times;}
    .form-control{}
    input[type='checkbox']{vertical-align: sub; margin-left: 120px;margin-bottom: -15px;}
    input[type='submit']{margin-left:145px;border:none; margin-bottom: -10px; background-color: green; font-weight: bold;border-style: dotted;}
    input[type='button']{margin-left:140px;background-color: green; font-weight: bold;border-style: dotted; border-color:black ;padding: 5px; size:20px; margin-bottom:-10px;}
</style>


<span class='details' >
   <h1>
      <bold>ENTER YOUR DETAILS HERE</bold>
   </h1>
</span>

<form action='trotle.php' method='POST'>

   <!-- HOW does a user enter a value here if it is HIDDEN??? Removed `required` attribute -->
   <div class='form-group'>
      <input type='hidden' class='form-control' name='id' placeholder='enter your id' />
   </div>
   <br>

   <div class='form-group'>
      <label>fullname</label>
      <input type='text' class='form-control' name='fullname' placeholder='Enter your fullname' required />
   </div>
   <br>
   <div class='form-group'>
      <label>username</label>
      <input type='username' class='form-control' name='username' placeholder='Enter your username' required />
   </div>
   <br>
   <div class='form-group' >
      <label>password</label>
      <input type='password' class='form-control' name='password' placeholder='type in your password' required />
   </div>
   <br>
   <div class='form-group'>
      <label>email</label>
      <input type='email' class='form-control' name='email'  placeholder='Enter your email' required />
   </div>
   <br>
   <div class='form-group'>
      <label>country</label>
      <input type='text' class='form-control' name='country' placeholder='Enter your country' required />
   </div>
   <br>
   <div class='form-group'>
      <label>phone number</label>
      <input type='number' class='form-control' name='phonenumber' placeholder='Enter your phonenumber' required />
   </div>
   <br>
   <div class='form-group'>
      <label>Alternative phone number</label>
      <input type='number' class='form-control' name='alternativephonenumber' placeholder='Enter your alternative phonenumber' required />
   </div>
   <br>
   <div class='form-group'>
      <label>referrer</label>
      <input type='text' class='form-control' name='referrer' placeholder='type in who referred you here' required />
   </div>
   <br>
   <div class='form-group'>
      <label>paymentmode</label>
      <select name='paymentmode' class='form-control' required />
         <option value=''>Select your paymentmode</option>
         <option value='bitcoin'>bitcoin</option>
         <option value='perfectmoney'>perfectmoney</option>
         <option value='naira'>naira</option>
      </select>
   </div>
   <br>
   <div class='form-group'>
      <!-- this checkbox needs a name!! Assign name `terms` -->
      <input type='checkbox' name='terms' required />
      <a href='homepage.php'>  I agree with </a><a href='../terms' target='_blank'>Terms and conditions</a>
      <br>
      <input type='submit' class='btn btn-success' name='submit_signup' value='REGISTER' />
      <a href='trotle.php'>
        <input type='button' class='btn btn-success' value='LOGIN' />
      </a>
   </div>
   <br>
</form>





<?php

    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['submit_signup'] ) ){
        /* id should be an auto-increment field in the db */
        $id = isset( $_POST['id'] ) ? $_POST['id'] : false;
        $fullname = isset( $_POST['fullname'] ) ? $_POST['fullname'] : false;
        $username = isset( $_POST['username'] ) ? $_POST['username'] : false;
        $password = isset( $_POST['password'] ) ? $_POST['password'] : false;
        $email = isset( $_POST['email'] ) ? $_POST['email'] : false;
        $country = isset( $_POST['country'] ) ? $_POST['country'] : false;
        $phonenumber = isset( $_POST['phonenumber'] ) ? $_POST['phonenumber'] : false;
        $alternativephonenumber = isset( $_POST['alternativephonenumber'] ) ? $_POST['alternativephonenumber'] : false;
        $referrer = isset( $_POST['referrer'] ) ? $_POST['referrer'] : false;
        $paymentmode = isset( $_POST['paymentmode'] ) ? $_POST['paymentmode'] : false;




        $errors=array();
        if( !$fullname )$errors[]='please enter your fullname';
        if( !$username )$errors[]='enter your username';
        if( !$password )$errors[]='enter your password';
        if( !$email )$errors[]='enter your email';
        if( !$country )$errors[]='enter your country';
        if( !$phonenumber )$errors[]='enter your phonenumber';
        if( !$alternativephonenumber )$errors[]='enter your alternativephonenumber';
        if( !$referrer )$errors[]='enter your referrer';
        if( !$paymentmode )$errors[]='enter your paymentmode';


        if( !empty( $errors ) ){

            /* display any errors with form submission */
            foreach( $errors as $error ){
                echo $error . '<br />';
            }

        } else {

            try{

                $serverhost='localhost';
                $serverdb='charles';
                $serveruser='root';
                $serverpassword='';


                $connect=new PDO("mysql:host={$serverhost};dbname={$serverdb};" , $serveruser, $serverpassword);
                $connect->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );


                if( $connect ){

                    $sql="insert into `users`
                            ( `fullname`, `username`, `password`, `email`, `country`, `phonenumber`, `alternativephonenumber`, `referrer`, `paymentmode` )
                            values
                            ( :fullname, :username, :password, :email, :country, :phonenumber, :alternativephonenumber, :referrer, :paymentmode )";

                    $stmt=$connect->prepare( $sql );

                    if( $stmt ){

                        $stmt->bindParam(':fullname', $fullname, PDO::PARAM_STR);
                        $stmt->bindParam(':username', $username, PDO::PARAM_STR);
                        $stmt->bindParam(':password', $password, PDO::PARAM_STR);
                        $stmt->bindParam(':email', $email, PDO::PARAM_STR);
                        $stmt->bindParam(':country', $country, PDO::PARAM_STR);
                        $stmt->bindParam(':phonenumber', $phonenumber );
                        $stmt->bindParam(':alternativephonenumber', $alternativephonenumber );
                        $stmt->bindParam(':referrer', $referrer, PDO::PARAM_STR);
                        $stmt->bindParam(':paymentmode', $paymentmode, PDO::PARAM_STR);

                        $result = $stmt->execute();
                        echo $result ? 'success' : 'fail';
                    }
                }
            } catch( PDOException $e ) {
                echo $e->getMessage();
            }
        }
    }

?>