I am using a form to submit table inserted values using <input name=[]>
as $_POST
to a PHP page. It's a template page of wordpress and I am getting an error.
<?php
/*
Template Name: table page
*/
get_header();
global $wpdb;
$names= $wpdb-> get_results('select stdName FROM wp_nis_student');
?>
<form action="" method="post" >
<table>
<thead>
<tr>
<th>Student Name</th>
<th>English</th>
<th>Maths</th>
</tr>
</thead>
<tbody>
<?php foreach($names as $name ){?>
<tr>
<td><input type="text" name="name[]" value="<?php echo $name->stdName;>"/</td>
<td><input type="text" name="Engmark[]"/></td>
<td><input type="text" name="Mathmark[]"/></td>
</tr>
<?php }?>
</tbody>
</table>
<button type="submit" name="submit" value="Submit">Submit</button>
</form>
<?php
if(isset($_POST['submit'])){
if(isset($_POST['name'])&& isset($_POST['Engmark']) && isset($_POST['Mathmark'])){
echo "Names: <br/>";
foreach ( $_POST["name"] as $name )
echo $name . "<br/>";
echo "<br/><br/>" .
"English : <br/>";
foreach ( $_POST["Engmark"] as $eng )
echo $eng . "<br/>";
echo "<br/><br/>" .
"Maths: <br/>";
foreach ( $_POST["Mathmark"] as $mat )
echo $mat . "<br/>";
}
else echo "Error: no data";
}
?>
And I am getting this error because am calling isset($_POST['submit'])
Fatal error: Uncaught Error: trim(): Argument #1 ($string) must be of type string, array given in ..\wp-includes\class-wp-query.php on line 811
Then a made change at the line 811 in class-wp-query.php $qv['name'] = trim( $qv['name'] ); to $qv['name'] = trim( $qv['name[]'] ); it worked fine but i start getting warning on the website
Warning: Undefined array key "name[]" in ..\wp-includes\class-wp-query.php on line 811
How can I resolve it?
name
is a reserved term in WordPress and using it like this is causing conflicts because WordPress expects it to be a string. You’ll need to use a different name for that field.echo
calls have escaping, so5
may be a valid value, but so is"/><script>alert("Minh didn't write secure code");</script>
. Look upesc_attr
,esc_url
andesc_html
. Same with$eng
and$name
and all the other places youecho
a variable and trust it's safe without checkingname
was the issue and it solved.