将数组数据插入mysql表

时间:2015-07-29 11:04:31

标签: php mysql

如何使用下面的代码将网页中的数组数据插入到mysql表中。 实际上我想将数据从xls / csv / xml文件导入到网页然后想要插入到mysql表中。喜欢这个链接的教程:http://www.ibm.com/developerworks/library/os-phpexcel/



    <html>
<body>
These records have been added to the database:
<table>
<tr>
<th>First</th>
<th>Middle</th>
<th>Last</th>
<th>Email</th>
</tr>
<?php foreach( $data as $row ) { ?>
<tr>
<td><?php echo( $row['first'] ); ?></td><
<td><?php echo( $row['middle'] ); ?></td><
<td><?php echo( $row['last'] ); ?></td><
<td><?php echo( $row['email'] ); ?></td><
</tr>
<?php } ?>
</table>
Click <a href="list.php">here</a> for the entire table.
</body>
</html>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

$columns = implode(", ",array_keys($row));
$escaped_values = array_map('mysql_real_escape_string', array_values($insData));
$values  = implode(", ", $escaped_values);
$sql = "INSERT INTO `table`($columns) VALUES ($values)";

答案 1 :(得分:0)

<?php foreach( $data as $row ) { 
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
}
?>

答案 2 :(得分:0)

<?php foreach( $data as $row ) { 
$sql= "INSERT INTO table_name (`first`, `middle`, `last`,`email`)VALUES (".$row['first'].", ".$row['middle'].", ".$row['last'].",".$row['email'].")";
}
$query = mysql_query($sql);
?>