简体   繁体   English

从一个表到另一个表并按顺序添加PHP MYSQL

[英]from one table to another table and order adding PHP MYSQL

I'm trying to get the customerid from customer table to ordertracking table. 我正在尝试从customer表到ordertracking表获取客户ordertracking -But- The adding of the order also needs the product information it gets from the orderform. -但是-订单的添加还需要从订单中获取的产品信息。 I've tried few ways, but can't get it to work. 我尝试了几种方法,但是无法正常工作。 Also another strange thing: 另外一件奇怪的事情:

//connect to database
$connection = mysql_connect("localhost","root","") or die ("Can't connect");
mysql_select_db("shoppingcart", $connection) or die ("Can't connect");  

//check if already customer
$result = mysql_query("SELECT * FROM customer WHERE email='$email'");
$rows = mysql_num_rows($result);

    if ($rows) 
    {
      echo '<br>Welcome back ' . $name .' '. $surname. '<br>';
    }
    else
    {
        //if new customer, add to database
        $customer = "INSERT INTO customer (customerid, name, surname, email, city, GETalcode, phonenumber) VALUES ('', '$name', '$surname', '$email', '$city', '$postalcode', '$phonenumber')";
        if (!mysql_query($customer,$connection))
        {
            die('Error: ' . mysql_error());
            echo "Sorry, there was an error";
        }
        echo "New customer added" . "<br />";
        echo '<br>Welcome as our new customer ' . $name . ' '. $surname;

        mysql_close($connection);   
    }

//connect to database
$connection = mysql_connect("localhost","root","") or die ("Can't connect");
mysql_select_db("shoppingcart", $connection) or die ("Can't connect");

//get customer id
????????????????????
    //add new order
    $ordertracking = "INSERT INTO ordertracking (orderid, customerid, productid, brand, model, price, amount, totalcost) VALUES ('', '$customerid', '$productid', '$brand', 'model', 'price', 'amount', 'totalcost')";
    if (!mysql_query($ordertracking,$connection))
        {
            die('Error: ' . mysql_error());
            echo "Sorry, there was an error";
        }
        echo "New order added" . "<br />";

        mysql_close($connection);

Just like you got the customer data in first place: 就像您首先获得客户数据一样:

$customerid = mysql_fetch_row(mysql_query("SELECT customerid FROM customer WHERE email='$email'")); 
echo $customerid[0];

在插入后使用mysql_insert_id()或在选择后使用$rows['customerid']

$res = mysql_query("SELECT customerid FROM customer WHERE email='$email'");
while($row=mysql_fetch_array($res))
{
 $customerid=$row['customerid'];
}

use this $customerid when inserting into the ordertracking table 在插入订单跟踪表时使用此$ customerid

$customer = "INSERT INTO customer (customerid, name, surname, email, city,
             GETalcode, phonenumber) VALUES 
            ('', '$name', '$surname', '$email', '$city',
             '$postalcode', '$phonenumber')";
        if (!mysql_query($customer,$connection))
        {
            die('Error: ' . mysql_error());
            echo "Sorry, there was an error";
        }
        else
        {
            $customer_id =mysql_insert_id();
        }   

// then insert into order tables //然后插入订单表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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