2014-05-18 3 views
-1

안녕하세요 저는 처음으로 데이터베이스에 데이터를 삽입하는 연습을하고 있습니다. 코드에 몇 가지 문제가 있습니다.PHP를 사용하여 데이터베이스에 SQL 삽입

그래서 내 코드는 폼에서 정보를 가져와 내가 연결 한 데이터베이스에 정보를 삽입한다는 것입니다. 2 가지 유형의 오류가 발생합니다. 하나는 내 POSTS에서 정의되지 않은 색인이고 SQL 구문 오류도 발생합니다. 나는 그 문제를 해결하는 데 도움이되는 행운을 보지 않으려 고 노력했다.

정의되지 않은 인덱스 :

편집 (I 게시 할 때 참으로 나는 메시지를 추가하는 것을 잊었다) 라인에 CustomerID를 queryYou 57

문제는 SQL 구문에 오류가있다; '', '', '', '', '', '') '라인 2

에서이 어떤 조언이 도움이 될 것입니다 : 지금은

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<title>Form</title> 
</head> 
<body> 
<h1>Customer Information Collection <br /></h1> 
<form method="post" action="task12.php" id="custinfo" > 
    <table> 
    <tr> 
     <td><label for="customerid">Customer ID (integer value): </label></td> 
     <td><input type="text" id="customerid" name="customerid" size=11/></td> 
    </tr> 
    <tr> 
     <td><label for="customerfname">Customer First Name: </label></td> 
     <td><input type="text" id="customerfname" name="customerfname" size=50/></td> 
    </tr> 
    <tr> 
     <td><label for="customerlname">Customer Last Name: </label></td> 
     <td><input type="text" id="customerlname" name="customerlname" size=50/></td> 
    </tr> 
    <tr> 
     <td><label for="customeraddress">Customer Address: </label></td> 
     <td><input type="text" id="customeraddress" name="customeraddress" size=65/></td> 
    </tr> 
    <tr> 
     <td><label for="suburb"> Suburb: </label></td> 
     <td><input type="text" id="suburb" name="suburb"/></td> 
    </tr> 
    <tr> 
    <td> State: </td> 
    <td> <select name="state" id="state"> 
     <option value="--">--</option> 
     <option value="ACT">ACT</option> 
     <option value="NSW">NSW</option> 
     <option value="NT">NT</option> 
     <option value="QLD">QLD</option> 
     <option value="SA">SA</option> 
     <option value="TAS">TAS</option> 
     <option value="VIC">VIC</option> 
     <option value="WA">WA</option> 
     </select> 
    </td> 
    </tr> 
    <tr> 
    <td><label for="postcode"> Post Code (default "2000"): </label></td> 
    <td><input type="text" id="postcode" name="postcode" value="2000" size=4/></td> 
    </tr> 
    <tr> 
    <td> <input type="submit" name="submit" value="Submit"/> </td> 
    <td> <input type="reset" value="Clear" /> </td> 
    </tr> 
    </table> 
    </form> 
<?php 
$customeridstr = $_POST['customerid']; 
$customerfnamestr = $_POST['customerfname']; 
$customerlnamestr = $_POST['customerlname']; 
$customeraddressstr = $_POST['customeraddress']; 
$suburbstr = $_POST['suburb']; 
$statestr = $_POST['state']; 
$postcodestr = $_POST['postcode']; 
?> 
<?php 
$conn = mysql_connect("xxxxx", "xxxxx", "xxxxxx"); 
mysql_select_db("xxxxxxx", $conn) 
or die ('Database not found ' . mysql_error()); 
$sql = "INSERT INTO Customer (customerID, firstName, lastName, Address, suburb, state, postcode) 
VALUES ('$customeridstr', '$customerfnamestr’, '$customerlnamestr', '$customeraddressstr', '$suburbstr', '$statestr', '$postcodestr');"; 
$rs = mysql_query($sql, $conn) 
or die ('Problem with query' . mysql_error()); 
?> 

</body> 
</html> 
+0

당신은 SQL 오류를 게시 할 수 있습니까? – Jens

+0

오류를 추가하십시오. 정의되지 않은 색인을 찾으려면 코드를 한 행씩 읽지 말고 ... 오류 메시지의 의미를 확인하십시오. 그들은 이유가 있습니다. –

+0

게시 할 때 죄송합니다. 게시 할 때 나는 그들을 추가하는 것을 잊어 버렸습니다. – InsignificantPuppet

답변

1
<?php 
if(isset($_POST['customerid'])){ 
    $customeridstr = $_POST['customerid']; 
    $customerfnamestr = $_POST['customerfname']; 
    $customerlnamestr = $_POST['customerlname']; 
    $customeraddressstr = $_POST['customeraddress']; 
    $suburbstr = $_POST['suburb']; 
    $statestr = $_POST['state']; 
    $postcodestr = $_POST['postcode']; 

    $conn = mysql_connect("xxxxx", "xxxxx", "xxxxxx"); 
    mysql_select_db("xxxxxxx", $conn) or die ('Database not found ' . mysql_error()); 
    $sql = "INSERT INTO Customer (customerID, firstName, lastName, Address, suburb, state, postcode) VALUES ('{$customeridstr}', '{$customerfnamestr}’, '{$customerlnamestr}', '{$customeraddressstr}', '{$suburbstr}', '{$statestr}', '{$postcodestr}');"; 
    $rs = mysql_query($sql, $conn); 
    if($rs){ 
     echo 'Records Inserted'; 
    }else{ 
     die ('Problem with query' . mysql_error()); 
    } 
}else{ 
?>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<title>Form</title> 
</head> 
<body> 
<h1>Customer Information Collection <br /></h1> 
<form method="post" action="" id="custinfo" > 
    <table> 
    <tr> 
     <td><label for="customerid">Customer ID (integer value): </label></td> 
     <td><input type="text" id="customerid" name="customerid" size=11/></td> 
    </tr> 
    <tr> 
     <td><label for="customerfname">Customer First Name: </label></td> 
     <td><input type="text" id="customerfname" name="customerfname" size=50/></td> 
    </tr> 
    <tr> 
     <td><label for="customerlname">Customer Last Name: </label></td> 
     <td><input type="text" id="customerlname" name="customerlname" size=50/></td> 
    </tr> 
    <tr> 
     <td><label for="customeraddress">Customer Address: </label></td> 
     <td><input type="text" id="customeraddress" name="customeraddress" size=65/></td> 
    </tr> 
    <tr> 
     <td><label for="suburb"> Suburb: </label></td> 
     <td><input type="text" id="suburb" name="suburb"/></td> 
    </tr> 
    <tr> 
    <td> State: </td> 
    <td> <select name="state" id="state"> 
     <option value="--">--</option> 
     <option value="ACT">ACT</option> 
     <option value="NSW">NSW</option> 
     <option value="NT">NT</option> 
     <option value="QLD">QLD</option> 
     <option value="SA">SA</option> 
     <option value="TAS">TAS</option> 
     <option value="VIC">VIC</option> 
     <option value="WA">WA</option> 
     </select> 
    </td> 
    </tr> 
    <tr> 
    <td><label for="postcode"> Post Code (default "2000"): </label></td> 
    <td><input type="text" id="postcode" name="postcode" value="2000" size=4/></td> 
    </tr> 
    <tr> 
    <td> <input type="submit" name="submit" value="Submit"/> </td> 
    <td> <input type="reset" value="Clear" /> </td> 
    </tr> 
    </table> 
    </form> 
</body> 
</html> 
<?php 
} 
?> 

은, 위의 해결 방법을 시도해보십시오, 이게 효과가있다. 그것이 지금은 사용되지 않으며, 대신 내 MySQL의 라이브러리를 mysqli를 사용

  • : 더 학습

    는이 일을하려고합니다. 데이터 ($ _POST 또는 $ _GET에서 가져 오는 데이터)의 살균을 시도하십시오.

  • 데이터베이스 연결을 위해 별도의 파일을 설정하고 필요한 곳에 하나의 PHP 스크립트/페이지에서 한 번만 포함하십시오.

  • 같은 페이지에 값을 게시하는 것은 좋지 않지만 제출 된 데이터를 처리하는 다른 페이지에 게시하는 것이 좋습니다.

  • 또한 변수를 설정했는지 여부를 확인하고 변수가 비어 있는지 확인하지 않으므로 확인하려는 모든 필요한 변수와 조건을 확인해보십시오.

건배 :)

+0

게시 된 코드는 SQL- 주사에 취약합니다. 또한 게시물 필드의 (명시 적으로 검사 된 것을 제외하고) 설정되어 있는지 보장 할 수 없습니다. – Rangad

+1

솔루션을 작성한 후에 내가 읽은 것을 읽으십시오. –

+0

나는 그것을 읽었지만, 대부분의 사람들이 문제를 해결하는 데 필요한 것 이상을 읽지는 않을 것이라고 생각합니다. 그러나 그것은 어떤 식 으로든 당신의 대답을 저하시키지 않습니다. – Rangad

관련 문제