dskld5423 2013-07-24 01:22
浏览 8
已采纳

在PHP的第二个查询中使用来自一个mysqli查询的值

I'm trying to build a registration page. I'm trying to associate the kiddies who are going to camp with the parent that is paying for camp. I've established a foreign key, and these queries run fine in phpAdmin.

The first query inserting the new kid into the database works fine. -the second query asking for the kidID that was just added also returns the correct value.

HOWEVER my third query, which requires the result of the second query does not work. I'm pretty new to this world and would greatly appreciate any help that could be offered.

require_once 'login.php';
//sending data to server. 
$MySQL = new mysqli($db_hostName, $db_username, $db_password, $db_databaseName);



$MySQL->query("INSERT INTO `Kid`(`First_Name`, `Last_Name`, `Parent1_First_Name`, `Parent1_Last_Name`, `Parent2_First_Name`, `P2_Last_Name`, `P1_Phone`, `P2_Phone`, `P1_Email`, `P2_Email`, `EmergencyContact_FName`, `EmergencyContact_LName`, `EmergencyContact_Phone`, `KidStreetAddress`, `KidZip`, `SpecialNeeds`, `DOB`, `PickupID`, `kidSex`, `SwimID`) 
        VALUES ('$kidFirstName','$kidLastName','$p1FirstName','$p1LastName','$p2FirstName','$p2LastName','$p1FinPhone','$p2FinPhone','$p1Email','$p2Email','$eFirstName','$eLastName','$eFinPhone','$kidStreetAddress',$kidZip,'$kidSpecialNeeds','$cDOB','$kidPickup','$kidSex',(SELECT `SwimID` FROM `SwimmingStrength` WHERE `SwimID` = 1))");

$kidID = $MySQL->query("SELECT `KidID` FROM `Kid` ORDER BY `KidID` DESC LIMIT 1");
$kidID->data_seek($i);
$row = $kidID->fetch_row();
$kidFinID = $row[0];

$MySQL->query("INSERT INTO `Customer`(`First_Name`, `Last_Name`, `Billing_Address`, `Billing_Zip`, `Billing_State`, `Billing_Phone`, `Billing_Email`, `KidID`) 
VALUES ('$BillFirstName','$BillLastName','$BillFullAddress',$BillZip,'$BillState','$billFinPhone','$BillEmail',(SELECT `KidID` FROM `Kid` WHERE `KidID`=$kidFinID)");

$kidID->close();
$MySQL->close();

My guess is somehow I need to make sure PHP is grabbing the result of the second query before running the third, but not sure how to set that up.

  • 写回答

1条回答 默认 最新

  • douyang2530 2013-07-24 01:26
    关注

    INSERT has two forms. One is VALUES and takes constants. The other is SELECT. The two do not mix.

    You want INSERT . . . SELECT only, used like this:

    INSERT INTO `Customer`(`First_Name`, `Last_Name`, `Billing_Address`, `Billing_Zip`, 
                           `Billing_State`, `Billing_Phone`, `Billing_Email`, `KidID`) 
        select '$BillFirstName', '$BillLastName', '$BillFullAddress', $BillZip, '$BillState',
               '$billFinPhone', '$BillEmail', `KidID`
        from Kid
        where `KidID` = $kidFinID;
    

    In other words, you can put constants on the SELECT list line. In fact, you don't really need the VALUES form of INSERT at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗