我将这个输入字段放在一个进入数据库
的表单中<div id="cities">
<input type="text" name="city[]" id="1" >
<input type="button" id="add_city()" onClick="addCity()" value="+" />
</div>
这是一个添加或删除额外字段的javascript
var i = 1;
function addCity(){
if (i <= 20){
i++;
var div = document.createElement('div');
div.style.color = "white";
div.setAttribute('class', 'myclass');
div.innerHTML = '<input type="text" name="city[]" id="'i'" ><input type="button" id="add_city()" onClick="addCity()" value="+" /><input type="button" value="-" onclick="removeCity(this)">';
document.getElementById('cities').appendChild(div);
}
}
function removeCity(div) {
document.getElementById('cities').removeChild( div.parentNode );
i--;
}
到目前为止一切似乎还可以,但是当我运行此代码时
if(is_array($city)){
while(list($key,$cities) = each($city)){
$sql4 = "INSERT INTO states (custId,State,country,city)
VALUES ('$custId','$value','$country','$cities')";
$q4 = mysqli_query($db_conx,$sql4) or die(mysqli_error($db_conx));
}
}
我只得到一个结果,数据库只存储一行custId工程状态工程国家工程exept $ cities它只获得存储一次即使我输入更多城市可以任何人帮助我
我从
复制了它if(is_array($state)){
while(list($keys,$value) = each($state)){
$sql3 = "INSERT INTO states (custId,State,country,city)
VALUES ('$custId','$value','$country','$city')";
$q3 = mysqli_query($db_conx,$sql3) or die(mysqli_error($db_conx));
}
}
上面的代码来自同一形式的texbox,它可以正常工作
<form enctype="multipart/form-data" action="sendInfo.php" method="POST">
Id: <input name="custId" id="custId">
Name: <input name="name" id="name">
Address: <input name="address" id="address">
City: <div id="cities">
<input type="text" name="city[]" id="1" >
<input type="button" id="add_city()" onClick="addCity()" value="+" />
</div>
State: <?php include ("../maps/states3.php");?>
Zip Code: <input type="text" name="zipCode">
Country: <select name="country" id="country" style="width: 150px;">
<option value="United States">United States</option>
</select>
Link: http:// <input name="url" id="url">
Category: <input name="category" id="category">
Phone: <input name="phone" id="phone">
Fax: <input name="fax" id="fax" >
e-mail: <input name="email" id="email">
Order Number: <input name="orderNumber" id="orderNumber" >
Image name: <input name="image" id="image">
Chose sites to be displayed:
<input type="radio" value="yes" name="site1"> Site1 <br />
<input type="radio" value="yes" name="site2"> Site2 <br />
<input type="radio" value="yes" name="site3"> Site3 <br />
Start time: <input type="date" name="start">
Stop time: <input type="date" name="stop">
Table: <?php include ("tables2.php");?>
Comments: <textarea name="comments" id="comments" title="comments" >
</textarea>
<input name"submit" type="submit" value="submit" />
</form>
答案 0 :(得分:1)
我认为您忘记为文本框写入值属性
<input type="text" name="city[]" id="1" > instead of this use
<input type="text" name="city[]" id="1" value="1" >
在查询之前,你可以看到使用print_r($ array)发布的所有值,并且更好地使用这样的foreach
foreach($city as $key=>$value){
echo $key."-----".$value;
}
if(count($state)){
foreach($state as $key=>$val){//$val will contain state value while $key will contain key or index of array.
$sql3 = "INSERT INTO states (custId,State,country,city)
VALUES ('$custId','$val','$country','$city')";
$q3 = mysqli_query($db_conx,$sql3) or die(mysqli_error($db_conx));
}
}