-1

customerテーブルからテーブルへの顧客IDを取得しようとしていordertrackingます。-しかし- 注文の追加には、注文フォームから取得した製品情報も必要です。いくつかの方法を試しましたが、うまくいきません。また、別の奇妙なこと:

//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

mysql_insert_id()挿入後または選択後に使用$rows['customerid']します。

于 2012-06-18T10:47:20.590 に答える
1

最初に顧客データを取得したのと同じように:

$customerid = mysql_fetch_row(mysql_query("SELECT customerid FROM customer WHERE email='$email'")); 
echo $customerid[0];
于 2012-06-18T10:44:50.467 に答える
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 に答える
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 に答える