这是我的三维数组。
我的$ _POST数组。这些是值
$_POST = Array (
[9] => Array ( [student_id] => 9 [subject_id] => 6 [class_id] => 2 [mark_obtained] => 40 [mark_obtainable] => 100 )
[10] => Array ( [student_id] => 10 [subject_id] => 6 [class_id] => 2 [mark_obtained] => 52 [mark_obtainable] => 100 )
[11] => Array ( [student_id] => 11 [subject_id] => 6 [class_id] => 2 [mark_obtained] => 30 [mark_obtainable] => 100 )
[12] => Array ( [student_id] => 12 [subject_id] => 6 [class_id] => 2 [mark_obtained] => 68 [mark_obtainable] => 100 ) )
如何循环并将每个数组提交到数据库?
我的列名是键。 “student_id
,subject_id
,class_id
,mark_obtained
,mark_obtainable
”。
我正在尝试这个,但我不断收到重复的条目.foreach($ _POST as $ i => $ values1){
foreach ($values1 as $key => $value) {
$columns = implode(", ",array_keys($values1));
$values = implode(", ", $values1);
$columns = $columns
$sql = "INSERT INTO `daily_report`($columns) VALUES ($values)";
if ($conn->multi_query($sql) === TRUE) {
echo "New records created successfully <br>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
答案 0 :(得分:1)
你必须像这样回显foreach循环中的sql变量。
foreach ($_POST as $data => $values) {
echo $sql = "INSERT INTO `daily_report` (student_id, subject_id, class_id,mark_obtained,mark_obtainable,daily_report.date)
VALUES (".$values['student_id'].", ".$values['subject_id'].", ".$values['class_id'].", ".$values['mark_obtained'].",".$values['mark_obtainable'].", now())";
// echo $sqlupdate1 = "UPDATE table SET commodity_quantity=$qty WHERE user='".$rows['user']."' ";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
在回显时连接变量。
答案 1 :(得分:0)
您不需要三维数组。您可以使用二维数组,并使用 $data_array = [
[
'student_id' => 9,
'subject_id' => 6,
'class_id' => 2,
'mark_obtained' => 40,
'mark_obtainable' => 100
],
[
'student_id' => 10,
'subject_id' => 6,
'class_id' => 2,
'mark_obtained' => 52,
'mark_obtainable' => 100
],
[
'student_id' => 11,
'subject_id' => 6,
'class_id' => 2,
'mark_obtained' => 30,
'mark_obtainable' => 100
],
[
'student_id' => 12,
'subject_id' => 6,
'class_id' => 2,
'mark_obtained' => 30,
'mark_obtainable' => 100
],
];
foreach($data_array as $data) {
$sql = "INSERT INTO `daily_report` (student_id, subject_id, class_id,mark_obtained,mark_obtainable)
VALUES ($data['student_id'], $data['subject_id'], $data['class_id'], $data['mark_obtained'],$data['mark_obtainable'])";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
逐个插入它们。这是一个例子:
var w = 500;
var h = 250;
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
// Values for calculating pdf of normal distribution
var sigma = 4;
var mu = 0;
var N = 10;
var step = 0.1;
var dataset = [];
var x;
// creating the pdf of the normal distribution and plotting it for -N to N
var C = 1/(sigma*Math.sqrt(2*Math.PI));
for (x=-N; x < N; x += step) {
var E = (x-mu)/sigma;
E = -(E*E)/2;
var d = C*Math.exp(E);
dataset.push(d);
}
// Scales slightly over fancy, required for features stripped out
var overlap = w*0.1;
var xscale1 = d3.scale.linear().range([0, w/2+overlap]).domain([0, dataset.length-1]).clamp(true);
var xscale2 = d3.scale.linear().range([w/2-overlap, w]).domain([0, dataset.length-1]).clamp(true);
// So specifies the height as max in dataset and it takes up 1/2 the svg
var yscale = d3.scale.linear().domain([0, d3.max(dataset)]).range([h,h/2]);
var area1 = d3.svg.area()
.x(function(d,i) { return xscale1(i); })
.y0(h)
.y1(function(d,i) { return yscale(d); });
svg.append("path")
.datum(dataset)
.attr("class", "area1")
.attr("d", area1)
.attr("opacity",0.75)
.call(d3.behavior.drag().on("drag", dragged));
function dragged(d) {
// Current position:
this.x = this.x || 0;
this.y = this.y || 0;
// Update thee position with the delta x and y applied by the drag:
this.x += d3.event.dx;
this.y += d3.event.dy;
// Apply the translation to the shape:
d3.select(this)
.attr("transform", "translate(" + this.x + "," + this.y + ")");
}