大家好,今天我得到了一个有趣的问题(至少对我而言)。我正在创建div和输入
在fetch_assoc中,我想知道提交所有这些输入的最佳方法。它们具有相同的名称,但它们具有不同的值,有时相同。
这是代码:
<?php
$sql = "SELECT IDBewoner, Foto FROM Bewoners LIMIT 5";
$res = mysqli_query($mysqli, $sql);
$totaalbewoners = mysqli_num_rows($res);
while($row = mysqli_fetch_assoc($res)) {
?>
<tr>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="POST">
<td><input type="text" value="<?php echo $row["IDBewoner"] ?>" name="bewoner"></td>
<td> <?php echo $row["IDBewoner"] ?> </td>
<td> <?php echo "<img src='data:image/jpeg;base64,".base64_encode($row['Foto'])."' width='90' height='90'>" ?> </td>
<td>
<div id="rang1" class="DropZone"></div>
<input type="hidden" value="" id="rang1input" name="rang1value" />
</td>
<td>
<div id="rang2" class="DropZone"></div>
<input type="hidden" value="" id="rang2input" name="rang2value" />
</td>
<td>
<div id="rang3" class="DropZone"></div>
<input type="hidden" value="" id="rang3input" name="rang3value" />
</td>
<td>
<div id="rang4" class="DropZone"></div>
<input type="hidden" value="" id="rang4input" name="rang4value" />
</td>
<td>
<div id="rang5" class="DropZone"></div>
<input type="hidden" value="" id="rang5input" name="rang5value" />
</td>
<td>
<div id="rang6" class="DropZone"></div>
<input type="hidden" value="" id="rang6input" name="rang6value" />
</td>
<td>
<div id="rang7" class="DropZone"></div>
<input type="hidden" value="" id="rang7input" name="rang7value" />
</td>
<input type="submit" value="Save">
</form>
</tr>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
/*for($i = 0; $i < $totaalbewoners; $i++) { [!!DANGEROUS CODE!!]
$rang1bewoner.$i = $_POST["rang1value"][$i];
}*/
$rang1 = $_POST["rang1value"];
$rang2 = $_POST["rang2value"];
$rang3 = $_POST["rang3value"];
$rang4 = $_POST["rang4value"];
$rang5 = $_POST["rang5value"];
$rang6 = $_POST["rang6value"];
$rang7 = $_POST["rang7value"];
$bewonerID = $_POST["bewoner"];
/*echo "<script>alert('Bewoner: $bewonerID')</script>";
echo "<script>alert('Rang1: $rang1')</script>";
echo "<script>alert('Rang2: $rang2')</script>";
echo "<script>alert('Rang3: $rang3')</script>";
echo "<script>alert('Rang4: $rang4')</script>";
echo "<script>alert('Rang5: $rang5')</script>";
echo "<script>alert('Rang6: $rang6')</script>";
echo "<script>alert('Rang7: $rang7')</script>";*/
}
}
?>
我首先将if $_SERVER["METHOD_REQUEST"]
放在fetch_assoc下,但只得到了值
第一排然后我把它放在while fetch_assoc
中(正如你在我的代码中看到的那样),但是没有工作
要么一遍又一遍地得到相同的价值观。然后我试着改变输入的名称
到name="rang6value[]"
,然后在PHP代码中我使用总行作为一个写for
最大(你可以在php的评论中看到它)和我的浏览器崩溃(这很有趣)。
现在问题是:如何以简单的方式获取输入字段的所有值
所有的行?如果你可以帮助我,我的生活会好得多。
非常感谢您提前。 ^^
更新:插入代码:
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
$rang1 = $_POST["rang1value"];
$rang2 = $_POST["rang2value"];
$rang3 = $_POST["rang3value"];
$rang4 = $_POST["rang4value"];
$rang5 = $_POST["rang5value"];
$rang6 = $_POST["rang6value"];
$rang7 = $_POST["rang7value"];
$bewonerID = $_POST["bewoner"];
if(!empty($rang1)) {
$insert1 = "INSERT INTO ActiviteitenSchema
(DagVanDeWeek, Bewoner, Activiteit, DatumAangemaakt, Rangnr)
VALUES
(4, '$bewonerID', '$rang1', '2015-04-14', 1)";
$res = $mysqli->query($insert1);
}
if(!empty($rang2)) {
$insert2 = "INSERT INTO ActiviteitenSchema
(DagVanDeWeek, Bewoner, Activiteit, DatumAangemaakt, Rangnr)
VALUES
(4, '$bewonerID', '$rang2', '2015-04-14', 2)";
$res = $mysqli->query($insert2);
}
if(!empty($rang3)) {
$insert3 = "INSERT INTO ActiviteitenSchema
(DagVanDeWeek, Bewoner, Activiteit, DatumAangemaakt, Rangnr)
VALUES
(4, '$bewonerID', '$rang3', '2015-04-14', 3)";
$res = $mysqli->query($insert3);
}
if(!empty($rang4)) {
$insert4 = "INSERT INTO ActiviteitenSchema
(DagVanDeWeek, Bewoner, Activiteit, DatumAangemaakt, Rangnr)
VALUES
(4, '$bewonerID', '$rang4', '2015-04-14', 4)";
$res = $mysqli->query($insert4);
}
if(!empty($rang5)) {
$insert5 = "INSERT INTO ActiviteitenSchema
(DagVanDeWeek, Bewoner, Activiteit, DatumAangemaakt, Rangnr)
VALUES
(4, '$bewonerID', '$rang5', '2015-04-14', 5)";
$res = $mysqli->query($insert5);
}
if(!empty($rang6)) {
$insert6 = "INSERT INTO ActiviteitenSchema
(DagVanDeWeek, Bewoner, Activiteit, DatumAangemaakt, Rangnr)
VALUES
(4, '$bewonerID', '$rang6', '2015-04-14', 6)";
$res = $mysqli->query($insert6);
}
if(!empty($rang7)) {
$insert7 = "INSERT INTO ActiviteitenSchema
(DagVanDeWeek, Bewoner, Activiteit, DatumAangemaakt, Rangnr)
VALUES
(4, '$bewonerID', '$rang7', '2015-04-14', 7)";
$res = $mysqli->query($insert7);
}
}
?>
答案 0 :(得分:2)
如果您希望一个表单将表单标记放在while循环之外
<?php
$sql = "SELECT IDBewoner, Foto FROM Bewoners LIMIT 5";
$res = mysqli_query($mysqli, $sql);
$totaalbewoners = mysqli_num_rows($res);
?>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="POST">
<?php while($row = mysqli_fetch_assoc($res)) {
?>
<tr>
<td><input type="text" value="<?php echo $row["IDBewoner"] ?>" name="bewoner"></td>
<td> <?php echo $row["IDBewoner"] ?> </td>
<td> <?php echo "<img src='data:image/jpeg;base64,".base64_encode($row['Foto'])."' width='90' height='90'>" ?> </td>
<td>
<div id="rang1" class="DropZone"></div>
<input type="hidden" value="1" name="rang_input[][]" id="rang1value" />
</td>
<td>
<div id="rang2" class="DropZone"></div>
<input type="hidden" value="2" name="rang_input[][]" id="rang2value" />
</td>
<td>
<div id="rang3" class="DropZone"></div>
<input type="hidden" value="3" name="rang_input[][]" id="rang3value" />
</td>
<td>
<div id="rang4" class="DropZone"></div>
<input type="hidden" value="4" name="rang_input[][]" id="rang4value" />
</td>
<td>
<div id="rang5" class="DropZone"></div>
<input type="hidden" value="5" name="rang_input[][]" id="rang5value" />
</td>
<td>
<div id="rang6" class="DropZone"></div>
<input type="hidden" value="6" name="rang_input[][]" id="rang6value" />
</td>
<td>
<div id="rang7" class="DropZone"></div>
<input type="hidden" value="7" name="rang_input[][]" id="rang7value" />
</td>
<?php };?>
<input type="submit" value="Save">
</form>
</tr>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
foreach ($_POST['rang_input'] as $key => $value) {
foreach ($value as $key1 => $value1) {
echo $value1 . "<br />";
}
}
}
?>
答案 1 :(得分:2)
尝试修改您的表单以包含您的IDBewoner
(无论一个好的独特字段是什么,此字段可能不是唯一的,我不知道,很难说)值在双数组中,类似于:
<?php
$sql = "SELECT IDBewoner, Foto FROM Bewoners LIMIT 5";
$res = mysqli_query($mysqli, $sql);
$totaalbewoners = mysqli_num_rows($res);
while($row = mysqli_fetch_assoc($res)) {
?>
<tr>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="POST">
<td>
<input type="text" value="<?php echo $row["IDBewoner"] ?>" name="bewoner[]">
</td>
<td>
<?php echo $row["IDBewoner"] ?>
</td>
<td>
<img src="data:image/jpeg;base64,<?php echo base64_encode($row['Foto']); ?>" width='90' height='90'>
</td>
<?php for($i=0; $i <= 7; $i++) {
$owner_id = $i.$row["IDBewoner"]; ?>
<td>
<div id="rang<?php echo $owner_id; ?>" class="DropZone"></div>
<input type="text" value="" id="rang<?php echo $owner_id; ?>input" name="rang[<?php echo $row["IDBewoner"]; ?>][<?php echo $i; ?>]" />
</td>
<?php }
} ?>
<td>
<input type="submit" name="submit" value="submit" />
</td>
</form>
</tr>
会给你一个类似于的数组(我没有填写任何值,因为它们都是空白的):
Array
(
[bewoner] => Array
(
[0] => 122840745
[1] => 1972681560
[2] => 971451812
[3] => 1882323675
[4] => 1165412341
[5] => 2086561009
[6] => 1194305395
[7] => 2031974218
[8] => 654763098
[9] => 1105106609
[10] => 399644802
)
[rang] => Array
(
[122840745] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
[1972681560] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
[971451812] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
[1882323675] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
[1165412341] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
[2086561009] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
[1194305395] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
[2031974218] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
[654763098] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
[1105106609] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
[399644802] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
)
[submit] => submit
)
这是你处理sql的方法。请注意,您应该清理或绑定值,但我没有包含这样的内容:
<?php
if(isset($_POST["submit"])) {
$sql['cols'] = "INSERT INTO `ActiviteitenSchema` (DagVanDeWeek, Bewoner, Activiteit, DatumAangemaakt, Rangnr) VALUES";
$i = 0;
foreach($_POST['rang'] as $key => $value) {
$z = 0;
foreach($value as $rang) {
if(!empty($rang)) {
$vals[$i][] = "(4, '".$_POST['bewoner'][$i]."', '".$rang."', '2015-04-14', '$z')";
}
$z++;
}
if(isset($vals[$i]))
$sql['vals'][] = implode(", ",$vals[$i]);
$i++;
}
if(isset($sql['vals'])) {
$statement = $sql['cols'].implode(", ",$sql['vals']);
$res = $mysqli->query($statement);
}
}
?>
答案 2 :(得分:1)
这是你想要做的吗?
$values = array;
for ($i=1; $i<=7; $i++)
{
$values [$i] = $_POST["rang".$i."value"];
}
答案 3 :(得分:1)
那怎么样?假设你的输入有id1 own1rang1value,own1rang2value等
$values = array;
for ($owner=1; $owner<=7; $owner++)
{
$rec = array;
for ($i=1; $i<=7; $i++)
{
$rec [$i] = $_POST["own".$owner."rang".$i."value"];
}
$values[$owner]=$rec;
}
答案 4 :(得分:1)
插入代码:
<?php
if(isset($_POST["submit"])) {
$sql['cols'] = "INSERT INTO ActiviteitenSchema (DagVanDeWeek, Bewoner, Activiteit, DatumAangemaakt, Rangnr) VALUES";
$i = 0;
foreach($_POST['rang'] as $key => $value) {
$z = 0;
foreach($value as $rang) {
if(!empty($rang)) {
$vals[$i][] = "(4, '".$_POST['bewoner'][$i]."', '".$rang."', '2015-04-14', '$z')";
}
$z++;
}
if(isset($vals[$i])) {
$sql['vals'][] = implode(", ",$vals[$i]);
}
$i++;
}
if(isset($sql['vals'])) {
$statement = $sql['cols'].implode(", ",$sql['vals']);
$res = $mysqli->query($statement);
echo "<script>alert('gegevens zijn toegevoegd')</script>";
header("Location: nieuwschema.php");
}
}
?>
fetch_assoc:
<div id="MainDiv">
<div id="ListBewoners">
<table id="ListBewonersUL">
<?php
$sql = "SELECT IDBewoner, Foto FROM Bewoners LIMIT 5";
$res = mysqli_query($mysqli, $sql);
$totaalbewoners = mysqli_num_rows($res);
while($row = mysqli_fetch_assoc($res)) {
?>
<tr>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="POST">
<td>
<input type="text" value="<?php echo $row["IDBewoner"] ?>" name="bewoner[]" size="2">
</td>
<td>
<?php echo $row["IDBewoner"] ?>
</td>
<td>
<img src="data:image/jpeg;base64,<?php echo base64_encode($row['Foto']); ?>" width='90' height='90'>
</td>
<?php for($i=0; $i < 7; $i++) {
$owner_id = $i.$row["IDBewoner"]; ?>
<td>
<div id="rang<?php echo $owner_id; ?>" class="DropZone"></div>
<input type="text" value="" id="rang<?php echo $owner_id; ?>input" name="rang[<?php echo $row["IDBewoner"]; ?>][<?php echo $i; ?>]" />
</td>
<?php }
} ?>
</tr><tr>
<td>
<input type="submit" name="submit" value="Save" />
</td>
</form>
</tr>
</table>
</div>
</div>
我将操作设为PHP_SELF
,并将插入代码放在底部的同一页面中。