WordPress.org

Ready to get started?Download WordPress

Forums

$wpdb insert (5 posts)

  1. redds013
    Member
    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.

    Thanks

  2. Chandan Kumar
    Member
    Posted 1 year ago #

    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

  3. redds013
    Member
    Posted 1 year ago #

    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

  4. Chandan Kumar
    Member
    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:

    require_once('../../../wp-load.php');
  5. chrispybacon
    Member
    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

Tags

No tags yet.