이것은 서버 측 기술을 사용하는 첫 번째 웹 사이트입니다. 그리고 을 따라 일하는 것은 PHP
에 대한 나의 첫 번째 탐구이기도합니다. 나는 가면서 일하고 배우고있다.PHP - 확인란 양식의 데이터를 MYSQL에 삽입하려면
나는 작은 댄스 용 드레스 비즈니스를위한 제품 항목의 데이터베이스를위한 기본 관리 페이지를 만들었습니다.
저는 관리자가 데이터베이스에 새 항목을 업로드하는 데 사용할 수있는 양식의 기능에 대해 작업했습니다.
이러한 항목 중 일부
은 다음과 같습니다드레스 이름 [텍스트 입력]컬러 가용성 [체크 박스]
나는 여러 검사에서 문제가 점점 데이터로 실행했습니다
상자를 HTML 양식에서 데이터베이스로 가져옵니다. 여기
<form action="inventory_list.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
<table width="90%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="20%" align="right">Dress Name</td>
<td width="80%"><label>
<input name="Dress_name" type="text" id="Dress_name" size="64" />
</label>
</td>
</tr>
<tr>
<td align="right">Collections</td>
<td>
<label>
<select name="collections" id="collections">
<option value="other">Other</option>
<option value="hermione">Hermione</option>
<option value="jora">Jora</option>
<option value="manon">Manon</option>
</select>
</label></td>
</tr>
<tr>
<td align="right">Available Sizes</td>
<td>
<input type="checkbox" name="sizes[]" value="1" /> Sizes 2-6<br />
<input type="checkbox" name="sizes[]" value="1" /> Sizes 6-10 <br />
<input type="checkbox" name="sizes[]" value="1" /> Sizes 10-14<br />
<input type="checkbox" name="sizes[]" value="1" /> Sizes 14-18 <br />
<input type="checkbox" name="sizes[]" value="1" /> Sizes 18-22<br />
<input type="checkbox" name="sizes[]" value="1" /> Sizes 22-26 <br />
</td>
</tr>
<tr>
<td align="right">Dress Colours</td>
<td>
<input type="checkbox" name="colours[]" value="1" /> Pinks/Reds <br />
<input type="checkbox" name="colours[]" value="1" /> Blues/Greens <br />
<input type="checkbox" name="colours[]" value="1" /> Violet/Purple<br />
<input type="checkbox" name="colours[]" value="1" /> Yellow/Orange/Brown/Gold <br />
<input type="checkbox" name="colours[]" value="1" /> Black/White<br />
</td>
</tr>
<tr>
<td align="right">Product Image</td>
<td><label>
<input type="file" name="fileField" id="fileField" />
</label></td>
</tr>
<tr>
<td> </td>
<td><label>
<input type="submit" name="button" id="button" value="Add This Item Now" />
</label></td>
</tr>
</table>
</form>
그리고 내장 한 PHP는 다음과 같습니다
여기 형태의 구조입니다 - 체크 상자에
선택 사항은 (1/0 예/아니오)이 부울 값을 나타내는 안 데이터베이스에 데이터를 제출하는 양식을 처리합니다.
<?php
if (isset($_POST['Dress_name'])) {
$dress_name = mysql_real_escape_string($_POST['Dress_name']);
$collections = mysql_real_escape_string($_POST['collections']);
$sizesArray = $_POST['sizes'];
$coloursArray = $_POST['colours'];
foreach ($sizesArray as $key => $value)
{
echo "Key = $key || Value = $value<br />";
}
foreach ($coloursArray as $ckey => $cvalue)
{
echo "Key = $ckey || Value = $cvalue<br />";
}
//See if that product name is an identical match to another product in the system
$sql = mysql_query("SELECT ID FROM names WHERE Dress='$dress_name' LIMIT 1");
$productMatch = mysql_num_rows($sql); // count the output amount
if ($productMatch > 0) {
echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>';
exit();
}
//Add this product into the database now
$sql = mysql_query("INSERT INTO names (Dress)
VALUES('$dress_name')") or die (mysql_error());
$pid = mysql_insert_id();
//Place image in the folder
$newname = "$pid.jpg";
move_uploaded_file($_FILES['fileField']['tmp_name'], "../inventory_images/$newname");
header("location: inventory_list.php");
exit();
}
?>
당신이 볼 수 있듯이
, 나는 지금까지 배열로 데이터를 퍼팅 있어요. 그리고 지금은 벽돌 벽을 조금 치었지만 어디서나 답을 찾을 수없는 것 같습니다.부울 값을 데이터베이스 테이블의 올바른 열로 가져 오려면 어떻게해야합니까?
감사합니다.
HTML 폼의 체크 박스는 모두 같은 값을 가지고있다. 적절한 크기? – andrewsi
MySQL의 [부울 유형] (http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html)은 0과 1처럼 보입니다. 값이 true이면 열에 1을 저장합니다. 값이 false (또는 단순히 true가 아님)이면 0을 저장하십시오. – buley
사실, MySQL이 부울을 처리하는 방식 때문에 TINYINT를 사용하는 것이 훨씬 더 좋습니다. 부울 유형은 이상한 오류를 일으킬 수 있습니다. 위 또는 아래 플래그가 필요한 경우 길이가 1 인 정수를 사용하십시오. –