2015-08-17 21 views
1

不同行我有三个变量:$标题,$ pubdate的和$链接把几个值成一列,但在PHP

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

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

喜欢这个。 然后我有一个表格(标题,出版和链接)。 我曾尝试:

$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);,并且它将所有值放在一列中的不同行中

+0

分解它,然后尝试循环。 –

+0

'$ Title','$ Pubdate'和'$ Link'的值总是相同的吗? – Hassaan

+0

@Hassan不,不会 –

回答

2

你将不得不拆分你的字符串把它放到一个数组中,这样你就可以循环它们。

$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. 
+0

如果在单个值内有逗号,我该怎么办? –

+0

当您构建字符串时,您需要使用引号作为每个值周围的分隔符。即:'$ Title =''aa','bb','cc','dd','ee','ff','g,g'等等。 – MaggsWeb

1

您可以使用一个循环来生成查询这么做 -

$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); 
0

您需要使用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扩展。

0

你需要这样的事情,

$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); 
0

你可以尝试这样的事情:

$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]')"; 
} 

我已如果不是空的尝试因为变量不能有相同数量的值。