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, homePh开发者_StackOverflowone, 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());
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
.
精彩评论