Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 循环中的数据库插入执行次数超过了它应该执行的次数_Php_Loops - Fatal编程技术网

Php 循环中的数据库插入执行次数超过了它应该执行的次数

Php 循环中的数据库插入执行次数超过了它应该执行的次数,php,loops,Php,Loops,好的,这是一些较旧的代码,但它插入的内容超出了应有的范围。在我清理它之前,我想弄明白为什么。现在$\u POST[tags]只有1个值。我把它炸开,然后循环。但是底部的insert3语句插入值为零的行 我不知道为什么 if($_POST['sec'] == "next") { $bloguser = $_POST['bloguser']; $blogpassword = $_POST['blogpassword']; $blog = $_POST['blog

好的,这是一些较旧的代码,但它插入的内容超出了应有的范围。在我清理它之前,我想弄明白为什么。现在$\u POST[tags]只有1个值。我把它炸开,然后循环。但是底部的insert3语句插入值为零的行

我不知道为什么

if($_POST['sec'] == "next") {

      $bloguser = $_POST['bloguser'];
      $blogpassword = $_POST['blogpassword'];
      $blog = $_POST['blog'];

mysql_select_db($database) or die ("Unable to select database!"); 
$insert = mysql_query("INSERT INTO blogs (id, url, user, pass, dname, islocal, cat2post) VALUES ('', '$blog', '$bloguser', '$blogpassword', '','NO','$_POST[cat2blog]')")or die( 'Error: ' . mysql_error());
$taggit1 = mysql_insert_id();

$page->content .= "<p class=\"alert\">Success - External blog Added!</p>";


$tags  = $_POST['tags'];
$pieces = explode(",", $tags);
foreach ($pieces as $l){
$l = trim($l);  
$query = "SELECT id FROM tags WHERE tag = '$l'";
$result = mysql_query($query) or die( "Error: " . mysql_error() . " in query $query");
$row = mysql_fetch_array($result);
$taggit2 = $row[0];

if ($taggit2 == '') {
$insert2 = mysql_query("INSERT INTO tags (id, tag) VALUES ('','$l')")or die( 'Error: ' . mysql_error());
$taggit2 = mysql_insert_id();
$page->content .= "<p class=\"alert\">This tag didn't exist - so I inserted a new tag</p>";
}



$insert3 = mysql_query("INSERT INTO blogstags (id, tag_id, blogstags_id) VALUES ('','$taggit2','$taggit1')")or die( 'Error: ' . mysql_error());
$page->content .= "<p>Inserted one    </p>";
}


/// some page content crap that doesn't matter to the problem goes here///

}
我想问题是我在哪里爆炸

$tags  = $_POST['tags'];
$pieces = explode(",", $tags);
foreach ($pieces as $l){
$l = trim($l); 

但是POST值只是一个单词,那么为什么它会在循环中运行两次呢?

如果
$\u POST['tags'不包含
,然后
$pieces=explode(“,”,$tags)将是仅包含一个元素的数组


我建议做
print\r($pieces)以检查是否正确。

在更新代码时,请确保转义输入数据以修复代码中的SQL注入漏洞(请参阅)这是在localhost上使用的东西-完全知道它不安全。返回:Array([0]=>word)就像只有一个单词时我应该思考的那样——但这并不能解释为什么它会循环不止一次,对吗?我想更好的问题是,为什么foreach与只有一个项的数组会循环不止一次。@Ed Charkow:我不相信错误在我们看到的代码中。当将代码缩减为
$tags=$\u POST['tags']时,您能否确认相同的行为$碎片=分解(“,”,$tags);var_dump(件);死亡(“停止”)在包含代码的文件的最顶端?@jurgen-打印出:array(1){[0]=>string(7)“fishing”}stop.///我想这可能意味着insert语句被多次触发,因为有第二个循环影响整个if。但我看不出来。。。。所以仍然困惑:)我只是在insert3查询下放了一个echo语句,它只触发了一次。另一个插件只发射一次。所以我想我可以确认我只进行了一次循环,但是insert3查询进行了两次。不知道为什么。
$tags  = $_POST['tags'];
$pieces = explode(",", $tags);
foreach ($pieces as $l){
$l = trim($l);