-1

我正在尝试从customer一张桌子到另一张桌子上获取客户ordertrackingID。-但是- 订单的添加还需要它从订单中获得的产品信息。我尝试了几种方法,但无法使其正常工作。还有一件奇怪的事:

//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);
4

4 回答 4

1

就像您首先获得客户数据一样:

$customerid = mysql_fetch_row(mysql_query("SELECT customerid FROM customer WHERE email='$email'")); 
echo $customerid[0];
于 2012-06-18T10:44:50.467 回答
1

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

于 2012-06-18T10:47:20.590 回答
1
$res = mysql_query("SELECT customerid FROM customer WHERE email='$email'");
while($row=mysql_fetch_array($res))
{
 $customerid=$row['customerid'];
}

插入 ordertracking 表时使用此 $customerid

于 2012-06-18T11:03:15.133 回答
1
$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();
        }   

// 然后插入订单表

于 2012-06-18T11:53:05.030 回答