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

从一个表到另一个表并按顺序添加PHP MYSQL,php,mysql,database,Php,Mysql,Database,我正在尝试将customerid从customer表获取到ordertracking表。 -但是-订单的添加还需要从订单表单获得的产品信息。我试过几种方法,但都没能奏效。 还有一件奇怪的事: //connect to database $connection = mysql_connect("localhost","root","") or die ("Can't connect"); mysql_select_db("shoppingcart", $connection) or die ("C

我正在尝试将customerid从customer表获取到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);

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

$customerid = mysql_fetch_row(mysql_query("SELECT customerid FROM customer WHERE email='$email'")); 
echo $customerid[0];
在insert之后使用mysql\u insert\u id,或者在select之后使用$rows['customerid']

$res = mysql_query("SELECT customerid FROM customer WHERE email='$email'");
while($row=mysql_fetch_array($res))
{
 $customerid=$row['customerid'];
}
在插入ordertracking表时使用此$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();
        }   

//然后插入到订单表中

我试过了,但当我回显它时,它显示了资源id 5。但在customer表中customerid是2而不是5$customerid=mysql\u query从客户中选择customerid,其中email='$email';echo$customerid;在ordertracking表中,它仍然显示“0”。为什么?当然,您仍然需要获取结果,请参阅更新的答案。AAAAAA和。。。。。。。它起作用了!非常感谢你的剧本。我现在在ordertracking中获取customerid。谢谢谢谢你的剧本,但是帕杜的剧本很好用。不管怎么说,谢谢你,很抱歉你得到了一个$rows=mysql\u fetch\u assoc$结果;