function accessFail($debug) { global $scriptSuccess, $idOut, $scriptDebug; $scriptSuccess = False; $idOut = -1; $scriptDebug = $debug; scriptOutput(); die; } // Connect to the MySQL server process. //TODO: Use a .cnf file instead of hardcoding the MySQL login. mysql_connect('ix-trusty:3022', 'xunl', 'tbc123bl') or accessFail("Could not connect: " . mysql_error()); // Select the database containing address books. mysql_select_db($dbName) or accessFail("Could not find database: " . mysql_error()); // Test that the specified address book already exists. doesBookExist($bookName) or accessFail("Could not find address book: " . $bookName); // Branch: If there is an exact match, set id and go on to return; // If there is not, insert the row and get the new id. // If there is an exact match, return the id (arbitrarily picked if multiple). // Otherwise, return NULL. function getIdOfExactMatch($fn, $ln, $city, $st, $zip, $ph, $email, $addr1, $addr2, $bookName) { global $tableName; $sql = "SELECT * FROM {$tableName}\n WHERE first_name = '{$fn}' AND\n last_name = '{$ln}' AND\n city = '{$city}' AND\n state = '{$st}' AND\n zip = '{$zip}' AND\n phone = '{$ph}' AND\n email = '{$email}' AND\n address_1 = '{$addr1}' AND\n address_2 = '{$addr2}' AND\n address_book_ID = '{$bookName}' "; $queryResult = mysql_query($sql); if (mysql_num_rows($queryResult) > 0) { $row = mysql_fetch_assoc($queryResult); $id = $row['person_ID']; return $id; } else { return NULL;
$scriptSuccess = False; $scriptDebug = $debug; scriptOutput(); mysql_close(); die; } // ============================================================================ // Connect to and query the database. // ============================================================================ // Connect to the MySQL server process. //TODO: Move login stuff to variables outside the call to mysql_connect. //TODO: Use a .cnf file instead of hardcoding the MySQL login. mysql_connect('ix-trusty:3022', 'xunl', 'tbc123bl') or accessFail("Could not connect: " . mysql_error()); // Select the database containing address books. mysql_select_db($dbName) or accessFail("Could not find database: " . mysql_error()); // Check for a duplicate before proceeding, returning with error if one is found. if (doesBookExist($bookName)) { otherFail("Duplicate attempt: Address book '{$bookName}' is already in the system."); } // Construct & execute MySQL query to add new book. $sql = "INSERT INTO {$tableName} (book_name, access_time) VALUES ('{$bookName}', NOW())"; if (!mysql_query($sql)) { otherFail("MySQL UPDATE query failed."); } // ============================================================================ // Ouptut results. // ============================================================================ // Once we get here, the update is a success. $scriptSuccess = True; scriptOutput(); mysql_close();
<?php include_once '../utils.php'; $dbName = 'panda_address_book'; mysql_connect('ix-trusty:3022', 'xunl', 'tbc123bl') or accessFail("Could not connect: " . mysql_error()); mysql_select_db($dbName) or accessFail("Could not find database: " . mysql_error()); /* $tableName = 'book_names'; $sql = "SELECT * FROM $tableName"; // Select all, sorted. $queryResult = mysql_query($sql); while($row = mysql_fetch_assoc($queryResult)) { print_r($row); echo '<br>'; } */ echo doesBookExist('my friends') ? 'my friends' : 'stop 1'; echo '<br>'; echo doesBookExist('friends contacts') ? 'friends contacts' : 'stop 2'; echo '<br>'; echo touchBook('friends contacts') ? 'friends contacts' : 'stop 3'; mysql_close();
<?php session_start(); require 'dbconnection.php'; if (!isset($_SESSION['user_id'])) { header('Location: login.php'); die; } if (isset($_POST['btnChooseBook'])) { $_SESSION['1000dangersbook_name'] = $_POST['book']; header('Location: pap.php'); die; } if (isset($_POST['btnCreateNewBook'])) { if (!doesBookExist($_SESSION['user_id'], $_POST['txtNewBookName'])) { createNewBook($_SESSION['user_id'], $_POST['txtNewBookName']); $_SESSION['1000dangersbook_name'] = $_POST['txtNewBookName']; header('Location: pap.php'); die; } else { echo "Schon vorhanden!<br><br>"; } } if (isset($_POST['btnLogout'])) { session_destroy(); header('Location: login.php'); die; } ?> <html> <head>