I am using wpdb but it not working perfectly.but if I dont use form data its work

admin2025-06-06  2

 <?php

 if (isset($_POST['submit'])) {
 global $wpdb;

$table_name = $wpdb->prefix . "table_booky";
$wpdb->insert($table_name, array(
    "Name" => $_POST['name'],
    "dep" =>  $_POST['age'];
    "age"  =>  $_POST['dep'];
 ));

  }
  ?>
 <form class="" action="<?php home_url(); ?>" method="POST">


    <input type="text" name="name" value="">
    <input type="text" name="age" value="">
    <input type="text" name="dep" value="">
    <input type="submit" name="submit" value="submit">

 </form>
 <?php

 if (isset($_POST['submit'])) {
 global $wpdb;

$table_name = $wpdb->prefix . "table_booky";
$wpdb->insert($table_name, array(
    "Name" => $_POST['name'],
    "dep" =>  $_POST['age'];
    "age"  =>  $_POST['dep'];
 ));

  }
  ?>
 <form class="" action="<?php home_url(); ?>" method="POST">


    <input type="text" name="name" value="">
    <input type="text" name="age" value="">
    <input type="text" name="dep" value="">
    <input type="submit" name="submit" value="submit">

 </form>
Share Improve this question edited Nov 28, 2018 at 21:17 shanebp 5,0857 gold badges28 silver badges40 bronze badges asked Nov 28, 2018 at 19:39 ReazReaz 1 2
  • It's hard to tell from your post - if you could go back please and wrap everything in code, which looks like a curly braces symbol, that should help - but it looks like in your array you have semicolons where you need to use commas instead. – WebElaine Commented Nov 28, 2018 at 20:15
  • Welcome to WPSE, Reaz. You may want to consider revising your question for clarity. Make your title a little more concise and be descriptive in the content area. Right now all you really have is a code snippet without a description of what is not working for you. – butlerblog Commented Nov 28, 2018 at 21:53
Add a comment  | 

1 Answer 1

Reset to default 0

While your question isn't entirely clear on the issue you're having, I'm guessing that it's the way that you defined the elements of the array you were trying to insert.

You had a mix of commas and semicolons separating the elements.

I revised your code to correct that - and to aid readability of your code so you can see it, I pulled this out into a variable - $data_array.

<?php

if ( isset( $_POST['submit'] ) ) {
    global $wpdb;

    $table_name = $wpdb->prefix . "table_booky";

    $data_array = array(
        "Name" => $_POST['name'],
        "dep"  => $_POST['age'],  // Note the comma, and not semicolon
        "age"  => $_POST['dep'],  // Same here...
    );

    $wpdb->insert( $table_name, $data_array );

}
?>
<form class="" action="<?php home_url(); ?>" method="POST">


<input type="text" name="name" value="">
<input type="text" name="age" value="">
<input type="text" name="dep" value="">
<input type="submit" name="submit" value="submit">

</form>

Now, this doesn't address other issues such as the fact that you are not sanitizing your input data. So you should look into that after you get your code working.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749143924a316740.html

最新回复(0)