我需要将以下数组插入到具有列名称,标题,URL,徽标的mysql表中。我创建了表并编写了一个查询来传输数组。但它插入阵列的最后一行并留下剩余的... plz帮助我...... 这是我的数组
$con = mysql_connect("localhost","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("media", $con);
$featured = array();
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$sql = "INSERT INTO featured";
$sql .= " (`".implode("`, `", array_keys($featured))."`)";
$sql .= " VALUES ('".implode("', '", $featured)."') ";
$result = mysql_query($sql) or die(mysql_error());
答案 0 :(得分:3)
您需要遍历$featured
数组:
$con = mysql_connect("localhost","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("media", $con);
$featured = array();
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
foreach ($featured as $array) {
$sql = "INSERT INTO featured";
$sql .= " (`".implode("`, `", array_keys($array))."`)";
$sql .= " VALUES ('".implode("', '", $array)."') ";
$result = mysql_query($sql) or die(mysql_error());
}
或者按照建议在一个查询中插入几条记录:
$sql = "INSERT INTO featured";
$sql .= " (`".implode("`, `", array_keys($featured[0]))."`) VALUES"; // make sure that $featured contains the same number of key=>value pairs
foreach ($featured as $array) {
$sql .= " ('".implode("', '", $array)."'),";
}
$sql = substr($sql,0,-1); // remove last comma
$result = mysql_query($sql) or die(mysql_error());
请阅读SQL injections。
答案 1 :(得分:1)
您可以将multiple insert
查询用作INSERT INTO (column1, column2,...) VALUES ('row1a', 'row1b'..), ('row2a', 'row2b'..)...
$featured = array();
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$sql = "INSERT INTO featured ";
$sql .= " (`".implode("`, `", array_keys($featured))."`)";
$sql .= " VALUES ";
for( $i=0; $i < count($featured); $i++) {
$sql .= "('". implode("', '", $featured[$i])."') ";
if( $i < (count($featured) - 1) )
$sql .= ", ";
}
在行动here中查看。
答案 2 :(得分:0)
正如hakra建议你可以通过对代码进行以下更改,使用一个mysql查询插入多行:
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$featured[] = array('name' => '', 'title' => '', 'url' => '', 'logo' => '');
$sql = "INSERT INTO featured";
$sql .= " (`".implode("`, `", array_keys($featured))."`)";
$sql .= " VALUES "; // moved parenthetical grouping to foreach loop below
foreach ($featured as $feature) {
$sql .= "('".implode("', '", $featured)."'), "; // note comma after each parenthetical grouping
}
$sql = substr($sql, 0, -2); // we'll cut off the last ", " added to the string
$result = mysql_query($sql) or die(mysql_error());
答案 3 :(得分:0)
这是PDO的工作!无论如何你应该使用PDO或mysqli而不是mysql_,但这是另一个讨论。
你想要做的是:
// ========================================================
// Define insert rows. Note the colon before each key name.
// That's there because we'll be passing this to a PDOStatement as bound
// parameters. Depending on the logic of your program there may be different
// ways you want to handle this.
// ========================================================
$featured = array();
$featured[] = array(':name' => '', ':title' => '', ':url' => '', ':logo' => '');
$featured[] = array(':name' => '', ':title' => '', ':url' => '', ':logo' => '');
$featured[] = array(':name' => '', ':title' => '', ':url' => '', ':logo' => '');
$featured[] = array(':name' => '', ':title' => '', ':url' => '', ':logo' => '');
// ========================================================
// Create the PDO object. Define your query and prepare a statement. I use the
// INSERT SET syntax rather than the INSERT VALUES syntax purely for readability.
// Either will work with PDO. Also, most programmers don't set the query_str as a
// separate variable as I do here. I find it improves readability.
// ========================================================
$db_obj = new PDO (/*connection params go here */);
$query_str = "INSERT INTO featured SET
name = :name,
title = :title,
url = :url,
logo = :logo";
$query_obj = $db_obj->query ($query);
// ========================================================
// Loop through your rows in $featured and execute the SQL query, passing the
// new row of parameters each time.
// ========================================================
foreach ($featured as $row) {
$query_obj->execute ($row);
}