0

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?

4
  • 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. Oct 27, 2022 at 11:15
  • @JacobPeattie you should have left that as an answer :)
    – Tom J Nowell
    Oct 27, 2022 at 11:20
  • Also Minh, none of your echo calls have escaping, so 5 may be a valid value, but so is "/><script>alert("Minh didn't write secure code");</script>. Look up esc_attr, esc_url and esc_html. Same with $eng and $name and all the other places you echo a variable and trust it's safe without checking
    – Tom J Nowell
    Oct 27, 2022 at 11:22
  • @JacobPeattie thank you. the name was the issue and it solved.
    – Minh
    Oct 27, 2022 at 11:55

1 Answer 1

0

The problem is name[], specifically name. When you are choosing what to name your inputs, keep in mind that WordPress itself has inputs and by choosing "name" you're reusing something that's already in use.

E.g. example.com/mypage is converted into index.php?name=mypage behind the scenes, and used to fill up the main query. By reusing name you're putting something in the system that isn't meant to be there. It's expecting a string because page and post names are strings, but your form uses an array.

So rename your name[] input to something else that isn't already reserved. You can look up the WP_Query documentation to see all the options that class takes ( and that you should not use )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.