안녕하세요 저는 처음으로 데이터베이스에 데이터를 삽입하는 연습을하고 있습니다. 코드에 몇 가지 문제가 있습니다.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>
당신은 SQL 오류를 게시 할 수 있습니까? – Jens
오류를 추가하십시오. 정의되지 않은 색인을 찾으려면 코드를 한 행씩 읽지 말고 ... 오류 메시지의 의미를 확인하십시오. 그들은 이유가 있습니다. –
게시 할 때 죄송합니다. 게시 할 때 나는 그들을 추가하는 것을 잊어 버렸습니다. – InsignificantPuppet