Exceptions in PHP
| From: | John Meyer | Date: | Sun, 14 May 2006 19:10:41 +0000 |
| Subject: | Exceptions in PHP | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-236154@lists.php.net to get a copy of this message | ||
I have the following script:
try {
$conn = mysql_connect("localhost","webuser","testme") or die("Could not connect");
mysql_select_db("bookcollection",$conn) or die("Could not select database");
//first, check to see if there is an author
if ($_POST["neworoldauthor"] == "new") {
$sql = "SELECT * FROM AUTHORS WHERE FirstName=\"" . $_POST["authorfname"] . "\" AND LastName=\"" . $_POST["authorlname"] . "\"";
$retval = mysql_query($sql);
if (mysql_num_rows($retval) == 0) {
$sql = "INSERT INTO AUTHORS(FirstName, LastName) VALUES(\"" . $_POST["authorfname"] . "\",\"" . $_POST["authorlname"] . "\")";
mysql_query($sql);
$authorID = mysql_insert_id();
} else {
$row = mysql_fetch_array($retval,MYSQL_ASSOC);
$authorID = $row["AuthorID"];
}
} else { // it's an old author
$authorID = $_POST["authors"];
}
//now, check titles
$sql = "SELECT * FROM TITLES WHERE TITLE_TITLE=\"" . $_POST["title"] . "\"";
$retval = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($retval) == 0) {
$sql = "INSERT INTO TITLES(TITLE_TITLE) VALUES(\"" . $_POST["title"] . "\")";
mysql_query($sql);
$titleID = mysql_insert_id();
} else {
$row = mysql_fetch_array($retval,MYSQL_ASSOC);
$titleID = $row["TITLE_ID"];
}
//now, insert the book
$sql = "INSERT INTO BOOKS(CopyrightYear,CoverType,DatePurchased,EditionNumber,ISBNNumber,Notes,Pages,Publisher,LOCNumber) VALUES(\"" . $_POST["copyrightyear"] . "\",\"" . $_POST["covertype"] . "\",\"" . $_POST["datepurchased"] . "\"," . $_POST["editionnumber"] . ",\"" . $_POST["isbn"] . "\",\"" . addslashes($_POST["notes"]) . "\"," . (isset($_POST["numberofpages"])?$_POST["numberofpages"]:0) . ",\"" . $_POST["publisher"] . "\",\"" . $_POST["locnumber"] . "\")";
mysql_query($sql);
$bookID = mysql_insert_id();
$sql = "INSERT INTO BOOK_TITLE(BOOK_ID,TITLE_ID) VALUES($bookID,$titleID)";
mysql_query($sql);
$sql = "INSERT INTO AUTHOR_TITLE(AUTHOR_ID, TITLE_ID) VALUES($authorID,$titleID)";
mysql_query($sql);
$sql = "INSERT INTO TITLE_GENRE(TITLE_ID,GENRE_ID) VALUES($titleID," . $_POST["genre"] . ")";
mysql_query($sql);
} catch(Exception $e) {
echo $e->GetMessage();
mysql_query("ROLLBACK");
}
?>
I'm trying to get to the point where if the query doesn't execute, it pops out a message and rollsback any possible results. It doesn't. What's wrong on this script?