简体   繁体   中英

form data insert in database with one foreign key columns in php

I am trying to insert contact form into database.

1.) I am trying to insert contact form without foreign key working fine.

2.) But form data insert with other table foreign key columns not working.

control.php

if(isset($_REQUEST['submit']))
            {
                $co_name=$_REQUEST['co_name'];
                $co_email=$_REQUEST['co_email'];
                $co_subject=$_REQUEST['co_subject'];
                $co_comment=$_REQUEST['co_comment'];
                
                $data=array("co_name"=>$co_name,"co_email"=>$co_email,"co_subject"=>$co_subject,"co_comment"=>$co_comment);
                $res=$this->insert('customer_contact',$data);
                if($res)
                {
                    echo"sucess";
                }
                else
                {
                    echo "Wrong";
                }
            }

This is model.php code

<?php 

class model
{
    public $conn="";
    function __construct()
    {
        $this->conn=new MySQLi("localhost","root","mohit@75","new_healthy_life");
    }
    function select($tbl)
    {
        $sel="SELECT * FROM $tbl";
        $exc=$this->conn->query($sel);
        while($fetch=$exc->fetch_object())
        {
            $arr[]=$fetch;
        }
        return $arr;
    }
    function insert($tbl,$data)
    {
        $col_arr=array_keys($data);
        $col=implode(",",$col_arr);
        
        $value_arr=array_values($data);
        $value=implode("','",$value_arr);
        
        echo $insert="INSERT INTO `$tbl`($col) value('$value')";
        $exc=$this->conn->query($insert);
        
        return $exc;
    }
}

Below contact table image

在此处输入图像描述

My Actual question is form data how to insert with foreign key.

Show image:-

在此处输入图像描述

How to solve this...

Thanks...

I will just modify your insert function and return inserted id:

function insert($tbl,$data)
    {
        $col_arr=array_keys($data);
        $col=implode(",",$col_arr);

        $value_arr=array_values($data);
        $value=implode("','",$value_arr);

        echo $insert="INSERT INTO `$tbl`($col) value('$value')";
        $exc=$this->conn->query($insert);
        $last_id = $this->conn->insert_id;
        return $last_id;
    }

Now in your control.php

if(isset($_REQUEST['submit']))
{
    $co_name=$_REQUEST['co_name'];
    $co_email=$_REQUEST['co_email'];
    $co_subject=$_REQUEST['co_subject'];
    $co_comment=$_REQUEST['co_comment'];

    $data=array("co_name"=>$co_name,"co_email"=>$co_email,"co_subject"=>$co_subject,"co_comment"=>$co_comment);
    $res=$this->insert('customer_contact',$data);
    if($res)
    {
        //
        $data=array("foreignkey_col1"=>$foreignkey_col1_data,"foreign_key"=>$res);
        $res1=$this->insert('foreign_key_table',$data);
        if($res1)
        {
            //inserted into master table as well
        }
        else
        {
        //issue while inserting into master table
        }
    }
    else
    {
        echo "Wrong";
    }
}

I am not sure about your other table name of foreign key so I have just kept static and sample data but you see now $res will return inserted id and that you can use to insert into another table name as well by writing another insert query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM