Re: multiple fields all unique? [almost solved]

From: Date: Wed, 04 Feb 2004 17:11:22 +0000
Subject: Re: multiple fields all unique? [almost solved]
References: 1 2 3 4 5 6 7 8 9 10  Groups: php.db 
Request: Send a blank email to php-db+get-32794@lists.php.net to get a copy of this message
[snip]
You're not going to be able to fetch anything from the result set because you're excuting an UPDATE query, not a SELECT. You also do not want to die() when the query fails, otherwise you won't be able to react to the error. Execute the query, then check mysql_error() for a value. If it contains a value, then the query failed more than likely because of a duplicate key. [snip]
Well looks like I need to brush up on my php - mysql functions. =) For anyone that has been following this thread here is a simple way to catch the duplicate and show the user the contents: <?php $update = mysql_query("UPDATE hosts SET hostname=\"$_POST[hostname]\", mac=\"$_POST[mac]\", ip=\"$_POST[ip]\", vlan=\"$_POST[vlan]\" WHERE id=\"$_SESSION[id]\"",$db); $rows = mysql_affected_rows(); if(!@mysql_errno($update)) { echo "No match found"; } else { echo "Match found"; $error = preg_match("/^[']$/",mysql_errno($update)); $sql = mysql_query("select * from hosts where id = $error")
    while($x = mysql_fetch_array($sql)) {
      list($id,$host,$mac,$ip,$vlan) = $x; }
} ?> Might want to check the preg_match string to look for everything between ' and ' but other than that you should be able to list the record that your update statement is having a problem with. HTH Jas

« previous php.db (#32794) next »