gpt4 book ai didi

php - mysql php脚本只能在第一次使用

转载 作者:行者123 更新时间:2023-11-30 22:36:30 24 4
gpt4 key购买 nike

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

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

这里是最后的错误检查和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);

在建立 mysqli_connection 之前。

关于php - mysql php脚本只能在第一次使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32411904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com