Php 如何将POST数据输入数据库?

Php 如何将POST数据输入数据库?,php,mysql,mysqli,Php,Mysql,Mysqli,示例代码: echo $mylinks; 输出回波: http://www.google1.com|http://www.google2.com 我想将它们中的每一个插入到数据库表中 以下是我试图使用的: INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, '".$mylinks."', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP); 问

示例代码:

echo $mylinks;
输出回波:

http://www.google1.com|http://www.google2.com
我想将它们中的每一个插入到数据库表中

以下是我试图使用的:

INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, '".$mylinks."', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP);
问题是,它基本上是这样插入数据库的:

INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, 'http://www.google1.com|http://www.google2.com', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP);
而我希望每个URL都像这样输入到自己的行中:

INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, 'http://www.google1.com', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP);
INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, 'http://www.google2.com', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP);
$array = explode('|', $mylinks);
foreach($array as $key => $value)
     //Insert $value into table
URL由管道(|)分隔-通常有多个URL被提交,但有时只有一个URL-以防造成差异


那么,如何将URL提交到数据库中,使每个URL都位于单独的行上呢?

mysql不会为您拆分这些URL,您需要自己拆分

$stmt = $pdo->prepare("INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL,:link, '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP)");


foreach (explode("|", $mylinks) as $link) {
    $stmt->execute(array(":link" => $link))
}

您可以使用php explode函数将此字符串转换为数组,然后插入如下数据:

INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, 'http://www.google1.com', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP);
INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, 'http://www.google2.com', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP);
$array = explode('|', $mylinks);
foreach($array as $key => $value)
     //Insert $value into table

使用
explode()
函数,它将为您提供一个数组。然后只需循环遍历数组
foreach
,然后编写插入查询。有没有办法用mysqli替代?是的,只需使用相应的
mysqli\u prepare
mysqli\u stmt\u execute
instead@Edward由于多种原因,PDO比mysqli好得多。如果你有机会使用PDO,就去做吧。