2017-09-23 46 views
-1

這是我的第一個問題。我希望我能找到答案。 正如標題所說,我無法添加使用我在html中創建的表單的用戶,並且我無法將使用php函數的用戶添加到數據庫中。無法在mysql數據庫中使用php表單添加用戶

db.php中

<?php 
$db['db_host'] = 'localhost'; 
$db['db_user'] = 'root'; 
$db['db_pass'] = ''; 
$db['db_name'] = 'cms'; 
foreach($db as $key => $value){ 
define(strtoupper($key),$value); 
} 

$con = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME); 

if (mysqli_connect_errno()) 
{ 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
//you need to exit the script, if there is an error 
exit(); 
} 

?> 
add-user.php 
    <?php 
    require_once('inc/top.php'); 
    if(!isset($_SESSION['username'])){ 
    header('Location:login.php'); 
    } 

    else if(isset($_SESSION['username']) and $_SESSION['role'] =='author'){ 
    header('Location:index.php'); 
    } 
    ?> 
    </head> 
    <body> 
    <div id="wrapper"> 
        <?php require_once('inc/header.php');?> 
     <div class="container-fluid body-section"> 
      <div class="row"> 
       <div class="col-md-3"> 
            <?php require_once('inc/sidebar.php');?> 
       </div> 
       <div class="col-md-9"> 
        <h1><i class="fa fa-user-plus"></i> Add User<small>Add 
       New User</small></h1> 
          <hr> 
          <ol class="breadcrumb"> 
           <li><a href="#"><i class="fa fa-tachometer"> 
     </i> Dashboard</a></li> 
          <li class="active"><i class="fa fa-user-plus"> 
    </i>Add New User</li> 
          </ol> 

          <?php 
        if(isset($_POST['submit'])){ 
         $date=time(); 
         $first_name = mysqli_real_escape_string($con,$_POST['first-name']); 
         $last_name = mysqli_real_escape_string($con,$_POST['last-name']); 
         $username = mysqli_real_escape_string($con,strtolower($_POST['username'])); 
         $username_trim=preg_replace('/\s*/','',$username); 
         $email = mysqli_real_escape_string($con,strtolower($_POST['email'])); 
         $password = mysqli_real_escape_string($con,$_POST['password']); 
         $role = $_POST['role']; 
         $image = $_FILES['image']['name']; 
         $image_tmp = $_FILES['image']['tmp_name']; 

         $check_query="SELECT * FROM users WHERE username='$username' or email='$email'"; 
         $check_run=mysqli_query($con,$check_query); 
         $salt_query="SELECT * FROM users ORDER BY id LIMIT 1"; 
         $salt_run=mysqli_query($con,$salt_query); 
         $salt_row=mysqli_fetch_array($salt_run); 
         $salt=$salt_row['salt']; 
         $password=crypt($password, $salt); 

         if(empty($first_name) or empty($last_name) or empty($username) or empty($email) or empty($password) or empty($image)) { 
          $error="All field Required"; 

         } 
         else if($username!=$username_trim){ 
          $error="Don't use spaces in username"; 

         } 
         else if(mysqli_num_rows($check_run)){ 
          $error="Username or Email Already Exist"; 
         } 
         else{ 

          $insert_query="INSERT INTO `users` (`id`, `date`, `first_name`, `last_name`, `username`, `email`, `image`, `password`, `role`) VALUES (NULL, NULL', '$first_name', '$last_name', '$username', '$email', '$image', '$password', '$role')"; 
          if(mysqli_query($con,$insert_query)){ 
           $msg="User has been Added"; 
           move_uploaded_file($image_tmp,"img/$image"); 

          } 
          else{ 
           $error="user has not been Added"; 
          } 
         } 
        } 

        ?> 
        <div class="row"> 
         <div class="col-md-8"> 
          <form action="" method="post" enctype="multipart/form-data"> 
           <div class="form-group"> 
            <label for="first-name">First Name:*</label> 
            <?php 
            if(isset($error)){ 
             echo "<span class='pull-right' style='color:red;'>$error</span>"; 
            } 
            else if(isset($msg)){ 
             echo "<span class='pull-right' style='color:green;'>$msg</span>"; 
            } 

            ?> 
            <input type="text" name="first-name" value="<?php if(isset($first_name)){ echo $first_name;}?>" id="first-name" class="form-control" placeholder="First Name"> 
           </div> 

           <div class="form-group"> 
            <label for="last-name">Last Name:*</label> 
            <input type="text" name="last-name" value="<?php if(isset($last_name)){ echo $last_name;}?>" id="last-name" class="form-control" placeholder="Last Name"> 
           </div> 

           <div class="form-group"> 
            <label for="username">Username:*</label> 
            <input type="text" name="username" id="username" value="<?php if(isset($username)){ echo $username;}?>" class="form-control" placeholder="Username"> 
           </div> 

           <div class="form-group"> 
            <label for="email">Email:*</label> 
            <input type="text" name="email" id="email" value="<?php if(isset($email)){ echo $email;}?>" class="form-control" placeholder="Email Address"> 
           </div> 

           <div class="form-group"> 
            <label for="Password">Password:*</label> 
            <input type="password" name="password" id="password" class="form-control" placeholder="Password"> 
           </div> 

           <div class="form-group"> 
            <label for="role">Role:*</label> 
            <select name="role" id="role" class="form-control"> 
            <option value="author">Author</option> 
            <option value="admin">Admin</option> 
            </select> 
           </div> 

           <div class="form-group"> 
            <label for="image">Profile Picture:*</label> 
            <input type="file" name="image" id="image"> 
           </div> 



           <input type="submit" value="Add User" name="submit" class="btn btn-primary"> 

          </form> 


         </div> 
         <div class="col-md-4"> 
          <?php 
          if(isset($check_image)){ 

           echo "<img src='img/$check_image' width='100%'>"; 
          } 

          ?> 

         </div> 
        </div>  

       </div> 
      </div> 
     </div> 
      <?php require_once('inc/footer.php');?> 

我已經做了很多的搜索,但沒有找到它的工作代碼,並添加用戶。每當我嘗試添加一個用戶作爲「作者」或「管理員」它說「用戶沒有被添加」。 在此先感謝

+0

你能解決它請提供您收到的錯誤? – pinoyCoder

+0

錯誤是「用戶未被添加」 –

+0

嘗試將mysqli錯誤添加到您的字符串以查看錯誤。 '''$ error =「用戶沒有被添加」。 mysqli_error($ con);''' – mirsch

回答

1

請嘗試此查詢您插入:

INSERT INTO `users` (`date`, `first_name`, `last_name`, `username`, `email`, `image`, `password`, `role`) VALUES (NULL, '$first_name', '$last_name', '$username', '$email', '$image', '$password', '$role'); 
+0

得到了這個添加INSERT INTO後的錯誤 「user has not beed添加列'id'不能爲空' –

+0

好吧,你有一個語法錯誤,現在你得到不同的錯誤。讓我更新,它將工作 –

+0

如果您定義了id列「auto_increment」,那麼您不應該在插入查詢中設置id。 –

0

通過刪除ID和日期,因爲它是AUTO_INCREMENT,它不應該被使用到INSERT INTO功能

相關問題