尝试在数据库中插入多个输入值,但只插入一个表..请检查并提供帮助。感谢
<form action="insert.php" method="post" id="database" name="database" >
<?php
foreach($_POST['url_link'] as $value)
{
echo '<input type="text" name="url_db[]" size="150" value="'.$value.'"/><br />';
}
?>
<br><hr>Categories:<select name="cat" id="cat">
<option value="Mobile">Mobile
<option value="T-Shirt">T-Shirt
</select>
Website:<input type="text" size="50" value="" name="web" id="web"/><input type="submit" value="Save to DB>>" name="datasub"/>
</form>
Insert.php -
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$count = count($_POST['url_db']);
if (isset($_POST['url_db'])) {
for($i=0;$i<$count;$i++){
$urllink = $_POST['url_db'][$i];
$web = $_POST['web'];
$cat = $_POST['cat'];
$sql="INSERT INTO $tbl_name (web, urllink, cat) VALUES ('$web', '$urllink', '$cat')";
if(!($result = mysql_query($sql))){
"<BR>Error Adding!!<BR>".mysql_error();
exit();
}
header("location:index.php");
}
}
?>
有关信息 - 我正在尝试使用带有类别名称(下拉列表)和网站名称的输入框插入多个URL链接。 url_db [] -
<form action="insert.php" method="post" id="database" name="database">
<input type="text" name="url_db[]" size="150" value="http://www.google.dk/imghp?hl=da&tab=wi"><br>
<input type="text" name="url_db[]" size="150" value="http://maps.google.dk/maps?hl=da&tab=wl"><br>
<input type="text" name="url_db[]" size="150" value="https://play.google.com/?hl=da&tab=w8"><br>
<input type="text" name="url_db[]" size="150" value="http://www.youtube.com/?gl=DK&tab=w1"><br>
<input type="text" name="url_db[]" size="150" value="https://mail.google.com/mail/?tab=wm"><br><br>
<hr>Categories:<select name="cat" id="cat">
<option value="Mobile">Mobile
</option><option value="T-Shirt">T-Shirt
</option></select>
答案 0 :(得分:0)
您重定向IN for for循环。
应该是这样的:
if (isset($_POST['url_db'])) {
for($i=0;$i<$count;$i++){
$urllink = $_POST['url_db'][$i];
$web = $_POST['web'];
$cat = $_POST['cat'];
$sql="INSERT INTO $tbl_name (web, urllink, cat) VALUES ('$web', '$urllink', '$cat')";
if(!($result = mysql_query($sql))){
"<BR>Error Adding!!<BR>".mysql_error();
exit();
}
}
header("location:index.php");
}
编辑: 在将值插入查询之前,您应该真正转义它们。
我建议:
if (isset($_POST['url_db'])) {
for($i=0;$i<$count;$i++){
//escape with mysqli_real_escape_string or PDO::quote() (mysql_real_escape_string) is deprecated
$urllink = mysqli_real_escape_string($_POST['url_db'][$i]);
$web = mysqli_real_escape_string($_POST['web']);
$cat = mysqli_real_escape_string($_POST['cat']);
$sql="INSERT INTO $tbl_name (web, urllink, cat) VALUES ('$web', '$urllink', '$cat')";
if(!($result = mysql_query($sql))){
"<BR>Error Adding!!<BR>".mysql_error();
exit();
}
}
header("location:index.php");
}
有关详细信息,请参阅:
答案 1 :(得分:0)
您正在for循环中重定向。为最佳实践创建像这样的输入
<?php
foreach($_POST['url_link'] as $key=>$value)
{
echo '<input type="text" name="url_db[]" size="150" value="'.$value.'"/><br />';
}
?>
然后你的插入代码将是这样的。
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
if (isset($_POST['url_db'])) {
print_r($_POST['url_db']) ; // Make sure you have more than one indexes in array
foreach($_POST['url_db'] as $key=>$urllink){
$web = $_POST['web'];
$cat = $_POST['cat'];
$sql="INSERT INTO `tbl_name` (`web`, `urllink`, `cat`) VALUES ('".$web."', '".$urllink."', '".$cat."')";
mysql_query($sql) or die("<BR>Error Adding!!<BR>".mysql_error()) ;
}
header("location:index.php");
}
如果这可以解决您的问题,请告诉我