这让我发疯了。我有一个表单,始终是用户将产品上传到数据库。表单验证和插入数据库位于同一页面上。
我的数据库类别,图片和产品中有3个表格。因此,当用户单击插入按钮时,我必须将图像数据添加到图片表,其余数据将转到产品表。图片表有一个名为key_id的自动增量键,products表有一个名为product_id的自动增量键。
尝试将图像插入图片表时,出现以下错误:
致命错误:错误的SQL:INSERT INTO图片(key_id,product_id,pic_name)VALUES(,6554,6554Bosch_02.jpg)
当尝试插入产品表时,我收到此错误:
错误:您的SQL语法出错;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在条件,energy_star,颜色,保修,宽度,深度,高度,重量,cubic_feet'附近使用。在第1行
我的PHP代码如下。非常感谢任何帮助。
// define variables and initialize with empty values
$categoryErr = $titleErr = $manufacturerErr = $subCategoryErr = $conditionErr = $energyErr = $modelNumberErr = $colorErr = $priceErr = $warrantyErr = $cuErr = $widthErr = $depthErr = $heightErr = $weightErr = $descriptionErr = $featuredErr = $imgEr = "";
$category = $title = $manufacturer = $subCategory = $condition = $energy = $modelNumber = $color = $price = $warranty = $cu = $width = $depth = $height = $weight = $description = $featured = "";
$imageNumber = 0;
$flag = "False"; // default flag is set to false. Product will not get inserted to database till True
$flagReset = "False"; // default flagReset is set to false. Form values will be reset when True
// connect to database
include 'config/config.php';
include 'config/opendb.php';
// Form Validation
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$flag = "True";
if (empty($_POST["category"])){
$categoryErr = "<span class='error2'>* Select a Category</span><br />";
$flag = "False";
} else {
$category = clean_input($_POST["category"]);
}
if (empty($_POST["title"])){
$titleErr = "<span class='error2'>* Missing Title</span><br />";
$flag = "False";
} else {
$title = clean_input($_POST["title"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$title)){
$titleErr = "<span class='error2'>* Only letters and white space allowed</span><br />";
$flag = "False";
}
}
if (empty($_POST["manufacturer"])){
$manufacturerErr = "<span class='error2'>* Missing Manufacturer</span><br />";
$flag = "False";
} else {
$manufacturer = clean_input($_POST["manufacturer"]);
}
if (empty($_POST["subCategory"])){
$subCategoryErr = "<span class='error2'>*Missing Sub Category</span><br />";
$flag = "False";
} else {
$subCategory = clean_input($_POST["subCategory"]);
}
if (empty($_POST["condition"])){
$conditionErr = "<span class='error2'>*Select a Product Condition</span><br />";
$flag = "False";
} else {
$condition = clean_input($_POST["condition"]);
}
if (empty($_POST["energy"])){
$energyErr = "<span class='error2'>*Select If Product is Energy Star</span><br />";
$flag = "False";
} else {
$energy = clean_input($_POST["energy"]);
}
if (empty($_POST["modelNumber"])){
$modelNumberErr = "<span class='error2'>*Missing Model Number</span><br />";
$flag = "False";
} else {
$modelNumber = clean_input($_POST["modelNumber"]);
}
if (empty($_POST["color"])){
$colorErr = "<span class='error2'>*Select At Least One Color</span><br />";
$flag = "False";
} else {
$color = clean_input($_POST["color"]);
}
if (empty($_POST["price"])){
$priceErr = "<span class='error2'>*Missing Price</span><br />";
$flag = "False";
} else {
$price = clean_input($_POST["price"]);
}
if (empty($_POST["warranty"])){
$warrantyErr = "<span class='error2'>*Missing Warranty</span><br />";
$flag = "False";
} else {
$warranty = clean_input($_POST["warranty"]);
}
if (empty($_POST["cu"])){
$cuErr = "<span class='error2'>*Missing</span><br />";
$flag = "False";
} else {
$cu = clean_input($_POST["cu"]);
}
if (empty($_POST["width"])){
$widthErr = "<span class='error2'>*Missing Width</span><br />";
$flag = "False";
} else {
$width = clean_input($_POST["width"]);
}
if (empty($_POST["depth"])){
$depthErr = "<span class='error2'>*Missing Depth</span><br />";
$flag = "False";
} else {
$depth = clean_input($_POST["depth"]);
}
if (empty($_POST["height"])){
$heightErr = "<span class='error2'>*Missing Height</span><br />";
$flag = "False";
} else {
$height = clean_input($_POST["height"]);
}
if (empty($_POST["weight"])){
$weightErr = "<span class='error2'>*Missing Weight</span><br />";
$flag = "False";
} else {
$weight = clean_input($_POST["weight"]);
}
if (empty($_POST["description"])){
$descriptionErr = "<span class='error2'>*Missing Description</span><br />";
$flag = "False";
} else {
$description = clean_input($_POST["description"]);
}
if (empty($_POST["featured"])){
$featuredErr = "<span class='error2'>*Select if Product is Featured</span><br />";
$flag = "False";
} else {
$featured = clean_input($_POST["featured"]);
}
if($flag == "True"){
$imgName = 1;
// checking then selecting the latest product id
$checkID='SELECT product_id FROM products ORDER BY product_id DESC LIMIT 1';
$rs=$conn->query($checkID);
$rs->data_seek(0);
while($row = $rs->fetch_assoc()){
$product_id = $row['product_id'] + 1; // adding +1 to the latest product id
}
// // // image upload section
while(list($key,$value) = each($_FILES['images']['name'])){
$imageNumber++;
// check to see if value is empty.
if(!empty($value)){
$Picname = $imgName++;
$orig = "$manufacturer"."_"."0".$imageNumber.".jpg"; // this will check if any blank field is entered
$filename = $product_id."$manufacturer"."_"."0".$imageNumber.".jpg"; // filename stores the value
$add = "../images/products/$filename"; // upload directory path is set
copy($_FILES['images']['tmp_name'][$key], $add); // upload the file to the server
chmod("$add",0777); // set permission to the file
// inserting information into database
$imgSql = "INSERT INTO pictures (key_id, product_id, pic_name) VALUES ( NULL, $product_id, $filename)";
if (!mysqli_query($conn,$imgSql)){
die('Error: ' . mysqli_error($conn));
} else {
echo "1 record added";
}
}
}
// // // inserting new product into database.
$insert="INSERT INTO products (product_id, cat_id, title, manufacturer, model_number, sub_cat, condition, energy_star, color, warranty, width, depth, height,weight, cubic_feet, price, product_desc, _sold, featured, _thumbnail) VALUES ('$product_id','$category', '$title', '$manufacturer', '$modelNumber', '$subCategory', '$condition', '$energy', '$color', '$warranty', '$width', '$depth', '$height', '$weight', '$cu', '$price', '$description', 'STOCKED', '$featured', '$orig')";
if($conn->query($insert) === false){
trigger_error('Wrong SQL: ' . $imgSql . ' Error: ' . $conn->error, E_USER_ERROR);
} else {
$last_inserted_id = $conn->insert_id;
$affected_rows = $conn->affected_rows;
echo "Record successfully inserted.";
$flagReset = "True";
}
if ($flagReset == "True"){
// Reset form values
$categoryErr = $titleErr = $manufacturerErr = $subCategoryErr = $conditionErr = $energyErr = $modelNumberErr = $colorErr = $priceErr = $warrantyErr = $cuErr = $widthErr = $depthErr = $heightErr = $weightErr = $descriptionErr = $featuredErr = $imgEr = "";
$category = $title = $manufacturer = $subCategory = $condition = $energy = $modelNumber = $color = $price = $warranty = $cu = $width = $depth = $height = $weight = $description = $featured = "";
}
}
}
// function to clean users input.
function clean_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
echo "$flag <br />";
echo "$category <br />";
echo "$featured <br />";
答案 0 :(得分:4)
致命错误:错误的SQL:INSERT INTO图片(key_id,product_id,pic_name)VALUES(,6554,6554Bosch_02.jpg)
您在查询中明显遗漏了一个值(对于key_id)。您还错过了pic_name 6554Bosch_02.jpg
的字符串值周围的引号。
错误:您的SQL语法出错;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在条件,energy_star,颜色,保修,宽度,深度,高度,重量,cubic_feet&#39;附近使用。在第1行
condition
是reserved keyword。如果您要使用它,则必须将其包裹在刻度线中。
答案 1 :(得分:1)
Wrong SQL: INSERT INTO pictures (key_id, product_id, pic_name) VALUES ( , 6554, 6554Bosch_02.jpg)
就像你说的那样key_id是自动递增所以你不需要这里的密钥如果你没有变量那么就像下面这里有额外的逗号
INSERT INTO pictures ( product_id, pic_name) VALUES (6554, 6554Bosch_02.jpg)
或
INSERT INTO pictures (key_id, product_id, pic_name) VALUES ( 'ID', '6554', 6554Bosch_02.jpg)
答案 2 :(得分:0)
致命错误:错误的SQL:INSERT INTO图片(key_id,product_id,pic_name)VALUES(,6554,6554Bosch_02.jpg)
缺少Key_id值,它为空,这就是您收到错误的原因。