3 回答

TA貢獻1946條經驗 獲得超3個贊
我最近不得不做類似的事情(雖然我輸出為 JSON 但你應該能夠讓它為你的 SQL 工作)
首先我創建了我的表單
例如
<input required type="text" name="customer[contactName]" class="form-control" placeholder="John Doe">
然后我把 POST 數據格式化成一個數組。
$UIDv4 = Uuid::uuid4();
$rmqMessage = array("GUID"=>$UIDv4,
"customer"=>array(
"companyName"=>$_POST['customer']['companyName'],
"contactName"=>$_POST['customer']['contactName'],
"contactEmail"=>$_POST['customer']['contactEmail'],
"contactPhone"=>$_POST['customer']['contactNumber'],
"billingName"=>$_POST['customer']['billingName'],
"billingEmail"=>$_POST['customer']['billingEmail'],
"billingPhone"=>$_POST['customer']['billingPhone'],
"Address"=>array(
"Line1"=>$_POST['customer']['Address']['Line1'],
"Line2"=>$_POST['customer']['Address']['Line2'],
"City"=>$_POST['customer']['Address']['City'],
"State"=>$_POST['customer']['Address']['State'],
"Zip"=>$_POST['customer']['Address']['Zip'],
"country"=>$_POST['customer']['Address']['country']
),
"appPrefix"=>$_POST['customer']['appurl']
)
);
最后,我將其轉換為 JSON 并將其推送到 SQL(但您可能不需要/不想這樣做,除非您使用的是 SQL Server 2016 或支持 JSON 的東西)
$provDatabase->query("exec web_pushRequest ?, ?", $UIDv4->toString(),json_encode($rmqMessage));

TA貢獻1844條經驗 獲得超8個贊
將表單的輸入更改為item[0][name],將為您提供一個更易于使用的數據集。想法取自:https : //stackoverflow.com/a/3314578/296555。
此外,您只需將用戶輸入的數據注入到您的查詢中,就對 SQL 注入攻擊持開放態度。查看準備好的報表。
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
foreach ($_POST['item'] as $item) {
// Each `$item` now represents an entire row from your form.
// And you can access individual cells with $item['name'] or $item['average'] to use in your SQL.
var_dump($item);
}
}
?>
<form method="post">
<table class="table table-condensed table-striped">
<tr>
<th>Name</th>
<th>Roll</th>
<th>Class</th>
<th>Phy</th>
<th>Eng</th>
<th>Maths</th>
<th>Average</th>
<th>Grade</th>
</tr>
<?php for ($i = 1; $i <= 2; $i++) { ?>
<tr class="active">
<td><input type="text" name="item[<?php echo $i; ?>][name]" placeholder="Name"></td>
<td><input type="text" name="item[<?php echo $i; ?>][roll]" placeholder="Roll"></td>
<td><input type="text" name="item[<?php echo $i; ?>][class]" placeholder="Class"></td>
<td><input type="number" name="item[<?php echo $i; ?>][phy]" placeholder="Physics"></td>
<td><input type="number" name="item[<?php echo $i; ?>][eng]" placeholder="Eng"></td>
<td><input type="number" name="item[<?php echo $i; ?>][maths]" placeholder="Maths"></td>
<td><input type="text" name="item[<?php echo $i; ?>][average]" placeholder="average"></td>
<td><input type="text" name="item[<?php echo $i; ?>][grade]" placeholder="Grade"></td>
</tr>
<?php } ?>
</table>
<input class="btn btn-primary" type="submit" name="submit" value="Submit">
</form>
樣本輸出
array (size=8)
'name' => string 'Matt' (length=4)
'roll' => string 'Manager' (length=7)
'class' => string '' (length=0)
'phy' => string '' (length=0)
'eng' => string '' (length=0)
'maths' => string '' (length=0)
'average' => string '' (length=0)
'grade' => string '' (length=0)
/var/www/html/public/psft/apply/index2.php:6:
array (size=8)
'name' => string 'John' (length=4)
'roll' => string 'Programmer' (length=10)
'class' => string '' (length=0)
'phy' => string '' (length=0)
'eng' => string '' (length=0)
'maths' => string '' (length=0)
'average' => string '' (length=0)
'grade' => string '' (length=0)
編輯
是的,它有效,但我怎么能以以前的方式做到這一點,因為到目前為止有人問我要這樣做。它有效后,我可以處理安全問題
除了建議使用準備好的語句之外,該解決方案與安全無關。我的建議是改變你輸入名字,以方便為你一起工作。您當前的實現name[],roll[]等等。將創建元素分組的數據結構。例如:
array[
'name' => [
0 => 'Matt',
1 => 'John'
],
'roll' => [
0 => 'Manager',
1 => 'Programmer'
]
]
我想你會明白這有什么問題。使用它非常困難,因為您需要訪問每個子數組中的單個元素。您可以對其進行按摩以使其成為可用于構建查詢的規范化格式。就像是:
$result = array();
foreach ($_POST as $key => $data) {
if (is_array($data)) {
foreach ($data as $offset => $value) {
if (isset($result[$offset])) {
$result[$offset][$key] = $value;
} else {
$result[$offset] = array($key => $value);
}
}
}
}
這會給你:
array[
[
'name' => 'Matt',
'roll' => 'Manager'
],
[
'name' => 'John',
'roll' => 'Programmer'
]
]
這與我原來的解決方案格式相同。但是它花費了你 2 個額外的循環和額外的代碼來維護。我會讓你決定選擇哪一個。

TA貢獻2041條經驗 獲得超4個贊
<?php
$con=mysqli_connect('localhost','root','','student');
if($con){
echo "connected";
}
else{
echo "not connected";
}
if(isset($_POST['submit'])){
$stack = array();
$name=$_POST['name'];
$roll=$_POST['roll'];
$class=$_POST['class'];
$phy=$_POST['phy'];
$eng=$_POST['eng'];
$maths=$_POST['maths'];
$average=$_POST['average'];
$grade=$_POST['grade'];
array_push($stack, $name);
array_push($stack, $roll);
array_push($stack, $class);
array_push($stack, $phy);
array_push($stack, $eng);
array_push($stack, $maths);
array_push($stack, $average);
array_push($stack, $grade);
;
for ($j=0; $j < sizeof($stack); $j++) {
foreach ($stack[$j] as $key=>$value) {
$query="INSERT INTO student_detail (id,name,roll,class,phy,eng,maths,avg,grade,date) VALUES('','$name[$key]','$roll[$key]','$class[$key]','$phy[$key]','$eng[$key]','$maths[$key]','$average[$key]','$grade[$key]',CURDATE())";
$result=mysqli_query($con,$query);
// // var_dump($stack);
}
break;
}
// var_dump($stack);
// print_r($stack);
if($result){
echo "data inserted";
}
else{
echo "data not".mysqli_error($con);
}
}
?>
添加回答
舉報