我一直在SQL DB中获得重复的帖子。 这是我用来创建表并将数据插入数据库的代码..
$query = "SELECT * FROM '.$cname.' WHERE name='$itemname' and price='$price'";
$result = mysqli_query($db, $query);
$count = mysqli_num_rows($result);
if($count == 0){
//insert doesn't exist, continue
//insert sizes
$list = "";
$number = count($sizes);
$num = 0;
if($usesize == 'yes'){
$num = 1;
if($number > 0){
for($i=0; $i<$number; $i++){
if(trim($_POST["size"][$i] != '')){
if(empty($list)){
$list = $_POST["size"][$i];
}else{
$list .= ",".$_POST["size"][$i];
}
}
}
echo $list;
}
$quantitylist = "";
$quantitynumber = count($quantity);
if($quantitynumber > 0){
for($a=0; $a<$quantitynumber; $a++){
if(trim($_POST["quantity"][$a] != '')){
if(empty($quantitylist)){
$quantitylist = $_POST["quantity"][$a];
}else{
$quantitylist .= ",".$_POST["quantity"][$a];
}
}
}
echo $quantitylist;
}
}else{
$num = 0;
}
$colorlist = "";
$colornumber = count($colors);
$cnum = 0;
if($usecolors == 'yes'){
$cnum = 1;
if($colornumber > 0){
for($i=0; $i<$colornumber; $i++){
if(trim($_POST["colors"][$i] != '')){
if(empty($colorlist)){
$colorlist = $_POST["colors"][$i];
}else{
$colorlist .= ":".$_POST["colors"][$i];
}
}
}
echo $colorlist;
}
}else{
$cnum = 0;
}
$query = "INSERT INTO ".$cname." (name, description, imgpath, price, sold, usesize, usecolors) VALUES ('$itemname', '$description', '$insertloc"."$name', '$price', '0', '$num', '$cnum')";
if(mysqli_query($db, $query)){
//inserted successfully
echo "Inserted successfully!";
}else{
$uploadOk = 0;
echo mysqli_error($db);
}
$conn = mysqli_connect('localhost', 'grampmkn_gramsandpops', 'Grams123', 'grampmkn_shop_quantity');
if($conn){
$query = "CREATE TABLE `".$itemname."` (
id int NOT NULL AUTO_INCREMENT,
size varchar(500),
color varchar(500),
quantity int(100),
PRIMARY KEY(id),
UNIQUE(size)
);";
$array = explode(',', $list);
if(mysqli_query($conn, $query)){
mysqli_select_db($conn, $name);
$array = explode(',', $list);
foreach($array as $value){
$arrays = explode(':', $colorlist);
foreach($arrays as $values){
$arrays1 = explode(',', $quantitylist);
foreach($arrays1 as $values1){
$query = "INSERT INTO `".$itemname."` (size, color, quantity) VALUES ('$value', '$values', '$values1')";
if(mysqli_query($conn, $query)){
echo "Successfully inserted data into grampmkn_shop_quantity!";
}else{
echo mysqli_error($conn);
}
}
}
}
}else{
echo "Could not create table";
}
}else{
echo "Could not connect to shop quantity database!";
echo mysqli_errno($conn);
}
}else{
//insert already exists
echo "Insert already exists!";
$uploadOk = 0;
}
来自DB的
如果我删除嵌套循环,那么如何将数组中的所有数据插入到sql数据库中。 我只创建一次表,我只是插入多次以将所有数据都放入db。
感谢任何帮助。