如何从表中获取数据并将其插入另一个表?

时间:2014-05-30 23:47:34

标签: php mysql sql phpmyadmin localhost

checkorder.php

<?php include("koneksi.php");mysql_select_db("project2");
$code = $_POST['code'];
$name = $sql = "Select name from 'orders' where 'code' = '<?php $_POST['code'] ?>' ";
$amount = $_POST['amount'];
$nameorder = $_POST['nameorder'];
$email  = $_POST['email'];  
$address= $_POST['address'];    
$price  = $sql= "Select price from 'orders' where 'code' = '<?php $_POST['code'] ?>' ";
$totalprice = $price * $_POST['amount'];
    $sql = " INSERT INTO orders (idorder, date, nameorder, email, code, name, amount, price, total price)
    VALUES 
    (default, NOW(), '$code', 'name','$amount','$nameorder','$email','$address')";
    mysql_query($sql) ; ?>

我想使用php插入数据。 代码,名称,价格从表产品获得,是否可以使用这种方式? 因为当我尝试输入数据时,它总是在$ name中显示错误。

1 个答案:

答案 0 :(得分:0)

首先使用查询选择然后获取您想要的内容

$code = $_POST['code'];
$query = "Select * from `orders` where `code` = '$code' ";
$result = mysql_query($query);
$name = mysql_fetch_assoc($result)['name']; //get the name
$amount = $_POST['amount'];
$nameorder = $_POST['nameorder'];
$email  = $_POST['email'];  
$address= $_POST['address'];.....................  
相关问题