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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。