Mysql PHP脚本只在第一次工作


mysql php script only works first time

我有一个脚本添加公司和类别的数据库,但它只工作的第一次。当我尝试添加第二个公司时,在通过所有其他错误检查后,它在最后的错误检查中失败。类别被成功添加到categories表中,但公司及其详细信息没有被添加到companies表中。

这可能是代码错误还是我的数据库结构中的错误?

下面是最后的错误检查和mysql查询:
if (empty($errors)) { // If everything's OK.
    // Add the company to the database:
    $q = 'INSERT INTO companies (category_id, company_name, phone, email, website, address_1, address_2, address_3, postcode, description, image_name) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
    $stmt = mysqli_prepare($dbc, $q);
    mysqli_stmt_bind_param($stmt, 'issssssssss', $catid, $cn, $p, $e, $w, $a1, $a2, $a3, $pc, $d, $i);
    mysqli_stmt_execute($stmt);
    // Check the results...
    if (mysqli_stmt_affected_rows($stmt) == 1) {
        // Print a message:
        echo '<p>The company has been added.</p>';
        // Rename the image:
        $id = mysqli_stmt_insert_id($stmt); // Get the company ID.
        rename ($temp, "../../../uploads/$id");
        // Clear $_POST:
        $_POST = array();
    } else { // Error!
        echo '<p style="font-weight: bold; color: #C00">Your submission could not be processed due to a system error.</p>'; 
    }
    mysqli_stmt_close($stmt);
} // End of $errors IF.

完整代码如下:

<?php
require_once ('../../../mysqli_connect.php');
if (isset($_POST['submitted'])) { // Handle the form.
// Validate the incoming data...
$errors = array();
// Check for a company name:
if (!empty($_POST['company_name'])) {
    $cn = trim($_POST['company_name']);
} else {
    $errors[] = 'Please enter the company''s name!';
}
// Check for an image:
if (is_uploaded_file ($_FILES['image']['tmp_name'])) {
    // Create a temporary file name:
    $temp = '../../../uploads/' . md5($_FILES['image']['name']);
    // Move the file over:
    if (move_uploaded_file($_FILES['image']['tmp_name'], $temp)) {
        echo '<p>The file has been uploaded!</p>';
        // Set the $i variable to the image's name:
        $i = $_FILES['image']['name'];
    } else { // Couldn't move the file over.
        $errors[] = 'The file could not be moved.';
        $temp = $_FILES['image']['tmp_name'];
    }
} else { // No uploaded file.
    $errors[] = 'No file was uploaded.';
    $temp = NULL;
}
// Validate the category...
if (isset($_POST['category']) && ($_POST['category'] == 'new') ) {
    // If it's a new category, add the category to the database...
    // Check for a category name...
    if (!empty($_POST['category_name'])) {
        $catn = trim($_POST['category_name']);
        // Add the category to the database:
        $q = 'INSERT INTO categories (category_name) VALUES (?)';
        $stmt = mysqli_prepare($dbc, $q);
        mysqli_stmt_bind_param($stmt, 's', $catn);
        mysqli_stmt_execute($stmt);
        // Check the results....
        if (mysqli_stmt_affected_rows($stmt) == 1) {
            echo '<p>The category has been added.</p>';
            $catid = mysqli_stmt_insert_id($stmt); // Get the category ID.
        } else { // Error!
            $errors[] = 'The new category could not be added to the database!';
        }
        // Close this prepared statement:
        mysqli_stmt_close($stmt);
    } else { // No category name value.
        $errors[] = 'Please enter the category''s name!';
    }
} elseif ( isset($_POST['category']) && ($_POST['category'] == 'existing') && ($_POST['existing'] > 0) ) { // Existing category.
    $catid = (int) $_POST['existing'];
} else { // No category selected.
    $errors[] = 'Please enter or select the category''s name!';
}
if (empty($errors)) { // If everything's OK.
    // Add the company to the database:
    $q = 'INSERT INTO companies (category_id, company_name, image_name) VALUES (?, ?, ?)';
    $stmt = mysqli_prepare($dbc, $q);
    mysqli_stmt_bind_param($stmt, 'iss', $catid, $cn, $i);
    mysqli_stmt_execute($stmt);
    // Check the results...
    if (mysqli_stmt_affected_rows($stmt) == 1) {
        // Print a message:
        echo '<p>The company has been added.</p>';
        // Rename the image:
        $id = mysqli_stmt_insert_id($stmt); // Get the company ID.
        rename ($temp, "../../../uploads/$id");
        // Clear $_POST:
        $_POST = array();
    } else { // Error!
        echo '<p style="font-weight: bold; color: #C00">Your submission could not be processed due to a system error.</p>'; 
    }
    mysqli_stmt_close($stmt);
} // End of $errors IF.
// Delete the uploaded file if it still exists:
if ( isset($temp) && file_exists ($temp) && is_file($temp) ) {
    unlink ($temp);
}
} // End of the submission IF.
// Check for any errors and print them:
if ( !empty($errors) && is_array($errors) ) {
echo '<h1>Error!</h1>
<p style="font-weight: bold; color: #C00">The following error(s) occurred:<br />';
foreach ($errors as $msg) {
    echo " - $msg<br />'n";
}
echo 'Please reselect the company image and try again.</p>';
}
// Display the form...
?>

我需要将主键指定为自动递增,在本例中是company_id字段。

添加详细错误检查的一个简单方法是插入

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);