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);
}
}
?>
添加回答
举报