2

次の配列を、name、title、url、logoの列を持つ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());
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());
}

または、提案されているように、1つのクエリに複数のレコードを挿入するには:

$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が提案したように、コードに次の変更を加えることで、1つの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の仕事です! とにかく mysql_ の代わりに PDO または mysqli を使用する必要がありますが、それはまったく別の議論です。

あなたがしたいことはこれです:

// ========================================================
// 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 に答える