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