plugins - How do I check if my $wpdb->insert() was successful?

I'm trying to write a little script within my footer.php (which I'll later transform into a plugin) that send

I'm trying to write a little script within my footer.php (which I'll later transform into a plugin) that send two form fields to a custom table (wp_newsletter). I'm already sending the form and writing to the table correctly, but I don't know how I can send a success or fail message back to the user. My current code is as follows:

<form method="post">
  <input type="text" name="user_name">Name
  <input type="text" name="user_email">Email
  <input type="submit">
  <?php echo $message;  ?>
</form>

<?php
global $wpdb;
$table   = $wpdb->prefix . "newsletter";
$name    = sanitize_text_field( $_POST["user_name"] );
$email   = sanitize_email( $_POST["user_email"] );
$message = "";


if( isset($_POST["submit"]) ) {
  if ( is_email($email) && isset($name)) {
    if ( $wpdb->insert( $table, array("name"  => $name, "email" => $email)) != false ) {
      $message = "Your subscription was sent.";
    }
  }
  else {
    if ( !is_email($email) ) {
        $message = "Invalid email address.";
    } elseif ( !isset($name) ) {
        $message = "The field name is mandatory.";
    } else {
        $message = "Both name and email fields are mandatory.";
    }
  }
} else {
    $message = "Please, try again later.";
}
?>


<?php wp_footer(); ?>

</body>
</html>

I (think) am testing it right, accordingly to the $wpdb docs which says that:

This function returns false if the row could not be inserted. Otherwise, it returns the number of affected rows (which will always be 1).

I'm trying to write a little script within my footer.php (which I'll later transform into a plugin) that send two form fields to a custom table (wp_newsletter). I'm already sending the form and writing to the table correctly, but I don't know how I can send a success or fail message back to the user. My current code is as follows:

<form method="post">
  <input type="text" name="user_name">Name
  <input type="text" name="user_email">Email
  <input type="submit">
  <?php echo $message;  ?>
</form>

<?php
global $wpdb;
$table   = $wpdb->prefix . "newsletter";
$name    = sanitize_text_field( $_POST["user_name"] );
$email   = sanitize_email( $_POST["user_email"] );
$message = "";


if( isset($_POST["submit"]) ) {
  if ( is_email($email) && isset($name)) {
    if ( $wpdb->insert( $table, array("name"  => $name, "email" => $email)) != false ) {
      $message = "Your subscription was sent.";
    }
  }
  else {
    if ( !is_email($email) ) {
        $message = "Invalid email address.";
    } elseif ( !isset($name) ) {
        $message = "The field name is mandatory.";
    } else {
        $message = "Both name and email fields are mandatory.";
    }
  }
} else {
    $message = "Please, try again later.";
}
?>


<?php wp_footer(); ?>

</body>
</html>

I (think) am testing it right, accordingly to the $wpdb docs which says that:

This function returns false if the row could not be inserted. Otherwise, it returns the number of affected rows (which will always be 1).

Share Improve this question asked Jul 20, 2016 at 19:10 vcamargovcamargo 1311 gold badge1 silver badge2 bronze badges 3
  • Where did you want the message to appear? You have your echo $message before the $message is created. – czerspalace Commented Jul 20, 2016 at 19:22
  • Hm, I was not sure if I could used it before declaration... But I wanted to show it under the form. – vcamargo Commented Jul 20, 2016 at 20:03
  • 1 use this !== FALSE and try – user93819 Commented Jul 21, 2016 at 2:24
Add a comment  | 

3 Answers 3

Reset to default 5

It returns either the number of rows inserted or false on error.

Maybe you can get id of inserted recode or false if the insert fails:

refer link: https://developer.wordpress/reference/classes/wpdb/insert/#return

So you can check like below:

$result_check = $wpdb->insert( $table, array("name"  => $name, "email" => $email));
if($result_check){
   //successfully inserted.
}else{
  //something gone wrong
}

When I realized that PHP is an acronym for "PHP Hypertext Preprocessor" -- emphasis on "preprocessor" -- I finally understood that I can't mix PHP and HTML and expect any kind of interactivity with the user. When a web page is served, the PHP creates the HTML and then the HTML is displayed by the browser. If the user does something the PHP must respond to, it must trigger a new PHP/HTML page to provide some sort of response. (An alternate would be to use AJAX to send data back and forth without loading a new page. Wordpress works well with AJAX and there are tutorials a google search away.)

For a simple form like yours, I would use javascript for error checking. If the form is not filled in, prevent form submission with JS. If the form is complete, the form's action can be a .php file that does the database insert and displays the success/failure message in HTML.

I came here via google and what helped me was using properties of $wpdb object:

$wpdb->last_error 

shows the last error if present

$wpdb->last_query 

shows the last query which gave the above error

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745596565a4635151.html

相关推荐

  • plugins - How do I check if my $wpdb-&gt;insert() was successful?

    I'm trying to write a little script within my footer.php (which I'll later transform into a plugin) that send

    13小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信