Ready to get started?Download WordPress


$wpdb insert (5 posts)

  1. redds013
    Posted 1 year ago #

    Hi I have been trying for days to get this working and I must be missing something,

    I have a custom table called hotels in my database. I have created a form for all the hotels info.

    Basically I want to be able to put the infomation from the form to the database.

    this is what I have used so far.

    $wpdb->query("INSERT INTO hotels (name,address,contact, Price) VALUES ('$hname', '$address', '$hcontact','$hcost')");..

    This is a pure php question but how do I get image links stored in the table as well.


  2. Chandan Kumar
    Posted 1 year ago #

    To insert form records use as wpdb->insert as follows:

    		'column1' => 'value1',
    		'column2' => 123

    You can get the image link by using select query in $wpdb->get_results function.

    For more reference: http://codex.wordpress.org/Class_Reference/wpdb

  3. redds013
    Posted 1 year ago #

    Thanks I have tried it and it hasn't worked.

    this is my code

    if (isset($_POST['submiited'])){

    $hname= $_POST['name'];
    $haddress= $_POST['address'];
    $hcontact= $_POST['contact'];
    $hcost= $_POST['cost'];
    global $wpdb;
    $wpdb->insert('hotel',array('name' => '$hname','Price' => $address),array('%s','%d'));

    I am not sure exatly why its not working, I don't get errors but nothing inserts into the database.


  4. Chandan Kumar
    Posted 1 year ago #

    You are passing string in price field and marked price as digit in next array. please check this and make sure if you are including the wp_load.php in your file as follows:

  5. chrispybacon
    Posted 1 year ago #

    Probably a bit late for this but you have a typo

    $wpdb->insert('hotel',array('name' => '$hname','Price' => $address),array('%s','%d'));

    Should be haddress

    $wpdb->insert('hotel',array('name' => '$hname','Price' => $haddress),array('%s','%d'));

    Probably worth turning on php errors

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.