2017-09-18 8 views
0

PHPを使用してMySQLに新しいユーザーを追加するページがあります。そして、この問題は、実行するたびに新しいユーザーのIDを0に設定することです。だから、私はそれを望んでいません。私はそれが1のidから始まり、それがテーブルに存在するならば、2よりも多くのものを試してみたいです。!= 0のIDを持つMySQLテーブルへのデータの追加を開始する方法

私はこの例のために、あなたは私のPHPファイルを見てする必要はありませんが、あなたがしなければ、ここであなたがadmin_new.phpを見ることができるとは思わない:

<?php 
$notice = array(); 
if (isset($_POST['submit'])){ 
    $username = $_POST['uname']; 
    $email = $_POST['email']; 
    $password = $_POST['pass']; 
    $groups = $_POST['groups']; 
    if($groups == "Main Admin"){ 
     $level = 1; 
    }else if($groups == "Administrator"){ 
     $level = 2; 
    }else if($groups == "Content Creator"){ 
     $level = 3; 
    }else if($groups == "Social Media Manager"){ 
     $level = 4; 
    }else{ 
     $level = 5; 
    } 
    if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { 
     $notice['email'] = "The email that you have entered is not a valid one"; 
    }else{ 
     $registration = new Register(); 
     $registration->CheckUname($username,$email,$password,$groups,$level); 
    } 
} 
?> 
<div class="content-wrapper"> 
    <section class="content-header"> 
     <h1> 
      Add New Admin 
      <small>You can add new admin here</small> 
     </h1> 
     <ol class="breadcrumb"> 
      <li class="active">addnewadmin.php</li> 
     </ol> 
    </section> 
    <?php 
    if(isset($notice['validation_email'])) { 
     echo " 
      <div class='alert alert-danger'> 
       <strong>Hey!</strong> ".$notice['validation_email'].". 
      </div> 
     "; 
    } 
    if(isset($notice['username_exists'])) { 
     echo " 
      <div class='alert alert-danger'> 
       <strong>Hey!</strong> ".$notice['username_exists'].". 
      </div> 
     "; 
    } 
    if(isset($notice['email_exists'])) { 
     echo " 
      <div class='alert alert-danger'> 
       <strong>Hey!</strong> ".$notice['email_exists'].". 
      </div> 
     "; 
    } 
    if(isset($notice['success_message'])) { 
     echo " 
      <div class='alert alert-danger'> 
       <strong>Hey!</strong> ".$notice['success_message'].". 
      </div> 
     "; 
    } 
    ?> 
    <section class="content"> 
     <div class="row"> 
      <div class="col-md-6"> 
       <div class="box box-primary"> 
        <div class="box-header with-border"> 
         <h3 class="box-title">Required Information</h3> 
        </div> 
        <form role="form" method="POST" action=""> 
         <div class="box-body"> 
          <div class="form-group"> 
           <label>User name</label> 
           <input type="text" class="form-control" placeholder="Enter username" name="uname" required> 
          </div> 
          <div class="form-group"> 
           <label for="exampleInputEmail1">Email address</label> 
           <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email" name="email" required> 
          </div> 
          <div class="form-group"> 
           <label for="exampleInputPassword1">Temporary password</label> 
           <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Enter password" name="pass" required> 
          </div> 
          <div class="form-group"> 
           <label>Group admin</label> 
           <select class="form-control" name="groups"> 
            <option value="Main Admin">Main Admin</option> 
            <option value="Administrator">Administrator</option> 
            <option value="Content Creator">Content Creator</option> 
            <option value="Social Media Manager">Social Media Manager</option> 
            <option value="Analyst">Analyst</option> 
           </select> 
          </div> 
         </div> 
         <div class="box-footer"> 
          Visit <a href="https://zite.pouyavagefi.com/documentation/types.php">admin types</a> documentation to know the differences between each admin. 
         </div> 
         <div class="box-footer"> 
          <button name="submit" type="submit" class="btn btn-primary">Submit</button> 
         </div> 
        </form> 
       </div> 
      </div> 
     </div> 
    </section> 
</div> 

そして、ここではIクラスですRegister.class.phpと呼ばれる使用しています

<?php 
class Register 
{ 
    private $db; 
    public function __construct() 
    { 
     $this->db = new Connection(); 
     $this->db = $this->db->dbConnect(); 
    } 
    public function CheckUname($username,$email,$password,$groups,$level) 
    { 
     if(!empty($username)&&($email)) 
     { 
      $chk1 = $this->db->prepare("SELECT username FROM admins WHERE user_name= ?"); 
      $chk1->bindParam(1,$username); 
      $chk1->execute(); 
      if($chk1->rowCount() == 1) 
      { 
       $notice['username_exists'] = "Try different username"; 
       return $notice; 
      }else{ 
       $chk2 = $this->db->prepare("SELECT email FROM admins WHERE email_address= ?"); 
       $chk2->bindParam(1,$email); 
       $chk2->execute(); 
       if($chk2->rowCount() == 1) 
       { 
        $notice['email_exists'] = "The email address that you have entered is already exists in database"; 
        return $notice; 
       }else{ 
        $this->NewAdmin($username,$email,$password,$groups,$level); 
        $notice['success_message'] = "New admin was successfully added"; 
        return $notice; 
       } 
      } 
     } 
    } 
    public function NewAdmin($username,$email,$password,$groups,$level) 
    { 
     if(!empty($username)&&!empty($email)&&!empty($password)&&!empty($groups)&&!empty($level)) 
     { 
      $reg = $this->db->prepare("INSERT INTO admins (user_name, email_address, password_hash, group_admin, date_joined, admin_level) VALUES (?, ?, ?, ?, NOW(), ?)"); 
      $reg->bindParam(1,$username); 
      $reg->bindParam(2,$email); 
      $reg->bindParam(3,$password); 
      $reg->bindParam(4,$groups); 
      $reg->bindParam(5,$level); 
      $reg->execute(); 
     } 
    } 
} 
?> 
+0

「ID」には自動インクリメント列を使用するだけで、何も渡されません。 – sagi

答えて

0

実行mysqlの端末でこの称賛:

ALTER TABLE tablename MODIFY COLUMN id INT auto_increment PRIMARY KEY 

上記のコマンドはidauto_incrementprimary keyとします。したがって、挿入するすべての値に対して、auto_incrementは列idの新しい値を生成しますが、値を割り当てる必要はありません。