【发布时间】:2015-05-30 16:26:39
【问题描述】:
我有这个代码(参考下面)
$sql = "SELECT * FROM records";
$result = mysqli_query($this->db,$sql);
while($row = mysqli_fetch_assoc($result)){
$itemid = $row['id'];
$itemname = $row['itemname'];
$itemdesc = $row['itemdesc'];
$brand = $row['brand'];
$serialno = $row['serialno'];
$nostock = $row['nostock'];
$price = $row['price'];
$onsale = $row['onsale'];
$poster = $row['poster'];
$thedate = $row['thedate'];
$sql = "INSERT INTO backupp SET id='$itemid', itemname='$itemname', itemdesc='$itemdesc', brand='$brand', serialno='$serialno', nostock='$nostock', price='$price', onsale='$onsale', poster='$poster', thedate='$thedate'";
$result = mysqli_query($this->db,$sql) or die(mysqli_error($this->db));
return $result;
}
从上面的代码可以看出,它首先会从名为“records”的db表中提取所有数据,然后将每一行放入相应的变量中,然后插入存储的数据(那些从db中提取的数据)表名“records”并存储在相应的变量上)到名为“backupp”的数据库表。现在,问题是,它只将一条记录添加到备份中(首先提取的记录),它应该将所有从名为记录的数据库表中提取的记录添加到名为备份的数据库表中。为什么??任何建议、建议、线索和想法将不胜感激。谢谢!
PS:它类似于导出和导入到具有相同结构的其他表,但我有自己的理由为什么要这样做,并假设我已经成功连接到名为“inventory”的数据库 ($this->db)并且那里有数据库表名,例如具有相同结构的“记录”和“备份”。
【问题讨论】:
-
请阅读一些最新的 PHP 教程,或一些编程教程
标签: php mysql mysqli mysql-insert-id mysqli-multi-query