我正在尝试更新表单中的多个行/行。我收到错误Notice: Array to string conversion in C:\wamp.....
我还收到了另一个错误Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\....
这两个现在都已修复。
表格
$ad = "<form action='updaterecords.php' method='post' id='update'> \n";
$records = $this->query_recs();
foreach ($records as $record) {
$ad .= "<p id='id{$record->id}'>";
$ad .= "<input type='hidden' name='id[]' value='" .$this->render_i18n_data($record->id) . "' />\n";
$ad .= "<input type='text' name='paname[]' value='". $this->render_i18n_data($record->pa_name) . "' class='paname' />\n";
$ad .= "<input type='text' name='pcname[]' value='". $this->render_i18n_data($record->pc_name) . "' class='pcname' />\n";
$ad .= "<input type='text' name='pdname[]' value='". $this->render_i18n_data($record->pd_name) . "' class='pdname' />\n";
$ad .= "<input type='text' name='pfname[]' value='". $this->render_i18n_data($record->pf_name) . "' class='pfname' />\n";
$ad .= "<input type='submit' name='update' value='Update' />";
$ad .= "</p>";
}
echo($ad);
PHP
<?php
include 'dbdetails.php';
$con = new mysqli($server, $user, $pass, $db);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
echo "Connected successfully";
if(isset($_POST['update'])){
$id = $_POST['id'];
$paname = $_POST['paname'];
$pcname = $_POST['pcname'];
$pdname = $_POST['pdname'];
$pfname = $_POST['pfname'];
mysqli_query($con, "UPDATE wp_pbcbc_records
SET pa_name = '$paname', pc_name='$pcname', pd_name='$pdname', pf_name='$pfname'
WHERE id = '$id' ");
header("location: localhost/myp");
exit;
}
?>
更新:现在已经解决了。感谢那些给我答案的人!
答案 0 :(得分:2)
由于您在表单中使用以[]
结尾的名称,$_POST
变量将成为数组,您必须循环它们。
$stmt = mysqli_prepare("UPDATE wp_pbcbc_records
SET pa_name = ?, pc_name=?, pd_name=?, pf_name=?
WHERE id = ? ");
mysqli_stmt_bind_param($stmt, "ssssi", $cur_paname, $cur_pcname, $cur_pdname $cur_pfname, $cur_id);
for ($i = 0; $i < count($id); $++) {
$cur_paname = $paname[$i];
$cur_pcname = $pcname[$i];
$cur_pdname = $pcname[$i];
$cur_pfname = $pcname[$i];
$cur_id = $id[$i];
mysqli_stmt_execute($stmt);
}
答案 1 :(得分:1)
isset()
函数之上,必须没有HTML实体的输出,这是header()
函数失败并导致错误的常见原因。您与数据库的连接:
include("dbdetails.php");
$con = new mysqli($server, $user, $pass, $db); /* MAKE SURE THAT THE VALUES OF YOUR VARIABLES ARE CORRECT CORRESPONDING TO YOUR DATABASE */
/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
您的更新代码:
/* THERE MUST HAVE NO OUTPUT IN THIS SECTION TO FIX THE HEADER ERROR */
if(isset($_POST['update'])){
$counter = count($_POST["paname"]);
for($x = 0; $x<=$counter; $x++){
if(!empty($_POST["paname"][$x])){
$id = mysqli_real_escape_string($con,$_POST['id'][$x]);
$paname = mysqli_real_escape_string($con,$_POST['paname'][$x]);
$pcname = mysqli_real_escape_string($con,$_POST['pcname'][$x]);
$pdname = mysqli_real_escape_string($con,$_POST['pdname'][$x]);
$pfname = mysqli_real_escape_string($con,$_POST['pfname'][$x]);
mysqli_query($con, "UPDATE wp_pbcbc_records
SET pa_name = '$paname', pc_name='$pcname', pd_name='$pdname', pf_name='$pfname'
WHERE id = '$id' ");
} /* END OF IF CHECKING paname */
} /* END OF FOR LOOP */
header("location: localhost/myp");
exit;
} /* END OF ISSET */
?>
旁注:
mysqli_real_escape_string()
来清理变量值,以防止SQL injections。mysqli_* prepared statement
。它会自动清理变量,不需要为每个变量转义字符串。您的代码使用mysqli_* prepared statement
:
/* THERE MUST HAVE NO OUTPUT IN THIS SECTION TO FIX THE HEADER ERROR */
if(isset($_POST['update'])){
$counter = count($_POST["paname"]);
for($x = 0; $x<=$counter; $x++){
if(!empty($_POST["paname"][$x])){
if($stmt = $con->prepare("UPDATE wp_pbcbc_records SET pa_name=?, pc_name=?, pd_name=?, pf_name=? WHERE id=?")){
$stmt->bind_param("ssssi",$_POST["paname"][$x],$_POST["pcname"][$x],$_POST["pdname"][$x],$_POST["pfname"][$x],$_POST["id"][$x]);
$stmt->execute();
$stmt->close();
} /* END OF PREPARED STATEMENT */
} /* END OF IF CHECKING paname */
} /* END OF FOR LOOP */
header("location: localhost/myp");
exit;
} /* END OF ISSET */
?>