我需要将来自后调用的插入更新为MYSQL DB中的相同数据。 这是表单代码:
<form action="salva_debiti.php" method="post">
'; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT * FROM rimanenti_mese WHERE Mese = $mese AND Anno = $anno ORDER BY cognome ASC"; $result = $conn->query($sql); if ($result->num_rows > 0) { echo '
<table id="ricambi" style="width: 100%;" border="1">
<tr>
<td id="titolotd">Nome</td>
<td id="titolotd">Cognome</td>
<td id="titolotd">Da incassare pezzi</td>
<td id="titolotd">Da incassare benzina</td>
<td id="titolotd">Residuo '.$mese_scritto.' '.$anno_pr_div.'</td>
</tr>'; while($row = $result->fetch_assoc()) { echo '
<tr>
<td>
<input type="" name="nome[]" value="'.$nome.'">
</td>
<td>
<input type="" name="cognome[]" value="'.$cognome.'">
</td>
<td>
<input type="" name="pezzi[]" value="'.$pezzi.'">
</td>
<td>
<input type="" name="benzina[]" value="'.$benzina.'">
</td>
<td>
<input type="" name="rimanenti[]" value="'.$rimanenti.'" </td>
</tr>
' ; } echo '
<input type="submit" value="Submit">'; echo '</form>';
这是我用来插入数据的代码:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn - > connect_error) {
die("Connection failed: ".$conn - > connect_error);
}
foreach($_POST['nome'] as $nome) {
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$rimanenti_ins = $_POST['rimanenti'];
//$sql = "UPDATE rimanenti_mese SET Rimanenti='$rimanenti_ins' WHERE Nome='$nome' AND Cognome = '$cognome'";
$sql = "INSERT INTO rimanenti_mese (Nome, Cognome, Mese, Anno, Benzina, Pezzi, Rimanenti)
VALUES('$nome', '$cognome', '09', '2015', 'xxxx', 'fdfdfd', '$rimanenti_ins')
";
}
if ($conn - > query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: ".$sql.
"<br>".$conn - > error;
}
$conn - > close();
问题是它只插入最后一行...... 我哪里错了?
答案 0 :(得分:2)
将$conn - > query($sql)
移至foreach
圈。
foreach($_POST['nome'] as $nome) {
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$rimanenti_ins = $_POST['rimanenti'];
$sql = ".....";
if ($conn - > query($sql) === TRUE) {
} else {
}
}
因为现在您只进行了一次查询,并且在foreach
循环结束后$sql
包含查询最后一项。
<强>更新强>:
单词Array
是数组值的字符串表示。
执行$nome = $_POST['nome']
时,$_POST['nome']
是数组,因此将数组添加到字符串会生成单词Array
。
因此,您应该在每次迭代时获取数组的每个项目,例如:
$i = 0;
foreach ($_POST['nome'] as $nome) {
$nome = $_POST['nome'][$i];
$cognome = $_POST['cognome'][$i];
$rimanenti_ins = $_POST['rimanenti'][$i];
// sql
// run query
$i++;
}
答案 1 :(得分:1)
您在foreach完成后调用$conn -> query($sql)
,这意味着在$sql
中只存储了foreach循环中的最后一个值。
试试这个:
// Create connection
$conn = new mysqli( $servername, $username, $password, $dbname );
// Check connection
if ( $conn->connect_error) {
die("Connection failed: ".$conn -> connect_error);
}
foreach ( $_POST['nome'] as $nome ) {
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$rimanenti_ins = $_POST['rimanenti'];
//$sql = "UPDATE rimanenti_mese SET Rimanenti='$rimanenti_ins' WHERE Nome='$nome' AND Cognome = '$cognome'";
$sql = "INSERT INTO rimanenti_mese (Nome, Cognome, Mese, Anno, Benzina, Pezzi, Rimanenti)
VALUES('$nome', '$cognome', '09', '2015', 'xxxx', 'fdfdfd', '$rimanenti_ins')
";
//put your ->query into the foreach loop
if ($conn -> query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: ".$sql.
"<br>".$conn -> error;
}
}
$conn -> close();