DB Error: constraint violation

From: Date: Wed, 18 Dec 2002 04:32:30 +0000
Subject: DB Error: constraint violation
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-3028@lists.php.net to get a copy of this message
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I'm gettiing a ' DB Error: constraint violation' when I run the following function against the following table. Note the error only happens on an insert. Anyone have any ideas, or know what this means? Thanks in advance. Shane Antyr /** * insertOrUpdate() V1 * * @param dbh database handler * @param table table to query * @param values array: values to enter into the database * @param id db record key * */ function insertOrUpdate($dbh, $table, $values, $id) { /* run a query and get number of columns for $table */ // define query $result = $dbh->query("SHOW COLUMNS FROM ".$table.""); // get results // build an array of column names while($columns = $result->fetchRow()){ $colName[] =$columns[0]; } // ERROR if(PEAR::isError($result)){ echo"get column count for ".$table." failed:\n\n"; dbError($result); } // end ERROR // get how many columns there are // used to build subsequent queries $count = count($colName); /* check to see if the record exists */ // define query $stmt = "SELECT id FROM ".$table." WHERE id = '".$id."'"; // send query $result1 = $dbh->query($stmt); // get results $row = $result1->fetchRow(DB_FETCHMODE_ORDERED); // ERROR if(PEAR::isError($result)){ echo"get id from ".$table." failed:\n\n"; dbError($result); } // end ERROR /* the record exits. update it */ if($row[0]){ // create columns for query, required by prepare() for($i=1; $i < $count -1; $i++){ $columns .= $colName[$i]."=?, "; } // last column $columns .= $colName[$i]."=?"; $sth = $dbh->prepare("UPDATE ".$table." SET ".$columns.
				    		 " WHERE  id = '".$id."'");
$result3 = $dbh->execute($sth, $values); // ERROR if(PEAR::isError($result3)){ echo $table." update failed:\n\n"; dbError($result3); } // end ERROR } // end if /* record doesn't exist, insert the data */ else{ // create columns for query, required by prepare() for($i=0; $i < $count -2; $i++){ $columns .= "?, "; } $columns .= "?"; $sth = $dbh->prepare("INSERT INTO ".$table." VALUES('$id',".$columns.")"); $result2 = $dbh->execute($sth, $values); // ERROR if(PEAR::isError($result2)){ echo $table." insert failed:\n\n"; dbError($result2); } // end ERROR } // end else } // end function 'insertOrUpdate()' # # Table structure for table guide_dining_meals # CREATE TABLE guide_dining_meals ( id int(11) NOT NULL default '0', breakfast tinyint(4) NOT NULL default '0', lunch tinyint(4) NOT NULL default '0', dinner tinyint(4) NOT NULL default '0', PRIMARY KEY (id) ) TYPE=MyISAM; -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (Darwin) iD8DBQE9//pmmU+jltT1nOoRAm9oAKCUFJSVjKPHr0hfEeO8oWT+4StHegCfd/tA zQTnj5RQuCQAFnn2xioQjkQ= =ODns -----END PGP SIGNATURE-----

« previous php.pear.general (#3028) next »