2013-05-11 66 views
-1

我遇到了问题,我在MYSQL中创建了两个表,其中一个表是bookticket,另一个表是客户表。将数据插入通过外键连接到另一个表的数据

客户表 CUSTOMER_ID(PK),全名,matric_id,ic_no,性别,地址,邮编,国家,hp_number,tel_number,用户名,密码,repeatpassword。

booktickect表 TICKET_ID(PK),衬板,方向,从,f_date,f_time,目的地,t_date,t_time,TOTAL_PRICE,CUSTOMER_ID(FK)

所以,现在我已经注册之后和登录作为用户,我如何创建一个php代码来输入bookticket数据,根据我在我的bookticket表中实现的customer_id外键,它将自动分配给当前登录的用户。敬请提前感谢。

对不起,我没有提供我的php代码在我以前post.I编辑了我的问题。 :)

<?php 

echo "<h1>Booking</h1>"; 

$submit = $_POST['submit']; 

//ticket booking form data 

$liner  = strip_tags($_POST['liner']); 
$direction  = strip_tags($_POST['direction']); 
$from  = strip_tags($_POST['from']); 
$f_date  = strip_tags($_POST['f_date']); 
$f_time  = strip_tags($_POST['f_time']); 
$destination = strip_tags($_POST['destination']); 
$t_date  = strip_tags($_POST['t_date']); 
$t_time  = strip_tags($_POST['t_time']); 
$total_price  = strip_tags($_POST['txttotal']); 

if ($submit) 
{ 

//check the direction selected by user 

if ($direction == "return") 
{ 

//check for existance 

    if($liner && $direction && $from && $f_date && $f_time && $destination && $t_date &&  $t_time && $total_price) 
      { 

      $connect = mysql_connect("localhost" ,"cc11205","11205"); 
       mysql_select_db("cc11205"); 

       $queryreg = mysql_query(" 

       INSERT INTO bookticket VALUES ('','$liner','$direction','$from','$f_date','$f_time','$destination','$t_date','$t_time','$total_price',''); 


       "); 

       die("Your booking details has been succesfully inserted into the database.Please <a href='customer.php'>Click here</a> to go to the main page to view your booking"); 

      } 

     else 

      echo "Please fill in <b>all</b> fields!"; 

} 
else if ($direction == "oneway") 
{ 
    if($liner && $direction && $from && $f_date && $f_time && $destination && $total_price ) 
      { 
       $connect = mysql_connect("localhost" ,"cc11205","11205"); 
       mysql_select_db("cc11205"); 

       $queryreg = mysql_query(" 

       INSERT INTO bookticket VALUES ('','$liner','$direction','$from','$f_date','$f_time','$destination','','','$total_price',''); 


       "); 
       die("Your booking details has been succesfully inserted into the database.Please <a href='customer.php'>Click here</a> to go to the main page to view your booking"); 

      } 
     else 
      echo "Please fill in <b>all</b> fields!"; 

} 
} 
?> 
+0

欢迎来到StackOverlow!这是一个非常广泛的问题。你需要把它缩小到你不明白或者有问题的地方。如果你只是希望有人为你写代码StackOverflow是错误的地方问,你可以尝试自由职业者的网站。 – peterm 2013-05-11 02:45:45

+0

Mr.peterm我很抱歉。我重新修正了这个问题。如果再次出现任何错误,请告诉我。谢谢。 – 2013-05-11 03:16:00

回答

-1

我假设你想知道如何获得第一个查询到customer表的生成的密钥。你可以使用mysql_insert_id。下面是一个例子。

$rs = mysql_query($insert_sql_for_customer); 
$customer_id = mysql_insert_id(); 

$booking_sql = "INSERT INTO bookticket (customer_id, field1, field2, ...) VALUES ($customer_id, $val1, $val2, ...); 
mysql_query($booking_sql); 
+0

因此,代码将与主键(客户表中的customer_id)匹配到外键(bookticket表中的customer_id)。 – 2013-05-11 03:08:37

+0

是的,试试吧,看看 – DevZer0 2013-05-11 05:31:14

+0

谢谢Mr @ DevZer0我试过这个,它工作。 '$ query =“SELECT * FROM customer WHERE username ='”。$ _ SESSION ['username']。“'”; $ result = mysql_query($ query); ($ row = mysql_fetch_array($ result)){ $ customer_id = $ row ['customer_id'];} $ username = $ _ SESSION ['username “]; $ booking_sql =“INSERT INTO bookticket VALUES('','$ customer_id','$ liner','$ direction','$ from','$ f_date','$ f_time','$ destination','$ t_date”, '$ t_time', '$ TOTAL_PRICE')“; mysql_query($ booking_sql); \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t } 别的 {回声 “无效的用户”;}' – 2013-05-13 09:25:32

相关问题