将多个值放入一列,但将不同的行放入 PHP

Putting several values into one column but different rows in PHP

我有三个变量:$Title、$Pubdate 和 $Link

在这些变量中,有几个这样的值:

$Title = aa, bb, cc, dd, ee, ff
$Pubdate = aa, bb, cc, dd, ee, ff
$Link = aa, bb, cc, dd, ee, ff

像这样。 然后我有一个 table 和列(Title、Pubdate 和 Link)。 我尝试过的:

$sql = "INSERT INTO ytable (Title, Pubdate, Link) VALUES ('$Title', '$Pubdate', '$Link')";
mysql_query($sql,$db_con);

虽然没有弹出错误,但它没有执行我想要它执行的操作。 它将所有值(aa、bb、cc、dd、ee、ff)放入一列(标题)和一行(ID = 1),这意味着所有值都被压缩到一个框中,而这不是我想要的是 :(
我想把它放在一列中,但放在不同的行中(1、2、3、4、5、6)。
我在网上搜索了答案,但如果他们想要一个与我想要的相反的盒子中的所有值,那么一切似乎都在回答。
也许一个接一个,因此 INSERT INTO ytable (Title)VALUES($Title);,它将所有值放在一列的不同行中。

您将不得不将您的字符串拆分成一个数组,以便您可以 'loop' 通过它们。

$Title   = "aa, bb, cc, dd, ee, ff";
$Pubdate = "aa, bb, cc, dd, ee, ff";
$Link    = "aa, bb, cc, dd, ee, ff";

$TitleArray   = explode(', ',$Title);
$PubdateArray = explode(', ',$Pubdate);
$LinkArray    = explode(', ',$Link);

//Either: loop through the array and build individual INSERTS
for($i=0;$i<count($TitleArray);$i++){

    // generate an INSERT for each row
    $sql = "INSERT INTO ytable (Title, Pubdate, Link) 
            VALUES ('$TitleArray[$i]', '$PubdateArray[$i]', '$LinkArray[$i]')";

}

//Or you could build a single query string inside the loop, and run it at the end.

您可以使用循环来生成查询 -

$Title = explode(', ', $Title);
$Pubdate = explode(', ', $Pubdate);
$Link = explode(', ', $Link);
$sql = "INSERT INTO ytable (Title, Pubdate, Link) VALUES ";
$values= array();
foreach($Title as $key => $val) {
   $values[] = "('" . $val . "','" . (isset($Pubdate[$key]) ? $Pubdate[$key] : '')  . "','" . (isset($link[$key]) ? $link[$key] : '') . "')";
}
$sql .= implode(',', $values);

您需要使用 explodeimplodeforeach 循环。

$Title   = "aa, bb, cc, dd, ee, ff";
$Pubdate = "aa, bb, cc, dd, ee, ff";
$Link    = "aa, bb, cc, dd, ee, ff";

$Title = explode(', ', $Title);
$Pubdate = explode(', ', $Pubdate);
$Link = explode(', ', $Link);
$sql = "INSERT INTO ytable (Title, Pubdate, Link) VALUES ";
$values= array();
foreach($Title as $key => $val)
{
   $values[] = "('" . $val . "','" . $Pubdate[$key] . "','" . $link[$key] . "')";
}
$sql .= implode(',', $values);

警告: MYSQL 扩展已从 PHP 5.5.0 开始弃用,并已从 PHP 7.0.0 开始删除。相反,应该使用 MySQLi 或 PDO_MySQL 扩展。

你需要这样的东西,

$Title = "aa, bb, cc, dd, ee, ff";
$Pubdate = "aa, bb, cc, dd, ee, ff";
$Link = "aa, bb, cc, dd, ee, ff";
$titleArr = explode(",",$Title);
$pubdateeArr = explode(",",$Pubdate);
$linkArr = explode(",",$Link);

foreach($titleArr as $key => $title){
    $insertSQL = "INSERT INTO ytable (Title, Pubdate, Link) VALUES ('$title', '$pubdateeArr[$key]', '$linkArr[$key]')";
}

或者您可以仅迭代值

$a = array();
$insertSQL = "INSERT INTO ytable (Title, Pubdate, Link) VALUES ";

foreach($titleArr as $key => $title){
    $a[] = "('$title', '$pubdateeArr[$key]', '$linkArr[$key]')";
}
$insertSQL .= implode(",",$a);

您可以尝试这样的操作:

$Title = "aa, bb, cc, dd, ee, ff";
$Pubdate = "aa, bb, cc, dd, ee, ff";
$Link = "aa, bb, cc, dd, ee, ff";
$titleArr = explode(",",$Title);
$pubdateeArr = explode(",",$Pubdate);
$linkArr = explode(",",$Link);

foreach($titleArr as $key => $title){
if(!empty($title) && !empty($pubdateeArr[$key]) && !empty($linkArr[$key]))
    $insertSQL = "INSERT INTO ytable (Title, Pubdate, Link) VALUES ('$title', '$pubdateeArr[$key]', '$linkArr[$key]')";
}

我已经尝试过 if 不为空,因为变量不能有相同数量的值。