Re: multiple fields all unique? [almost solved]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: "Jas" <jason.gerfen@xxxxxxxxxxxx>

> If I do this statement:
> mysql_query("UPDATE hosts SET hostname=\"$_POST[hostname]\",
> mac=\"$_POST[mac]\", ip=\"$_POST[ip]\", vlan=\"$_POST[vlan]\" WHERE
> id=\"$_SESSION[id]\"",$db)or die(mysql_error() . mysql_errno());
>
> I get this error:
> Duplicate entry '128.110.22.139' for key 41062
>
> I have tried using these types of checks with no success:
> $update = mysql_query("UPDATE hosts SET hostname=\"$_POST[hostname]\",
> mac=\"$_POST[mac]\", ip=\"$_POST[ip]\", vlan=\"$_POST[vlan]\" WHERE
> id=\"$_SESSION[id]\"",$db)or die(mysql_error() . mysql_errno());
> $rows = mysql_affected_rows();
>    while($match = mysql_fetch_assoc($update)) {
>      echo $match["hostname"];
>      echo $match["mac"];
>      echo $match["ip"]; }
> if($rows == 0) {
>    echo "update worked";
> } else {
>    echo "update didn't work"; }
>
> And...
> while($match = mysql_fetch_object($update)) {
>
> And..
> while($match = mysql_fetch_array($update)) {
>
> So far everything I have tried will not allow me to find the exact field
> and contents of a record that matches an existing record in the
> database.  See below for details on database structure etc.
> Any help is appreciated,

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.

The format of the error message is always the same, "Duplicate entry 'XXXX'
for key ZZZZ, where XXXX is the value that was duplicated and ZZZZ is the
key number. It looks like key 41062 is the IP column. So you can look for
the 41062 and display a message about duplicate IP.

If it were me, I'd just match the value between single quotes, and echo a
message such as "Duplicate value XXXX". The difference between a mac, ip,
and hostname are pretty obvious, so you don't really _have_ to tell the user
which one it is.

---John Holmes...

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


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux