2013-02-02 2 views
0

텍스트 상자 코드에서 PHP 양식으로 이동하여 PHP 양식에서 데이터베이스로 보내도록하려고합니다. 내가 겪고있는 문제는 $ code를 게시하는 textbox 값을 게시하는 대신에 발생합니다. 나는 mysql과 PHP를 사용하고있다.변수 이름보다는 변수 값 게시

PHP :

<? 
if($_POST) 
{ 
$username="***"; 
$password="***"; 
    $con = mysql_connect("***",$username,$password); 

    if (!$con) 
    { 
     die('Could not connect: ' . mysql_error()); 
    } 

    mysql_select_db("inmoti6_mysite", $con); 

    $code = $_POST['code']; 


    $code = htmlspecialchars($code); 


    $query = 'INSERT INTO `storycodes`.`storycodes` (`code`) VALUES ("$code");'; 


    mysql_query($query); 

    echo "<h2>Thank you for your Comment!</h2>"; 

    mysql_close($con); 
} 
?> 

의심이 문제이지만, 여기에 HTML이다 : 당신은 변수가 문자열로 인식 할 수 따옴표를 사용하므로 변경해야

<!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>Database</title> 
</head> 

<body> 
<form id="form1" name="form1" method="post" action="/scripts/database2.php"> 
    <label>Code: 
    <input name="code" type="text" id="code" value="" size="45" /> 
</label> 
    <p> 
    <label> 
    <input type="submit" name="submit" id="submit" value="Submit" /> 
    </label> 
    </p> 
</form> 
</body> 
</html> 
+0

내가 틀렸다고 정정하되 따옴표 안에있는 변수의 리터럴 용도는 큰 따옴표로만 유효합니다. – TheDeadLike

답변

2

:

$query = 'INSERT INTO `storycodes`.`storycodes` (`code`) VALUES ("$code");'; 

또한 SQL 주입 문제가 있습니다. 준비된 문과 바운드 변수를 사용하여 PDO (또는 mysqli)로 전환하는 것이 좋습니다. 적어도 데이터베이스에 삽입하기 전에 변수에 mysql_real_escape_string을 사용해야하지만 설명서에서 볼 수 있듯이 mysql_* 함수는 더 이상 사용되지 않습니다.

+0

또는 $ query = 'INSERT INTO storycodes.storycode (코드) VALUES ("'. $ code. '") "; – TheDeadLike

0

$ 코드를 게시하지 않으므로 $ code를 변수 대신 값으로 사용합니다. 문제는 따옴표와 관련이 있습니다.

$query = "INSERT INTO `storycodes`.`storycodes` (`code`) VALUES (\"$code\");"; 

이 항목을 시도해보십시오.