2013-03-21 2 views
0

가정용 컴퓨터를 떠나지 않으려는 웹 사이트의 두 페이지를 통해 양식을 사용하여 내 컴퓨터에서 호스팅되는 MySQL 데이터베이스에 항목을 입력하고 싶습니다. 나는 거의 동일하게 무언가를하기 전에 이것을 해왔지만, 어떤 이유로이 것은 작동하지 않습니다. 나는 내 컴퓨터를 떠나지 않을 것이기 때문에이 보안 또는 그와 같은 것의 보안에 대해 걱정하지 않는다. 나는 실제로 그것이 실제로 작동하기를 바란다.양식에서 MySQL 데이터베이스에 항목 삽입

형태 :

같은 레이아웃을 유지하기 위해 HTML 쌌다이처럼 보이는, addclothes.php을 통해 전송
<form action='addclothes.php' method='post'><table style="font-family:verdana;font-size:14px;color:#004766;"><tr><td> 
Type of clothing:</td><td><select name="type"> 
<option value="0">---</option> 
<option value="dresses">Dress</option> 
<option value="tops">Top</option> 
<option value="bottoms">Bottom</option> 
<option value="shoes">Shoes</option> 
<option value="accessories">Accessory</option></select></td></tr> 
<tr><td>Name:</td><td><input type="text" name="name"></td></tr> 
<tr><td>Path to full image:</td><td><input type="text" name="largeimagepath"></td></tr> 
<tr><td>Path to thumbnail:</td><td><input type="text" name="smallimagepath"></td></tr> 
<tr><td colspan="2"><center><input type="submit" value="Submit" name="submit"></center></td></tr> 
</table></form> 

:

<?php 

$name = $_POST['name']; 
$table = $_POST['type']; 
$largepath = $_POST['largeimagepath']; 
$thumbpath = $_POST['smallimagepath']; 

    $db = mysql_connect("localhost", "root", "******") or die(mysql_error()); 
    mysql_select_db("Default") or die(mysql_error()); 

    $query = "INSERT INTO clothes."{$table}" (name, imagepath, thumbimagepath) 
VALUES("{$name}", "{$largepath}", "{$thumbpath}")"; 
    mysql_query($query) or die(mysql_error()); ?> 

<p>Item Added!</p> 

그것은 다음 페이지 단지로 온다 무엇이든간에 "항목이 추가되었습니다"라고 말합니다. 쿼리를 에코하려는 경우 바로 나타나지 않는 변수를 만듭니다.

+4

[** ** 구식 ** 데이터베이스 API] (http://stackoverflow.com/q/12859942/19068)를 사용하고 있으며 [최신 대체] (http://php.net/manual/)를 사용해야합니다. en/mysqlinfo.api.choosing.php). 또한 ** [SQL 주입 공격] (http://bobby-tables.com/) **에 현대적인 API를 사용하면 쉽게 방어 할 수 있습니다 ** (http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)을 사용하십시오. – Quentin

+0

문자열 연결에 PHP에서 구문 분석 오류가 발생할 것으로 예상되는 구문 오류가 있습니다. (이전의 코멘트에 따르면, 쿼리 빌딩에서 문자열 연결을 제거해야하기 때문에 실제로는 사물의 웅장한 계획에서 중요하지 않습니다.) – Quentin

답변

1

이것은 잘못된 것입니다 :

$query = "INSERT INTO clothes."{$table}" (name, imagepath, thumbimagepath) 
      VALUES("{$name}", "{$largepath}", "{$thumbpath}")"; 

당신은 테이블 이름을 인용하지 않는 (그것을 깨는 피하기 위해 쿼리 내부에 작은 따옴표를 사용해야합니다, 당신이 사용하는 MySQL의 예약어가 될 수있는 경우 그에 대한 역 따옴표) :

$query = "INSERT INTO clothes.`{$table}` (name, imagepath, thumbimagepath) 
      VALUES('{$name}', '{$largepath}', '{$thumbpath}')"; 

는 또한 보안/SQL 인젝션 그냥 나쁜 의도를 가진 사람들로부터 당신을 보호하지 않는 것입니다주의; SQL 쿼리에 사용하기 위해 데이터를 올바르게 준비하지 않으면 사용자가 입력 한 유효한 데이터조차도 예를 들어 이름에 ' 문자 (예 : O'Neill ...)가 포함되어 있으면 쿼리/응용 프로그램이 중단 될 수 있습니다.

안전은 항상 중요하므로 PDO (또는 mysqli)와 준비된 문으로 전환해야합니다. 그 외에도 mysql_* 함수는 사용되지 않습니다.

마지막 주석 한 개 : 외부 세계에 사이트를 열어야 만 준비 또는 이스케이프 처리가 필요하지 않으므로 쿼리에서 테이블 이름을 보호 할 수 있습니다. SQL 인젝션을 피하기 위해 허용 된 테이블 이름 목록을 확인해야합니다.

0
<?php 
    $name = $_POST['name']; 
    $table = $_POST['type']; 
    $largepath = $_POST['largeimagepath']; 
    $smallpath = $_POST['smallimagepath']; 

    $name = htmlentities($name); 
    $table = htmlentities($table); 
    $largepath = htmlentities($largepath); 
    $smallpath = htmlentities($smallpath); 

    $connection = new PDO('mysql:host=localhost;dbname=Default','root','*****'); 
    $query = $connection->prepare('INSERT INTO :table (name,imagepath,thumbimagepath) VALUES (:name,:image,:thumb)'; 

    $query->bindParam(':table', $table); 
    $query->bindParam(':name', $name); 
    $query->bindParam(':image',$largepath); 
    $query->bindParam(':thumb',$smallpath); 
    $query->execute(); 

    if($query->rowCount()) { 
     echo "Inserted correctly"; 
    } else { 
     echo "Failure inserting"; 
    } 
?> 

다른 사람들이 말했듯이 누군가가 양식을 통해 테이블 ​​이름을 입력하도록 허용하면 안됩니다.

관련 문제