2016-10-12 407 views
1

嘿那裏對不起,如果這個問題可能已被問太多,但沒有一個問題,我在網站上看到的幫助我,並作爲標題指出,我有一個插入查詢會爲我正在創建的網站創建一個帳戶,但是當我按下提交按鈕時它什麼都不會做......只需刷新頁面即可。PHP插入查詢不起作用,並沒有給出任何錯誤

注:包括數據庫。 這部分網站包含在signup.php和signup.php中,包含連接文件。 經過測試,我可以從connect.php獲取,所以我不認爲這是問題所在。 即使這並不能證明爲什麼代碼不會始終運行。

故障排除和測試後,我想通了,通過去除這部分代碼:

 if (
     $username_error_message_status = false 
     and $password_error_message_status = false 
     and $email_error_message_status = false 
     and $bot_check_error_message_status = false 
    ) { 

代碼工作尚未插入查詢語句沒有。所以任何形式的幫助將不勝感激,並感謝您的閱讀。

全碼:

<?php 
    $sign_up_button = isset($_POST["sign_up_button"]); 

    $username_error_message = $password_error_message = $email_error_message = $bot_check_error_message = ""; 
    $username_error_message_status = $password_error_message_status = $email_error_message_status = $bot_check_error_message_status = ""; 
    $username_field = $email_field = ""; 

    if ($sign_up_button) { 

     $username_field = $_POST["sign_up_username"]; 
     $password_field = $_POST["sign_up_password"]; 
     $email_field = $_POST["sign_up_email"]; 
     $bot_check_field = isset($_POST["sign_up_bot_check"]); 

     if ($_SERVER["REQUEST_METHOD"] == "POST") { 

      if (empty($username_field)) { 
       $username_error_message_status = true; 
       $username_error_message = "This field is required."; 
      } 
      else { 
       $username_error_message_status = false; 
      } 

      if (empty($password_field)) { 
       $password_error_message_status = true; 
       $password_error_message = "This field is required."; 
      } 
      else { 
       $password_error_message_status = false; 
      } 

      if (empty($email_field)) { 
       $email_error_message_status = true; 
       $email_error_message = "This field is required."; 
      } 
      else { 
       $email_error_message_status = false; 
      }  

      if ($bot_check_field < 100) { 
       $bot_check_error_message_status = true; 
       $bot_check_error_message = "Please slide the slider to the end."; 
      } 
      else { 
       $bot_check_error_message_status = false; 
      }   

     } 
     if (
      $username_error_message_status = false 
      and $password_error_message_status = false 
      and $email_error_message_status = false 
      and $bot_check_error_message_status = false 
     ) { 
      /* --- --- --- --- --- --- --- --- Check username availability --- --- --- --- --- --- --- --- */ 

      $check_username_query = "SELECT `Username` FROM `users` WHERE `Username` = '$username_field' "; 

      $check_username_query_result = mysqli_query($GLOBALS["___mysqli_ston"], $check_username_query); 
      $numrows_check_username_query_result = mysqli_num_rows($check_username_query_result); 
      if ($numrows_check_username_query_result == 1) { 
       $username_availability_status = false; 
      ?> 
       <p><span class = "sign-up-error-tag">That username already exists in our database. Please choose another.</span></p> 
      <?php 
      } 
      else { 
       $username_availability_status = true; 
      } 

      /* --- --- --- --- --- --- --- --- Check email availability --- --- --- --- --- --- --- --- */ 

      $check_email_query = "SELECT `Email` FROM `users` WHERE `Email` = '$email_field' "; 

      $check_email_query_result = mysqli_query($GLOBALS["___mysqli_ston"], $check_email_query); 
      $numrows_check_email_query_result = mysqli_num_rows($check_email_query_result); 
      if ($numrows_check_email_query_result == 1) { 
       $email_availability_status = false; 
      ?> 
       <p><span class = "sign-up-error-tag">That email already exists in our database. Please choose another.</span></p> 
      <?php 
      } 
      else { 
       $email_availability_status = true; 
      } 

      /* --- --- --- --- --- --- --- --- Check email validity --- --- --- --- --- --- --- --- */ 

      if (filter_var($email_field, FILTER_VALIDATE_EMAIL)) { 
       $email_validity_status = true; 
      } 
      else { 
       $email_validity_status = false; 
      ?> 
       <span class = "sign-up-error-tag">That email is invalid. Please choose another.</span> 
      <?php 
      } 

      /* --- --- --- --- --- --- --- --- Hashing password --- --- --- --- --- --- --- --- */ 

      function better_crypt($input, $rounds = 7) { 
      $salt = ""; 
      $salt_chars = array_merge(range('A','Z'), range('a','z'), range(0,9)); 
      for($i=0; $i < 22; $i++) { 
      $salt .= $salt_chars[array_rand($salt_chars)]; 
      } 
      return crypt($input, sprintf('$2a$%02d$', $rounds) . $salt); 
      } 
      $password_field_hash = better_crypt($password_field); 
      $password_field_hash = better_crypt($password_field, 10); 
      $password_field_hash = better_crypt($password_field, 15);  

      /* --- --- --- --- --- --- --- --- Create account in database --- --- --- --- --- --- --- --- */ 

      $sign_up_date = date("Y-m-d h:i:sa"); 
      $sign_up_code = rand(); 
      $sign_up_active_status = "0"; 

      $create_account_query = mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO `users`(`Username`, `Email`, `Password`, `RegisterCode`, `Date`, `ActiveStatus`) VALUES ('$username_field', '$email_field', '$password_field_hash', '$sign_up_code', '$sign_up_date', '$sign_up_active_status')"); 

      /* --- --- --- --- --- --- --- --- Send activation email --- --- --- --- --- --- --- --- */ 

      $email_to = $email_field; 
      $WebsiteName = "ForTheHodor"; 
      $email_message = " 
      <table width='100%' border='0' cellspacing='0' cellpadding='3'> 
       <tr> 
       <td colspan=2><b>".$WebsiteName."</b></td> 
       </tr> 
       <tr> 
       <td colspan=2>Thank you for creating an account.</td> 
       </tr> 
       </table> 
       <hr><h3>Account info</h3><hr> 
       <table width='100%' border='0' cellspacing='0' cellpadding='3'> 
       <tr> 
       <td width='28%' >Username : </td> 
       <td width='72%'>".$username_field."</td> 
       </tr> 
       <tr> 
       <td>Email : </td> 
       <td>".$email_field."</td> 
       </tr> 
      </table> 
      <p>".$WebsiteName." advise you not to delete this email for it contains informations that could be in use later.</p> 
       <hr><h3>Account activation</h3><hr> 
       <p>If you wish to activate your account please use the following link : </p> 
       <p><a href = 'localhost/SignalShare/activate.php' style = 'text-decoration: none; color: blue;' >Activate account</a></p> 
       <p>Activiation code :</p> 
       <p>".$sign_up_code."</p> 
       <hr> 
       <p>If this email does not concern you please ignore it.</p> 
       <p>All regards.</p> 
       <p>".$WebsiteName."</p> 
       "; 
      $email_subject = "Accout activation";        
      $email_headers = "From:".$WebsiteName."\r\n"; 
      $email_headers .= "Content-type: text/html\r\n"; 

      mail($email_to, $email_subject, $email_message, $email_headers);  

      echo $email_message;  

     } 

    } 
?> 
<div id = "SignupForm"> 
    <div class="panel panel-default"> 
     <div class="panel-heading"> 
      <h3 class="panel-title">Sign up</h3> 
     </div> 
     <div class="panel-body"> 
      <span> 
       <form method = "POST" action= "signup.php"> 
        <table class = "table table-borderless"> 
         <thead> 
          <tr> 
           <th></th> 
           <th></th> 
          </tr> 
         </thead> 
         <tbody>     
          <tr> 
           <td> 
            <span class = "Sign-up-tag">Username</span> 
           </td> 
           <td> 
            <input type = "text" name = "sign_up_username" class = "sign-up-field" value = "<?php echo $username_field; ?>"></input> 
           </td> 
          </tr> 
          <tr> 
           <td> 

           </td> 
           <td> 
            <span class = "sign-up-error-tag"><b><?php echo $username_error_message; ?></b></span> 
           </td> 
          </tr> 
          <tr> 
           <td> 
            <span class = "Sign-up-tag">Password</span> 
           </td> 
           <td> 
            <input type = "password" name = "sign_up_password" class = "sign-up-field"></input> 
           </td> 
          </tr> 
          <tr> 
           <td> 

           </td> 
           <td> 
            <span class = "sign-up-error-tag"><b><?php echo $password_error_message; ?></b></span> 
           </td> 
          </tr> 
          <tr> 
           <td> 
            <span class= "Sign-up-tag">Email</span> 
           </td> 
           <td> 
            <input type = "text" name = "sign_up_email" class = "sign-up-field" value = "<?php echo $email_field; ?>"></input> 
           </td> 
          </tr> 
          <tr> 
           <td> 

           </td> 
           <td> 
            <span class = "sign-up-error-tag"><b><?php echo $email_error_message; ?></b></span> 
           </td> 
          </tr> 
          <tr> 
           <td> 
            <span class= "Sign-up-tag">Bot check</span> 
           </td> 
           <td> 
            <input id= "defaultSlider" type="range" min="0" max="100" value = "0" name = "sign_up_bot_check"/> 
           </td> 
          </tr> 
          <tr> 
           <td> 

           </td> 
           <td> 
            <span class = "sign-up-error-tag"><b><?php echo $bot_check_error_message; ?></b></span> 
           </td> 
          </tr> 
          <tr> 
           <td> 

           </td> 
           <td> 
            <input type = "submit" name = "sign_up_button" class="btn btn-success" value = "Sign up"></input> 
           </td> 
          </tr> 
         </tbody> 
        </table> 
       </form>    
      </span> 
     </div> 
    </div> 
</div> 

表瑪:

-- phpMyAdmin SQL Dump 
-- version 4.5.5.1 
-- http://www.phpmyadmin.net 
-- 
-- Host: localhost 
-- Generation Time: Oct 12, 2016 at 07:59 AM 
-- Server version: 5.7.11 
-- PHP Version: 5.6.19 

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; 
SET time_zone = "+00:00"; 


/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; 
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; 
/*!40101 SET @[email protected]@COLLATION_CONNECTION */; 
/*!40101 SET NAMES utf8mb4 */; 

-- 
-- Database: `forthehodor` 
-- 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `users` 
-- 

CREATE TABLE `users` (
    `ID` int(11) NOT NULL, 
    `Username` varchar(32) NOT NULL, 
    `Email` varchar(45) NOT NULL, 
    `Password` varchar(32) NOT NULL, 
    `RegisterCode` varchar(50) NOT NULL, 
    `Date` varchar(50) NOT NULL, 
    `ActiveStatus` int(11) NOT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

-- 
-- Indexes for table `users` 
-- 
ALTER TABLE `users` 
    ADD PRIMARY KEY (`ID`); 

-- 
-- AUTO_INCREMENT for dumped tables 
-- 

-- 
-- AUTO_INCREMENT for table `users` 
-- 
ALTER TABLE `users` 
    MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2; 
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; 
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; 
/*!40101 SET [email protected]_COLLATION_CONNECTION */; 
+0

分享你的表('用戶')模式嗎? – C2486

+0

編輯..也許添加connect.php以及如果它有幫助? @Rishi – Jad

+0

不能真正遵循這個邏輯,但你可能應該使用預先準備的語句,而不是直接在插入/選擇查詢中的變量,因爲這會讓你打開SQL注入... –

回答

1

你如果查詢應該如下刪除:

if (
    $username_error_message_status == false 
    and $password_error_message_status == false 
    and $email_error_message_status == false 
    and $bot_check_error_message_status == false 
) { 

您需要使用雙等號(==)不如果要比較值,則在if語句中使用單個(=)。

+0

感謝您的答案..在我的舊代碼我曾經把單(=),但由於某種原因,即使代碼現在運行的數據沒有插入...但我想這是一部分完成。 – Jad

相關問題