나는 하나의 선택 목록과 여러 행을 형성하기 위해 동적으로 추가 할 수있는 두 개의 텍스트 박스가있는이 양식을 가지고 있습니다. 시나리오에서는 하나의 선택 값과 여러 텍스트 상자 값을 갖게됩니다. 나는 이것을 저장하기를 원한다. SelectValue, TextBox1Value 및 TextBox2Value의 세 컬럼을 가진 mysql 테이블이다. SelectValue는 동일하거나 중복되지만 텍스트 상자 값은 고유해야합니다. 내가 PHP는 다음과 같은 시도 :PHP를 사용하여 MySQL에 배열 저장
include('connection.php');
if(isset($_POST['submit'])){
$roomId = $_POST['roomno'];
$rowData = array();
foreach($_POST['inventoryname'] as $row=>$inventory){
$inventoryName = mysql_real_escape_string($inventory);
$inventoryCount = mysql_real_escape_string($_POST['inventorycount'][$row]);
$rowData[] = "('$inventoryName','$inventoryCount')";
}
}
if(!empty($rowData)){
$insert = mysql_query("INSERT INTO room_inventory_details(RoomId,Inventory,Count) VALUES('$roomId')".implode(',',$rowData));
if(!$insert){
die('Error: ' . mysql_error());
}else{
echo "Data saved successfullt";
}
}
그리고 여기가 내 마크 업
<form action="save_room_inventory.php" method="post" name="reservation-form" id="reservation_form">
<div class="left-form">
<label for="roomno"><span>Room No.</span>
<select name="roomno">
<option value="">---Please Select---</option>
<?php
include('connection.php');
$select_query = mysql_query("SELECT RoomId,RoomName FROM room ORDER BY RoomName");
while($rows = mysql_fetch_assoc($select_query)){ ?>
<option value="<?php echo $rows['RoomId']; ?>"><?php echo $rows['RoomName']?></option>
<?php }
?>
</select>
</label>
</div>
<div class="left-form">
<input type="button" id="addinventory" Value="Add Inventory">
</div>
<div class="inventory-table">
<div class="detail-table">
<table class="itemTable" border="1">
<thead>
<th>Inventory</th>
<th>Count</th>
<th><input type="button" value="Add New Row" id="addNew"></th>
</thead>
<tbody>
<tr class="cloneme">
<td>
<input type="text" name="inventoryname[]">
</td>
<td>
<input type="text" name="inventorycount[]">
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="event_button">
<input type="submit" name="submit" value="Save">
<input type="reset" name="reset" Value="Clear">
<iframe name="acknowledgement" id="res_frame"></iframe>
</div>
</form>
내가 저장할 때 다음과 같은 오류가 오전 : 오류 : 당신은 당신의 SQL 구문에 오류가 있습니다; 라인 1에서 '('Bed ','1 '), ('Cderf ','4 ')'근처에서 사용할 올바른 구문에 대해서는 MySQL 서버 버전에 해당하는 매뉴얼을 확인하십시오.
당신이 보여준 쿼리에서'('Bed', '1'), ('Cderf', '4')'와 비슷한 것을 보지 못했습니다. –
@SpencerWieczorek 그것은'implode (',', $ rowData)'에서옵니다. – Barmar
사람, 데이터베이스 응용 프로그램으로 코딩하는 경우 주입 공격을 방지하기 위해 정확하고 최신 기능을 사용하고 있는지 확인하는 것이 실제 작업입니다. mysql 함수를 두 번 확인하여 감가 상각되지 않도록하십시오. ** mysql_query **는 사용 불능이며 직접 주입 취약점을 나타냅니다. PHP에서 데이터베이스 연결 작업을 할 때 ** prepared statements **를 사용하십시오. 준비된 진술이 무엇인지 잘 모르는 경우 연구하고 읽어보고, 코드를 사용하여 작업하는 많은 개발자와 개발자를 구할 수 있습니다. – Axel