• Hello WordPress admin,

    I have created the form and insert data into database, after submit the “alert box” it says unable to save. this is my code in function.php

    if (isset($_POST['submitbtn'])) {
    		$data = array(
    			'title' => $_POST['title'],
    			'author' => $_POST['author'],
    		);
    		$table_name ='mybook';
    
    		$result = $wpdb->insert($table_name, $data, $format=null);
    
    		if ($result==1)
    		 {
    			echo "<script>alert('Book Saved');</script>";
    		}else{
    			echo "<script>alert('Unable to Save');</script>";
    		}
    
    	}
    
    and this my code to my frontpage.php
    
    <form  method="post" >
                  <div class="form-group">
                    <input id="title" type="text" name="title" placeholder="Book Title" class="form-group input-sm">
                  </div>  
    
                  <div class="form-group">
                    <input id="author" type="text" name="author" placeholder="author" class="form-group input-sm">
                  </div>
                  <div class="row justify-content-center">
                    <div class="col-xs-4 col-sm-4 col-md-4">
                      <input type="submit" value="Submit" class="btn btn-info btn-block" name="submitbtn">
                    </div>
                  </div>
               </form>
    • This topic was modified 5 years, 9 months ago by andz89.
    • This topic was modified 5 years, 9 months ago by Jan Dembowski. Reason: Formatting
Viewing 6 replies - 1 through 6 (of 6 total)
  • @andz89,

    I hope your database is using some table prefix. Can you please make sure you add table prefix in your table name ?

    You can also check the SQL error using $wpdb->print_error();

    Thanks

    Thread Starter andz89

    (@andz89)

    Hi Dnesscarkey,

    Thank you for your reply.
    I tried this prefix name to my table name “$wpdb->prefix .’mybooks'”
    the result is the same. unable to save.
    I hope you can help me for this.

    Have you tried checking the error using $wpdb->print_error(); ?

    Thread Starter andz89

    (@andz89)

    how to check using $wpdb->print_error(); ? where I put that?

    After your wpdb insert code.

    Thread Starter andz89

    (@andz89)

    I insert $wpdb->print_error(); after my wpdb code but the result is same. unable to save.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Insert data in database using form’ is closed to new replies.