Dan Shirah wrote:
Ah, what a lovely case of the Friday morning brain farts!
I have a query that selects some data from a table based on the current ID
selected.
If the query does not return any results, I want it to continue to another
query that will insert a record into the table.
Below is what I have...but it will not insert anything if the first query
does not find a match.
<?php
$request_id = $_GET['id'];
$current_user = substr($_SERVER['AUTH_USER'], 13);
$lock_query = "SELECT id, locked_by_user FROM locked_payments WHERE id =
'$request_id'";
$lock_result = mssql_query($lock_query) or die(mssql_get_last_message());
if (empty($lock_result)) {
$set_lock = "INSERT into locked_payments (
id,
locked_by_user)
VALUES
('$request_id',
'$current_user')";
mssql_query($set_lock) or die ("Insert failed: <br
/>".mssql_get_last_message());
}
?>
Any ideas on what I'm doing wrong? My guess is that (empty($lock_result))
is probably not the correct way to check if an array is empty?
I won't say anything about what others have already warned you about, but here
is what I would do.
<?php
$request_id = intval($_GET['id']);
$current_user = substr($_SERVER['AUTH_USER'], 13);
$lock_query = " SELECT id, locked_by_user
FROM locked_payments
WHERE id = '{$request_id}'";
$lock_result = mssql_query($lock_query) or
die('MSSQL ERROR: Lock Query Failed<br />'.
mssql_get_last_message());
#
## here is the key to making this work...
# checking to make sure that the query returned 0 (zero) results
# http://us3.php.net/mssql_num_rows
if ( mssql_num_rows($lock_result) == 0 ) {
$set_lock = "INSERT INTO locked_payments
(id,locked_by_user)
VALUES
('{$request_id}','{$current_user}')";
mssql_query($set_lock) or
die ('MSSQL ERROR: Insert failed:<br />'.
mssql_get_last_message());
}
?>
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php