Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql php脚本只在第一次使用时有效_Php_Mysql_Mysqli - Fatal编程技术网

mysql php脚本只在第一次使用时有效

mysql php脚本只在第一次使用时有效,php,mysql,mysqli,Php,Mysql,Mysqli,我有一个脚本可以将公司和类别添加到数据库中,但是它只在第一次工作。当我尝试添加第二个公司时,在通过所有其他错误检查后,它在最终错误检查中失败。类别已成功添加到“类别”表中,但公司及其详细信息未添加到“公司”表中 这可能是代码错误还是数据库构造中的错误 下面是最后的错误检查和mysql查询: if (empty($errors)) { // If everything's OK. // Add the company to the database: $q = 'INSERT I

我有一个脚本可以将公司和类别添加到数据库中,但是它只在第一次工作。当我尝试添加第二个公司时,在通过所有其他错误检查后,它在最终错误检查中失败。类别已成功添加到“类别”表中,但公司及其详细信息未添加到“公司”表中

这可能是代码错误还是数据库构造中的错误

下面是最后的错误检查和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.
if(空($errors)){//如果一切正常。
//将公司添加到数据库:
$q='插入公司(类别id、公司名称、电话、电子邮件、网站、地址1、地址2、地址3、邮政编码、描述、图像名称)值(?,,,,,,,,,,,,,);
$stmt=mysqli_prepare($dbc,$q);
mysqli_stmt_bind_param($stmt,$issss',$catid,$cn,$p,$e,$w,$a1,$a2,$a3,$pc,$d,$i);
mysqli_stmt_execute($stmt);
//检查结果。。。
如果(mysqli\u stmt\u受影响的行($stmt)==1){
//打印消息:
echo'该公司已被添加。

; //重命名图像: $id=mysqli\u stmt\u insert\u id($stmt);//获取公司id。 重命名($temp,“../../../uploads/$id”); //清除$\u帖子: $\u POST=array(); }否则{//错误! echo“

由于系统错误,无法处理您提交的内容。

”; } mysqli_stmt_close($stmt); }//如果发生错误,则结束$errors。
以下是完整的代码:

<?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...
?>

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

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

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

在建立mysqli_连接之前。

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

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

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

在建立mysqli_连接之前。

MySQL/PHP会出现什么错误?请参阅以了解如何添加错误或
die(mysqli_错误($dbc))在mysqli\u stmt\u执行($stmt)
之后,我得到了以下错误:键“PRIMARY”的重复条目“0”,这说明自动增量无法正常工作?主键是公司表中的公司id,我刚刚添加了
mysqli_报告(mysqli_报告错误| mysqli_报告严格)到mysqli_connect上面的代码顶部,现在错误显示为“致命错误:未捕获异常'mysqli_sql_exception',在add_company.php:132堆栈跟踪:#0 add_company.php(132):mysqli_stmt_execute(Object(mysqli_stmt))#1{main}抛出在add_company.php的第132行”MySQL/PHP给出了什么错误?请参阅以了解如何添加错误或
die(mysqli_error($dbc))在mysqli\u stmt\u执行($stmt)
之后,我得到了以下错误:键“PRIMARY”的重复条目“0”,这说明自动增量无法正常工作?主键是公司表中的公司id,我刚刚添加了
mysqli_报告(mysqli_报告错误| mysqli_报告严格)到mysqli_connect上面的代码顶部,现在错误显示为“致命错误:未捕获异常'mysqli_sql_exception',在add_company.php:132堆栈跟踪:#0 add_company.php(132):mysqli_stmt_execute(Object(mysqli_stmt))#1{main}抛出在add_company.php的第132行”