0

试图在数据库中插入多个输入值但只插入一个表..请检查并帮助。谢谢

<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>

插入.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&amp;tab=wi"><br>
<input type="text" name="url_db[]" size="150" value="http://maps.google.dk/maps?hl=da&amp;tab=wl"><br>
<input type="text" name="url_db[]" size="150" value="https://play.google.com/?hl=da&amp;tab=w8"><br>
<input type="text" name="url_db[]" size="150" value="http://www.youtube.com/?gl=DK&amp;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>
4

2 回答 2

0

你在你的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");
    }

有关更多信息,请参阅:

mysqli_real_escape_string()

PDO::quote()

mysql_real_escape_string() [已弃用]

于 2013-07-10T14:06:25.103 回答
0

您正在 forloop 中重定向。最佳实践 创建这样的输入

<?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");
 }

让我知道这是否能解决您的问题

于 2013-07-10T14:11:14.057 回答