2011-12-08 5 views
0
나는 그에서 PHP에 문제가 있어요

를 추가하면 MySQL 데이터베이스에 데이터를 추가합니다PHP 및 MySQL 양식. 빈 행

<?php require("config/connection.php"); ?> 
<?php require("includes/functions.php"); ?> 

<?php 
$username = $_POST['username']; 
$password = $_POST['password']; 

$error='';// zmienna do błędów 

if(isset($_POST[submit])){ 



if(trim($_POST[username])=='' || strlen(trim($_POST[username])) < 6 ||strlen(trim($_POST[username])) >12){ 


    $error.="Name must be between 6 ad 12 chars<br />"; 

} 

}if($error==''){ // if no error, do a query 

    $sql = @mysql_query("INSERT INTO users SET username=\"$username\", password=\"$password\""); 




} 

    else { 

    echo "<span style=color:red>$error</span>"; 

    } 

    mysql_close($connection); 

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<h2>Add user</h2> 

<form action="add.php" method="post"> 

<p>Name: <input type="text" name="username" id="username" /></p> 
<p>Password: <input type="text" name="password" id="password" /></p> 
<p> <input type="submit" value="submit " name="submit" /></p> 

</form> 

<a href="index.php">Cancel</a> 

</body> 
</html> 

가장 큰 문제는 내가 입력 사용자 이름과 여기에 암호가 내가 무엇을 얻을 때이다 :

http://i.padsbanger.pl/img/cdf5b5p988ga

알 수없는 이유 때문에 빈 행을 추가 한 다음 trully이 내 양식에 입력 한 내용을 추가합니다. 어떤 도움이 필요합니까?

+1

해야 제출,하지만 당신은 PDO를 사용하여 데이터베이스를 보호해야하는 및 매개 변수화 된 쿼리. 데이터를 가져 오거나 게시하여 쿼리에 넣지 마십시오. –

+2

As @TimG added -> [SQL injection] (http://php.net/manual/en/security.database.sql-injection.php) – ManseUK

+0

이 부분을 읽으십시오. 암호. 이 코드는 편집/삭제 버튼을 인쇄하지 않습니다. – Halcyon

답변

4

한 번 페이지로드, 두 번 쿼리를 실행 한 다음 다시

$error=''; 
if(isset($_POST[submit])){ 
if(trim($_POST[username])=='' || strlen(trim($_POST[username])) < 6 ||strlen(trim($_POST[username])) >12){ 
    $error.="Name must be between 6 ad 12 chars<br />"; 
} 
}// closes if(isset *** move this until after the next if statement *** 

//this is run everytime as its outside the if(isset block 
if($error==''){ // errors is initialised as '' so this will run 
    $sql = @mysql_query("INSERT INTO users SET username=\"$username\", password=\"$password\""); 
} 

if(isset($_POST[submit])){이 주제 오프 if(isset($_POST['submit'])){

SQL Injection - You should read this