Miles is correct, this the standard way (without knowing your application
and if I could figure out another trick).

But if you absolutely don't want to use two SQL blocks you can set your SQL
block to something like this:

$sql = "

DELETE FROM $table_name
  WHERE our_serv = \"$our_serv\"

INSERT INTO $table_name
 (our_serv)
 VALUES
 (\"$our_serv\")";

This works because if the value in question is not there the DELETE
statement is perfectly valid - but it won't delete anything.

What you're looking for is an "INSERT OR UPDATE" statement and there is no
such thing in SQL (no doubt there ought to be one - but there isn't).  As
for using a DELETE and then an INSERT statement - that's just about as much
work for the DBMS as an UPDATE.

Good Luck,

Frank

On 2/4/02 6:30 PM, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:

> From: Miles Thompson <[EMAIL PROTECTED]>
> Date: Mon, 04 Feb 2002 14:20:29 -0400
> To: "jas" <[EMAIL PROTECTED]>,[EMAIL PROTECTED]
> Subject: Re: [PHP-DB] Combined sql statement
> 
> This just showed up, where was it stuck?
> 
> Essentially you do a SELECT into your target table, if the target key
> exists, which you determine from row count, then you do an UPDATE,
> otherwise an INSERT.
> 
> I don't know what else you have wrapped around this, in terms of control
> structure, so here's the guts:
> // determine whether or not a record exists
> // you will have to tell us what the WHERE condition is
> $strCondition = " db_field_name = 'some_value' ";
> $sql = " select * from $table_name where $strCondition;
> $result = mysql_query( $sql );
> if( mysql_num_rows( $result ) > 0 )
> {
>   // a record was returned, thus an UPDATE, using the same where condition
>   $sql = "UPDATE $table_name SET our_serv='$our_serv' where $str_condition";
> }
> else
> {
>   // no record was returned, so an INSERT
>   $sql = "INSERT INTO $table_name SET our_serv='$our_serv'";
> }
> $result = mysql_query( $sql );
> //and here you can test for the affected rows to determine success or failure.
> 
> This is sort of a hybrid, in that the table name is stored in a variable,
> whereas the field name isn't. One would assume that there is some variation
> of field names among table so this will need some cleaning up.
> There should also be some testing as to whether or not the $result is valid.
> 
> 
> If this has already been answered, well I guess I shouldn't reply to stale
> posts. Shame on me!
> 
> Regards - Miles
> 
> At 10:07 PM 1/29/2002 -0700, jas wrote:
>> I would like to know how to have a php script loop through two different sql
>> statements and use one according to a yes or no answer.  I am still kinda
>> new to php and I already have my sql statements, but I dont know how to use
>> php to tell it to use one of the other.  My sql statements are as follows...
>> 
>> $sql = "INSERT INTO $table_name
>> (our_serv)
>> VALUES
>> (\"$our_serv\")
>> ";
>> 
>> $sql = "UPDATE $table_name SET our_serv=\"$our_serv\"";
>> 
>>  How can I get it to pick one of these... for instance if there is no entry
>> in the unique table to enter one using the first sql statement, or if there
>> is already an entry to simply update and overwrite the current table entry.
>> Any help would be appriciated and if you could please document it so I
>> understand and dont have to ask again.  Thanks,
>> jas
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> WOW, your neat....
>> 
>> 
>> 
>> 
>> --
>> PHP Database Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
> 


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to