2016-02-05 114 views
-8

我正在使用php註冊表單,但是當我點擊註冊按鈕時,它會在電子郵件和密碼字段中保存1,1而不是給定的電子郵件和密碼。php數據庫數據保存

<?php 
include("connection.php") 
?> 
<?php 

$email=isset($_POST['email']); 
$password=isset($_POST['password']); 

if(isset($_POST['register'])) 
{ 
    $q= "insert into admin (email, password) values ('$email', '$password')"; 
    $qr=mysqli_query($con, $q); 
    if($qr) 
    { 
     echo "data added sucessfully"; 
    } 
    else 
    { 
     die(); 
    } 
} 

?> 

<!DOCTYPE html> 
<html> 
<head> 
    <title>Log in</title> 
</head> 
<body> 
<form method="post" action=""> 
<input type="email" name="email"> 
<br> 
<input type="password" name="password"> 
<br> 
<button type="submit" name="register" value="register">register</button> 
</form> 
</body> 
</html> 

+1

http://php.net/manual/en/function.isset.php,isset函數返回一個布爾值,更改爲if(isset($ _ POST ['email'])){$ email = $ _ POST ['email'];}編輯:張貼爲答案 –

+0

[您的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql -injection-in-php) –

+0

請使用PHP的[內置函數](http://jayblanchard.net/proper_password_hashing_with_PHP.html)來處理密碼安全性。如果您使用的PHP版本低於5.5,則可以使用'password_hash()'[兼容包](https://github.com/ircmaxell/password_compat)。 –

回答

1

http://php.net/manual/en/function.isset.php

Isset函數返回一個布爾值,嘗試:

if(isset($_POST['email'])){ 
    $email=$_POST['email']; 
} 
if(isset($_POST['password'])){ 
    $password=$_POST['password']; 
} 
+0

或者你可以使用tenary操作符:'$ email =(isset($ _ POST ['email'])?$ _POST ['email']:「」);',因爲當沒有設置所有字段時會發生什麼? *未定義的變量* – Qirel

+0

這不是你做錯誤處理的方式...... –

2

爲什麼它進入1原因「在你的數據庫s是因爲isset()的的爲POST數組。

旁註:您的意思是不是使用三元運算符http://php.net/manual/en/language.operators.comparison.php

RTM:http://php.net/manual/en/function.isset.phpbool isset (mixed $var [, mixed $... ])返回boolean。

$email=isset($_POST['email']); 
$password=isset($_POST['password']); 

您需要刪除isset()

$email=$_POST['email']; 
$password=$_POST['password']; 

然後檢查它們是不是空的。

if(!empty($_POST['email']) && !empty($_POST['password'])) 

,放在裏面if(isset($_POST['register'])){...}

,我不知道爲什麼你將代碼嵌入到textarea的。
編輯:我看到你在編輯中刪除它。

還要確保您的列類型能夠存儲字符串而不是整數,並且足夠長以容納正在存儲的數據。

您現在的代碼對SQL injection開放。使用prepared statementsPDOprepared statements


密碼

我也注意到,你可能會採用明文存儲密碼。如果您打算繼續使用此功能,建議不要這樣做。下面的

用途之一:

其他鏈接:

有關列長度重要旁註:

如果當你決定使用password_hash()或地下室,重要的是要注意,如果你現在的密碼列的長度是低於60的任何東西,它將需要改變爲(或更高)。本手冊建議長度爲255.

您需要更改列的長度,並使用新散列重新開始以使其生效。否則,MySQL將無聲無息地失敗。


腳註:

您也應該檢查針對您的查詢錯誤。

和錯誤報告是另一個,這將幫助你在這裏。

0

我想應該是類似下面

$電子郵件= isset($電子郵件)? $ email:「」 $ password = isset($ password)? $ password:「」