我从textarea插入mysql.i时出现问题,其中包含一个包含多个textarea的表单。form.php
<form name="form1" method="post" action="insert.php">
<thead>
<tr>
<th> </th>
<th>Center Name</th>
<th>User Name</th>
<th>Password</th>
<th>IP Details</th>
</tr>
<tr>
<td valign="top">1</td>
<td valign="top">ABCE</td>
<td valign="top">abce</td>
<td valign="top">abce123</td>
<td valign="top">
<textarea rows="4" cols="40" name="ip_details[]">59.162.181.45,27.251.106.75,59.162.182.155</textarea><br/>
</td>
</tr>
<tr class="alt-row">
<td valign="top">2</td>
<td valign="top">XYZ</td>
<td valign="top">xyz</td>
<td valign="top">1234</td>
<td valign="top">
<textarea rows="4" cols="40" name="ip_details[]">54.23.87.32,198.12.65.64,34.56.43.12</textarea><br/>
</td>
</tr>
<tr>
<td colspan="5" align="center">
<input type="submit" name="submit" id="submit" value="Update" /></td>
</tr>
</form>
我想将这些数组值插入到数据库中,该数据库将输入ip_details [0]到ABCE行,ip_details [1]到数据库中的XYZ行。 当我做的时候
$a=$_POST['ip_details'];
$b= $_POST['ip_details'][0];
echo "$b";
$c= $_POST['ip_details'][1];
echo "$c";
它显示了ABCE和XYZ的ip_details值。
我想在database.id_center(主键)的ipdetails列中添加ip的这些数组值my table structure.show create table table_name
CREATE TABLE `center_listt` (
id_center int(10) NOT NULL AUTO_INCREMENT,
center_name varchar(10) NOT NULL,
user_name varchar(10) NOT NULL,
password varchar(10) NOT NULL,
ipdetails varchar(30) NOT NULL,
PRIMARY KEY (`id_center`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1
id_center center_name user_name password ipdetails
1 ABCE abce 1234 59.162.181.45,27.251.106.75,59.162.182.155
2 XYZ xyz 1234 54.23.87.32,198.12.65.64,34.56.43.12
我无法找到一种方法来做到这一点。我想做的就是在我的浏览器中回显数组。需要帮助在数据库中插入相同的
答案 0 :(得分:0)
<form name="form1" method="post" action="">
<thead>
<tr>
<th> </th>
<th>Center Name</th>
<th>User Name</th>
<th>Password</th>
<th>IP Details</th>
</tr>
<tr>
<td valign="top">1</td>
<td valign="top">ABCE</td>
<td valign="top">abce</td>
<td valign="top">abce123</td>
<td valign="top">
<input type="hidden" name="row[]" value="abc" />
<textarea rows="4" cols="40" name="ip_details[]">59.162.181.45,27.251.106.75,59.162.182.155</textarea><br/>
</td>
</tr>
<tr class="alt-row">
<td valign="top">2</td>
<td valign="top">XYZ</td>
<td valign="top">xyz</td>
<td valign="top">1234</td>
<td valign="top">
<input type="hidden" name="row[]" value="XYZ" />
<textarea rows="4" cols="40" name="ip_details[]">54.23.87.32,198.12.65.64,34.56.43.12</textarea><br/>
</td>
</tr>`enter code here`
<tr>
<td colspan="5" align="center">
<input type="submit" name="submit" id="submit" value="Update" /></td>
</tr>
</form>
<?php
if($_POST){
$count = count($_POST['row']);
for($i = 0;$i<$count;$i++){
$sql = "INSERT INTO TABLE SET field = '{$_POST['row'][$i]}',ip = '{$_POST['ip_details'][$i]}' ";
echo "<br>".$sql;
}
}
答案 1 :(得分:0)
<?php
if(isset($_POST['submit'])) {
$ip_details = $_POST['ip_details'];
$values = array(
array('ipdetails' => '"'.$ip_details[0].'"'),
array('ipdetails' => '"'.$ip_details[1].'"'),
);
$columns = implode(', ', array_keys($values[0]));
foreach($values as $value) {
$value = implode(',', $value);
$statement = "INSERT INTO `center_listt` (ipdetails) VALUES ($value) ";
//echo $statement . "<br/>";
$res=mysql_query($statement);
if(!$res) {
die('could not connect'.mysql_error());
}
echo "gg";
}
}
?>
列计数dosent匹配第1行的值计数,因为它期望所有数组键都插入mysql insert语句的列表中。
它会运行,但是你所期望的输出如数据库所示,其中已经有一个表有字段值,只是你想在ipdetails列中输入这些ip,它将与id_center匹配。 / p>
它只会插入一个不是你期望发生的新行