作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个脚本可以将公司和类别添加到数据库中,但它只能在第一次使用。当我尝试添加第二家公司时,它在通过所有其他错误检查后最终错误检查失败。类别已成功添加到类别表中,但公司及其详细信息未添加到公司表中。
这可能是代码错误还是我的数据库构建错误?
这里是最后的错误检查和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/
有一条(相对)众所周知的 Perl 公理:“只有 Perl 可以解析 Perl”。我想知道 Perl 6 是否仍然如此? 扩大讨论...考虑到 PyPy 最近的更新,我想到了这个问题。 Perl 独特
这是设置。在上一个问题中,我发现我可以通过子组件中的状态传递对象属性,然后使用 componentDidUpdate 获取该对象属性。在这种情况下,状态和属性都称为到达。 这是基本代码... expo
我运行的是 10.5.2 社区版。我已经标记了 源/主要/资源 作为源目录。我可以右键单击并“编译”某些文件,据我所知,这意味着 IDE 将文件复制到与发送类文件的“com.mydomain.pack
我是一名优秀的程序员,十分优秀!