Jump to content

php insert records mysql indexed db


paynod

Recommended Posts

Hi,I'm hoping someone can help me:).  When I add foreign keys to the database in MySQL I CAN change the primary key field and the corresponding indexed field will automatically change giving me the same value which is exactly what i want. The issue is when I try to insert the values using my php code i get the 'cannot update child error'. I have not selected a value for the unique_id field as I want it to inherit the value from the primary key. Here's my code as simplified as I could do it.

 

<?php  //this calls the class productupload and instantiates the function insert_form to insert values into the db.

 

$product = new productUpload();

$product->insert_form();?>

//This calls the insert_form function and inserts info into tables

<?php

class productUpload extends DatabaseObject{

protected static $table_name="itm_details";

protected static $db_fields =array('id','unique_id','itm_cat','itm_make','itm_model','itm_desc','itm_cond','itm_date_from','itm_date_to','itm_add_date');     

 

 

    public $id;

    public $unique_id;

    public $itm_cat;

    public $itm_make;

    public $itm_model;

    public $itm_desc;

    public $itm_cond;

    public $itm_date_from;

    public $itm_date_to; 

    public $itm_add_date;

Heres the function insert_form which grabs the form submitted data. Note I have not defined a value for $unique_id

 

public function insert_form(){ 

global $database; 

//set the object attributes to the form the parameters

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

        $result = array(

        $this->itm_cat =(!empty($_POST['itm_cat'])) ? trim($_POST['itm_cat']) : NULL,

        $this->itm_make =(!empty($_POST['itm_make'])) ? trim($_POST['itm_make']) : NULL,     

        $this->itm_model = (!empty($_POST['itm_model'])) ? trim($_POST['itm_model']) : NULL,                                 

        $this->itm_desc =(!empty($_POST['itm_desc'])) ? trim($_POST['itm_desc']) : NULL,

        $this->itm_cond =(!empty($_POST['itm_cond'])) ? trim($_POST['itm_cond']) : NULL,

        $this->itm_date_from =(!empty($_POST['itm_date_from'])) ? trim($_POST['itm_date_from']) : NULL,

        $this->itm_date_to =(!empty($_POST['itm_date_to'])) ? trim($_POST['itm_date_to']) : NULL,

        $this->itm_add_date = date("Y-m-d H:m:s"));

//check date is numeric

        //if(is_numeric($_POST['itm_date_from'])) {

        //$this->itm_date_from = $_POST['itm_date_from'];

//}   

//check date is numeric                       

        //if(is_numeric($_POST['itm_date_to'])) {

        //$this->itm_date_to = $_POST['itm_date_to'];

        //}

        if($result){

        $result = $this->create();

        }

        }

        }

 

//here's the create function referred to above which sanitises the posted data and inserts it into the db.

 

 

public function create() {

        global $database;

        $attributes = $this->sanitised_attributes();

 

        $sql = "INSERT INTO ".self::$table_name."(";

        $sql .= join(", ", array_keys($attributes));

        $sql .= ") VALUES ('";

        $sql .= join("', '", array_values($attributes));

        $sql .= "')";

        if($database->query($sql)) {

        $this->id = $database->insert_id();

        return true;

        } else {     

        return false;

        }

 

}

Link to comment
https://forums.phpfreaks.com/topic/229312-php-insert-records-mysql-indexed-db/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.