Php 从一个表中获取数据,并将其与新信息一起放入另一个表中

Php 从一个表中获取数据,并将其与新信息一起放入另一个表中,php,mysql,database,webforms,Php,Mysql,Database,Webforms,我有两张桌子。第一个是客户,第二个是发票,两者都有一行“客户ID”。当通过web表单添加新发票时,我如何使其从客户处获取“客户ID”,并在提交表单时将其与新信息一起放入发票中的“客户ID” 这是我的index.php中的代码 ?php include 'DBConfig.php'; //Insert or Update contact information if(isset($_POST['action_type'])) { if ($_POST['action_type'] == 'add'

我有两张桌子。第一个是客户,第二个是发票,两者都有一行“客户ID”。当通过web表单添加新发票时,我如何使其从客户处获取“客户ID”,并在提交表单时将其与新信息一起放入发票中的“客户ID”

这是我的index.php中的代码

?php
include 'DBConfig.php';
//Insert or Update contact information
if(isset($_POST['action_type']))
{
if ($_POST['action_type'] == 'add' or $_POST['action_type'] == 'edit')
{
    //Sanitize the data and assign to variables
    $invum = mysqli_real_escape_string($link, strip_tags($_POST['invnum']));
    $cost = mysqli_real_escape_string($link, strip_tags($_POST['cost']));

    if ($_POST['action_type'] == 'add')
    {
        $sql = "insert into invoice set 
                cost = '$cost'";
    }else{
        $sql = "update invoice set 
                cost = '$cost',
                where invnum = $invnum";
    }


    if (!mysqli_query($link, $sql))
    {
        echo 'Error Saving Data. ' . mysqli_error($link);
        exit(); 
    }
}
header('Location: invoicelist.php');
exit();
}
//End Insert or Update contact information

//Start of edit contact read
$gresult = ''; //declare global variable
if(isset($_POST["action"]) and $_POST["action"]=="edit"){
$id = (isset($_POST["ci"])? $_POST["ci"] : '');
$sql = "select invnum, cost from invoice 
        where invnum = $id";

$result = mysqli_query($link, $sql);

if(!$result)
{
    echo mysqli_error($link);
    exit();
}

$gresult = mysqli_fetch_array($result);

include 'invoiceupdate.php';
exit();
}
//end of edit contact read

您的数据位于
$gresult
中。只需相应地插入新数据即可。@sgtBOSE恐怕我没有跟上你。我对PHP和MySql非常陌生。我已经更新了我的原始问题,以显示我的整个index.phpTry,从而将您的示例限制为最小、可验证和完整:。在这里,您的索引相当大-仅限于您的问题:-)