Inside Array – “unidentified index” error with “prepare was called incorrectly” despite not calling the prepared statement with wordpress

I know that there is a very old answer that details unidentified index but using the methods there I can not figure out how to make isset work with the code I have. It is below but doesnt solve the issue.

Plus it my error is paired with the PREPARE error AND the fact that my understanding is that answer with ISSET is NOT a safe way anymore to prevent SQL injections. Someone please correct me if I am wrong. As per request I have also included the entire error message.

I am getting a couple of errors with the code below and am not sure why it thinks this isnt prepared properly. Can anyone shed light on this for me? Thanks.

for each line that has a $POST, it is telling me that the “Notice: Undefined index”

     if (!empty($_POST)) {
        global $wpdb;
        $table = "mytablenameplaceholder";
            if(isset($_POST['name']) && isset($_POST['emailaddress'])){
  $number1 = $_POST['name']; 
  $message = $_POST['emailaddress']; 
  $message = urlencode($message); 
        $data = array(
            'name' => $_POST['name'],
            'emailaddress'    => $_POST['emailaddress'],
            'phonenumber'    => $_POST['phonenumber']
        );
        $format = array(
            '%s',
            '%s'
        );
        $success=$wpdb->insert( $table, $data, $format );
        if($success){
            echo 'data has been saved' ; 
        }
    } else {
?>
    <form action="" method="post" >
        <input type="text"  id="name" name="name" value="John">
        <input type="text" name="email">
        <input type="text" name="phonenumber">
        <input type="submit">
    </form>
<?php
    }

I am also receiving a “Notice: wpdb::prepare was called incorrectly.” notice despite not having a prepare statement currently in the code. I know I need to add it.

Inside Array - "unidentified index" error with "prepare was called incorrectly" despite not calling the prepared statement with wordpress

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I saw in your code the $_POST[‘phonenumber’] doesn’t check isset so may be this is an error you try to find. I think you can try this

if (!empty($_POST)) {
        global $wpdb;
        $table = "mytablenameplaceholder";
            if(isset($_POST['name']) && isset($_POST['emailaddress'])){
  $number1 = $_POST['name']; 
  $message = $_POST['emailaddress']; 
  $message = urlencode($message); 

  //process phonenumber
  $phonenumber= '';
  if(isset($_POST['phonenumber'])){
    $phonenumber = $_POST['phonenumber'];
  }
  //END - process phonenumber

        $data = array(
            'name' => $_POST['name'],
            'emailaddress'    => $_POST['emailaddress'],
            'phonenumber'    => $phonenumber
        );
        $format = array(
            '%s',
            '%s'
        );
        $success=$wpdb->insert( $table, $data, $format );
        if($success){
            echo 'data has been saved' ; 
        }
    } else {
?>
    <form action="" method="post" >
        <input type="text"  id="name" name="name" value="John">
        <input type="text" name="email">
        <input type="text" name="phonenumber">
        <input type="submit">
    </form>
<?php
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x