mysql - Update form row in php error -
i'm trying update multiple row/rows in form. i'm getting error notice: array string conversion in c:\wamp.....
i'm getting error warning: cannot modify header information - headers sent (output started @ c:\wamp\....
both of these fixed.
form
$ad = "<form action='updaterecords.php' method='post' id='update'> \n"; $records = $this->query_recs(); foreach ($records $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' id = '$id' "); header("location: localhost/myp"); exit; } ?>
update: has been solved. people gave me answer!
note:
- you passed values in array. must run them in loop before using them in query.
- on top of
isset()
function, there must have no output of html entities, common reasonheader()
function fail , cause error.
your connection database:
include("dbdetails.php"); $con = new mysqli($server, $user, $pass, $db); /* make sure values of variables correct corresponding database */ /* check connection */ if (mysqli_connect_errno()) { printf("connect failed: %s\n", mysqli_connect_error()); exit(); }
your updated code:
/* there must have no output in section fix 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' id = '$id' "); } /* end of if checking paname */ } /* end of loop */ header("location: localhost/myp"); exit; } /* end of isset */ ?>
on side note:
- you must use
mysqli_real_escape_string()
sanitize values of variables before using them in query prevent sql injections. - better recommendation, use
mysqli_* prepared statement
. sanitize variables automatically , no need escape strings per variable.
your code using mysqli_* prepared statement
:
/* there must have no output in section fix 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=? 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 loop */ header("location: localhost/myp"); exit; } /* end of isset */ ?>