Support » Plugins and Hacks » Hacks » $wpdb insert

  • 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.

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • To insert form records use as wpdb->insert as follows:

    $wpdb->insert(
    	'table',
    	array(
    		'column1' => 'value1',
    		'column2' => 123
    	),
    	array(
    		'%s',
    		'%d'
    	)
    );

    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

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

    this is my code
    <?php

    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.

    Thanks

    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:

    require_once('../../../wp-load.php');

    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

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘$wpdb insert’ is closed to new replies.