2013-10-10 2 views
-1

웹 사이트 코딩 방법을 배우기 위해 노력하고 있습니다. 폼에서 정보를 얻고 입력해야합니다. 내 MySQL 데이터베이스, 그리고 내가 뭘 원하는지 최선의 방법을 모아서에서 PHP를 사용하는 것입니다. 제출 버튼을 누르면 내 index.php의 스크립트가 실행되지만 아무것도 DB에 저장되지 않습니다. 스크립트는 내 웹 루트 밖에있는 다른 스크립트에 연결됩니다. 암호가 포함되어 있기 때문에 거기에 넣어야했습니다 (이 작업을 수행하는 더 좋은 방법이 있습니까?) 이것이 내 문제의 일부일 수 있습니다.PHP로 폼 데이터를 얻으 려하고 mysql db에 넣어 둡니다. 무엇이 잘못 되었습니까?

<?php 
$con=mysqli_connect("192.168.1.125","root","pass","site"); 
// Check connection 
if (mysqli_connect_errno()) 
{ 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$sql="INSERT INTO users (Email) 
VALUES 
('$_POST[email]')"; 

if (!mysqli_query($con,$sql)) 
{ 
die('Error: ' . mysqli_error($con)); 
} 
echo "1 record added"; 

mysqli_close($con); 
?> 

 <?php 
    // define variables and set to empty values 
    $emailErr = ""; 
    $email = $password = ""; 
    $file = basename(urldecode($_GET['insert.php'])); 
    $fileDir = '/var/insert.php'; 

    if ($_SERVER["REQUEST_METHOD"] == "POST") 
    { 
     if (empty($_POST["email"])) 
      {$emailErr = "email is required";} 
     else 
      {$email = test_input($_POST["email"]); 
      if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) 
       { 
       $emailErr = "Invalid email format"; 
       } 
      } 
    } 

    function test_input($data) 
    { 
     $data = trim($data); 
     $data = stripslashes($data); 
     $data = htmlspecialchars($data); 
     return $data; 
    } 

    if (file_exists($fileDir . $file)) 
    { 
    // Note: You should probably do some more checks 
    // on the filetype, size, etc. 
    $contents = file_get_contents($fileDir . $file); 

    // Note: You should probably implement some kind 
    // of check on filetype 
    header('Content-type: php'); 

    echo $contents; 
    } 
    ?> 
     <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
     <input id="email" placeholder=" email" type="email" name="email" maxlength="50"> 
     <input id="pass" placeholder=" Password" type="password" name="pass" maxlength="25"> 
     <button id="submit">SUBMIT</button></form> 
    </div> 
    </div><div id="date" align="center"><img src="192.168.1.125/date.png"></div> 
    <?php 
    echo "<h2>Your Input:</h2>"; 
    echo "$email"; 
    echo "$emailErr"; 
    ?> 
+1

$sql="INSERT INTO users (Email) VALUES ('$_POST[email]')"; 

당신은 vulnera 있습니다 : 아래 코드의 관련 부분은 [SQL 주입 공격] (http://bobby-tables.com) –

답변

1

변화 라인,

$sql="INSERT INTO users (Email) VALUES ('".$_POST['email']."')"; 
0
$email = mysqli_real_escape_string($con, $_POST['email']); 
$sql="INSERT INTO users (Email) 
VALUES 
('$email')";