0

私は古い、言葉遣いの悪い質問を削除し、誰かの時間を無駄にしないように再投稿しています。

2 つのテーブル、部屋、アイテムからクエリを実行しようとしています。次に、ネストされたループで、最初の 2 つの情報を使用して 3 番目のテーブルにエントリを作成します。

「各部屋には、すべての標準アイテムを挿入してください」

<?php
mysql_connect("******", "****", "******") or die(mysql_error());
mysql_select_db("MaintenanceTracking") or die(mysql_error());// Check connection

//collect standard items names
$stditemdata = 'SELECT * FROM `StandardItems`';
$itemresult = mysql_query($stditemdata) or die("Couldn't execute query. ". mysql_error());
$itemarray = mysql_fetch_array( $itemresult ));

//collect room info
$roomdata = 'SELECT * FROM `Rooms`';
$roomresult = mysql_query($roomdata) or die("Couldn't execute query. ". mysql_error());

//repeat for each room
while($room = mysql_fetch_array( $roomresult )) 
{
    //repeat for each item
 for ($i = 0; $i <= count($itemarray); $i++)
   {
      mysqlquery("INSERT into Items 
      (ItemNumber, Name, LocationCode)
      VALUES 
      (NULL, $itemarray['Name'], $room['LocationCode'])");
   }
}
?>

私はphpにかなり慣れていないため、構文が時々困惑することをお詫びする必要があります...たとえば、行の末尾にあるセミコロンを見逃していることで有名です。

私を助けてくれるすべての人に、前もって百万の感謝を捧げます。

よろしくお願いします

4

1 に答える 1

0
  mysqlquery("INSERT into Items 
  (ItemNumber, Name, LocationCode)
  VALUES 
  (NULL, $itemarray['Name'], $room['LocationCode'])");

そのはず

  mysql_query("INSERT into `Items` 
  (`ItemNumber`, `Name`, `LocationCode`)
  VALUES 
  (NULL, $itemarray['Name'], $room['LocationCode'])");

mysql_queryの代わりにmysqlqueryを使用している

mysql で予約されている名前 (日付、テーブルなど) の重複を避けるには、次の構文を使用します。

`column_name` or `table_name`

アップデート

ああ.. 懐かしい!ほら、ここでDBにいくつかの文字列を書き込もうとします

  mysql_query("INSERT into `Items` 
  (`ItemNumber`, `Name`, `LocationCode`)
  VALUES 
  (NULL, $itemarray['Name'], $room['LocationCode'])");

クエリ内のすべての文字列は、引用符 single 'または double "で終了する必要があるため、クエリは次のようになります。

  mysql_query("INSERT into `Items` 
  (`ItemNumber`, `Name`, `LocationCode`)
  VALUES 
  (NULL, \"$itemarray['Name']\", \"$room['LocationCode']\")");

(引用符をエスケープするために " の前に \ 記号を使用します)、次のような構文を使用することをお勧めします。

  mysql_query("INSERT into `Items` 
  (`ItemNumber`, `Name`, `LocationCode`)
  VALUES 
  (NULL, '".$itemarray['Name']."', '".$room['LocationCode']."')");
于 2013-04-15T16:46:50.353 に答える