douyan8413 2011-07-27 21:02
浏览 60
已采纳

PHP,SELECT在插入相同脚本后不返回值

Here is an easy one, but can't figure out why is not working. Need another set of eyes. 3 queries. 1 insert, 1 select, then 1 insert again. My select query is supposed to grab a value from the data recently inserted, and then store that value in a variable, then run the second insert for another table. Well, everything works, except the select. I even tryied to add a mysql_query("COMMIT",$conn) and still nothing. Any help is very appreciated. Thanks!!!

$customerQ = "INSERT INTO
customerData(fname, mname, lname, email, homePhone, cellphone, address, 
city, county, state, zip, gascompany, eleccompany, addedBy)
VALUES('$fname', '$mname', '$lname', '$emailCustomer', '$hphone', '$cellphone',
  '$address', '$city', '$county', '$state', '$zip', '$gas', '$electric', 
  '$userName'
) ";             
//General WO# calculation
$calcWO = "SELECT 
              auditRequest.workOrderNum
            FROM
              auditRequest
            ORDER BY
               auditRequest.workOrderNum DESC
            LIMIT 1
        ";  

//General Customer number calculation. For the Work Order Insert
$calc = "SELECT
              customerData.custnumber
           FROM
              customerData
           WHERE
              fname = '$fname' and
              mname = '$mname' and
              lname = '$lname'
           ORDER BY
           customerData.custnumber DESC
           LIMIT 1
     "; 

    //Customer Information Insert (from Insert Query)
    $resultCustQ = mysql_query($customerQ, $connection);
    mysql_query("COMMIT", $connection); //make sure to commit to be able to query new records
    // Test the query
    if(!$resultCustQ) die ("error 1". mysql_error());

    //Customer Information Insert (from Insert Query)
    $resultCustQ = mysql_query($customerQ, $connection);
    mysql_query("COMMIT", $connection); //make sure to commit to be able to query new records
    // Test the query
    if(!$resultCustQ) die ("error 1". mysql_error());

    //Calculate new customer number
    $calc_result = mysql_query($calc, $connection);
    // Test the query
    if(!$calc_result) die ("error 1". mysql_error());
    $row=mysql_fetch_array($calc_result);
    $custnum = $row['custnumber']; // This is the new Customer.

    //Work Order Information (run SQL Insert)
    $resultWOQ = mysql_query($workOrderQ, $connection);
// Test the query
    if(!$resultWOQ) die ("error 1". mysql_error());
  • 写回答

1条回答 默认 最新

  • douji0108 2011-07-27 21:08
    关注

    I would avoid the crutch of INSERT followed by SELECT to find the inserted record altogether and use MySQL's ability to return the ID of the most recently inserted row (provided your table as an AUTO INCREMENT primary key) via PHP's mysql_insert_id, also available through PDO via lastInsertId.

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

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)