2015-05-19 146 views
0

我有一個小問題。腳本沒有插入數據[MySQL/PHP]

我做了一個PHP腳本,該腳本應該將數據插入到我的數據庫中,但可悲的是,它不起作用,它已經工作過。我現在正在主機上運行我的腳本,這可能是問題..但我不認爲它是。

代碼:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 

<?php 
$usernameErr = $emailErr = $passwordErr = $password_valErr = ""; 
$username = $email = $password = $password_val = ""; 

if($_SERVER['REQUEST_METHOD'] == 'POST') { 
    if(empty($_POST['username'])) { 
     $usernameErr = "Name is required"; 
    } else { 
     $username = validate_input($_POST['username']); 
     if(strlen($username) <= 3) { 
      $usernameErr = "Username must be 4 characters or longer."; 
     } 
     if(strlen($username) > 26) { 
      $usernameErr = "Username can't be longer as 26 characters."; 
     } 
     if(!preg_match("/^[a-zA-Z ]*$/", $username)) { 
      $usernameErr = "Only letters and white space allowed."; 
     }  
    } 

    if(empty($_POST['email'])) { 
     $emailErr = "Email is required"; 
    } else { 
     $email = validate_input($_POST['email']); 
     if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
      $emailErr = "Invalid email format."; 
     }   
    } 

    if(empty($_POST['password'])) { 
     $passwordErr = "Password is required"; 
    } else { 
     $password = validate_input($_POST['password']); 
     if(strlen($password) <= 5) { 
      $passwordErr = "Password must be 6 characters or longer."; 
     } 
     if(strlen($password) > 26) { 
      $passwordErr = "Password can't be longer as 26 characters."; 
     } 
     if(!preg_match("#[0-9]+#", $password)) { 
      $passwordErr = "Password must contain atleast 1 number."; 
     } 
    } 

    if(empty($_POST['password_val'])) { 
     $password_valErr = "Password_val is required"; 
    } else { 
     $password_val = validate_input($_POST['password_val']); 
     if($password_val != $password) { 
      $password_valErr = "Password_val must be equal to password."; 
     }  
    } 

    if($usernameErr == '' && $emailErr == '' && $passwordErr == '' && $password_valErr == '') { 

     $check_user = mysqli_query($conn, "SELECT * FROM users WHERE username='".trim($username)."'"); 
     $check_mail = mysqli_query($conn, "SELECT * FROM users WHERE email='".trim($email)."'"); 

     if(mysqli_num_rows($check_user) > 0) { 
      echo 'This username allready exists'; 
     } elseif(mysqli_num_rows($check_mail) > 0) { 
      echo 'This email address is already registered.'; 
     } else { 
      $username = mysql_real_escape_string(trim($username)); 
      $email  = mysql_real_escape_string(trim($email)); 
      $password = mysql_real_escape_string(trim($password)); 
      $rand_salt = randString(); 

      /*$final_pass = password_hash($password_val, PASSWORD_DEFAULT)."\n";*/ 
      $final_pass = sha1($password.PASSWORD_SALT.$rand_salt); 
      $privileges = 0; 

      $sql = "INSERT INTO users (username,password,salt,email) 
        VALUES ('".$username."','".$final_pass."','".$rand_salt."','".$email."')"; 

      if($conn->query($sql) === TRUE) { 
       echo "User registered."; 

      } else { 
       echo 'Error: ' . $sql . '<br>' . $conn->error; 
      } 
     } 
    } 
} 
?> 

<table border="1"> 
    <tr> 
     <td><label>Username</label><?=' <b>' . $usernameErr . '</b>';?></td> 
     <td><input type="text" name="username" value="<?=$username;?>" placeholder="Enter your desired username..." /></td> 
    </tr> 
    <tr> 
     <td><label>E-mail</label><?=' <b>' . $emailErr . '</b>';?></td> 
     <td><input type="text" name="email" value="<?=$email;?>" placeholder="Enter your email address..." /></td> 
    </tr> 
    <tr> 
     <td><label>Password<?=' <b>' . $passwordErr . '</b>';?></label></td> 
     <td><input type="password" name="password" placeholder="Enter your desired password..." /></td> 
    </tr> 
    <tr> 
     <td><label>Repeat Password<?=' <b>' . $password_valErr . '</b>';?></label></td> 
     <td><input type="password" name="password_val" placeholder="Repeat your chosen password.." /></td> 
    </tr> 
    <tr> 
     <td><input type="submit" name="register" value="Register" /></td> 
    </tr> 
</table> 
</form> 

此代碼應工作,但第一個問題是:

action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" 

此代碼將不得不送我去同一個頁面,register.php在這種情況下, ,但不知何故,它將我發送到index.php頁面。所以,如果我刪除此代碼,並保留採取行動空或剛進入register.php在那裏,得到的數據插入除用戶名和電子郵件這兩者都是VARCHAR處理..

我希望有人能幫助我,

謝謝!

+0

你在混合MySQL API /函數。不能這樣做。 –

+0

你也可以做'action =「」''和「self」一樣。 –

+0

你也很容易受到[sql注入攻擊](http://bobby-tables.com)的攻擊。 –

回答

1

您正在使用mysql_函數以及連接mysqli_ API。

你不能混合這些功能。您必須從連接中使用相同的MySQL API進行查詢。

  • 您需要更改的mysql_real_escape_string所有實例mysqli_real_escape_string($conn, $_POST['variable'])

  • variable是您的接線柱陣列。

另外,請確保你確實有mysqli_,而不是mysql_或PDO連接對這一問題。

  • 即使您的代碼確實包含多個mysqli_函數,您仍然不確定要連接哪個MySQL API。

添加錯誤報告給您的文件(S)的頂部您打開PHP的標記之後。

例如<?php error_reporting(E_ALL); ini_set('display_errors', 1);那麼你的代碼的其餘部分,看它是否得到任何東西, 以及or die(mysqli_error($conn))mysqli_query()


旁註:sha1()不被認爲是用於存儲散列最安全的方法。

從ircmaxell的回答中拉出https://stackoverflow.com/a/29778421/並使用PDO和準備好的陳述以及password_hash()

只需使用一個庫。認真。他們的存在是有原因的。

不要自己做。如果你創建你自己的。鹽,你這樣做是錯誤您應該使用一個處理一個庫,爲您

$dbh = new PDO(...); 

$username = $_POST["username"]; 
$email = $_POST["email"]; 
$password = $_POST["password"]; 
$hash = password_hash($password, PASSWORD_DEFAULT); 

$stmt = $dbh->prepare("insert into users set username=?, email=?, password=?"); 
$stmt->execute([$username, $email, $hash]); 

並會在登錄:。

$sql = "SELECT * FROM users WHERE username = ?"; 
$stmt = $dbh->prepare($sql); 
$result = $stmt->execute([$_POST['username']]); 
$users = $result->fetchAll(); 
if (isset($users[0]) { 
    if (password_verify($_POST['password'], $users[0]->password) { 
     // valid login 
    } else { 
     // invalid password 
    } 
} else { 
    // invalid username 
} 
+0

我不會做變量嗎?既然我已經宣佈了那些並給了那些$ _POST? – Peurr

+0

@Peurr你可以,但是你可以開放給SQL注入。 –

+0

但是,會發生什麼?既然我已經宣佈了變量?像這樣:$ email = [email protected],如果我在那之後再做mysqli_real_escape_string,它會起作用嗎? – Peurr