示例#1
0
 */
if (@$_POST['newbutton'] == "Return to category page" or @$_POST['newbutton'] == "Cancel") {
    //➝7 Starts an if statement that checks whether the user clicked the submit button labeled Cancel or Return to category page. If so, it returns to the first page.
    header("Location: choosepetcat.php");
}
include "misc.inc";
include "functions.inc";
//➝13 Includes the file that defines the function AddNewType(), which is used later in this program. The function is shown in Listing 11-8.
$cxn = mysqli_connect($host, $user, $passwd, $dbname) or die("Couldn't connect to server");
//➝14 Creates a connection to the database.
/* If new was selected for pet category, check if
  category name and description were filled in. */
if (trim($_POST['category']) == "new") {
    //➝18 Starts an if block that executes only if the user selected the radio button for New Category in the form from the previous program. This block checks whether the new category name and description are filled in. If the user forgot to type them, he or she is asked for the pet type name and description again. After the name and description are filled, the program calls a function that adds the new category to the PetType table. The following lines describe this if block in more detail:
    $_POST['category'] = trim($_POST['newCat']);
    //➝20 Sets the category name, which currently equals “new”, to the new category name.
    if (empty($_POST['newCat']) or empty($_POST['newDesc'])) {
        //➝21 Starts an if block that executes only if the category name //or the category description is blank. Because this if block /is inside the if block for a new category, this block executes //only if the user selected New Category for pet type //but didn’t fill in the new category name and description.
        include "newcat_form.inc";
        //➝24 Creates a form that asks for the category name and description. The HTML for the form is included from a file — NewCat_form.inc, which is shown in Listing 11-6. This executes only when the if statement on line 21 is true — that is, if the category is new and the category name and/or description is blank.
        exit;
        //➝25 Stops the program after displaying the form on line 24. The program can’t proceed until the category name and description are typed. This block repeats until a category name and description are filled.
    } else {
        //➝27 Starts an else block that executes only if both the category name and description are filled in. Because this block is inside the if block for the new radio button, this block executes when the user selected new and filled in the new category name and description.
        addNewType($_POST['newCat'], $_POST['newDesc'], $cxn);
        //29 Calls a function that adds the new category to the PetType table.
    }
}
// ➝31 This line ends the if block. If the user selected one of the existing pet types, the statements between line 17 and this line did not execute.
include "newname_form.inc";
//➝32 Creates the form where the user can enter information about the new pet. A file is included that contains the code for the form, shown in Listing 11-7.
<?php

require_once 'sql_query.php';
$name = $_POST["name"];
$parent = $_POST["parent"];
$txt = $_POST["txt"];
$pic = $_POST["pic"];
$id = -1;
$result = getTypesByName($name);
if (!is_null($result->fetch_assoc())) {
    echo "Уже существует!";
    exit;
}
$result = getTypesNewID();
while ($row = $result->fetch_assoc()) {
    $id = $row["nextID"];
}
if ($name === $parent) {
    $parent_id = $id;
} else {
    while ($row = $result->fetch_assoc()) {
        $parent_id = $row["id"];
    }
}
echo $id . " " . $parent_id . " " . $name . " " . $txt . " " . $pic;
$result = addNewType($id, $parent_id, $name, $txt, $pic);