2

我需要将以下数组插入到具有列名称、标题、网址、徽标的 mysql 表中。我创建了表并编写了一个查询来传输数组。但它正在插入数组的最后一行并留下剩余的......请帮助我......这是我的数组

$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());
4

4 回答 4

5

您需要遍历$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 注入

于 2012-09-28T16:53:37.777 回答
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 .= ", ";
  }

在这里查看它的实际应用。

于 2012-09-28T17:01:22.073 回答
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());
于 2012-09-28T17:05:10.500 回答
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);
}
于 2012-09-28T17:10:19.253 回答