[英]How do I insert an array into a database in php using MySQL?
My code is: 我的代码是:
<?php
include("connect.php");
mysql_select_db("cars",$conec);
car = array("BMW","Rolls-Royce","Lamborghini","Mustang");
color = array("red","green","blue","yellow");
?>
What I want to do is insert each car into a database and this is what I tried to do but I only get the id increasing becuase it is an auto-increment. 我想做的是将每辆车插入数据库,这是我尝试做的事,但是我得到的id却越来越多,因为它是自动递增的。 This is my insertion code below:
这是我的插入代码如下:
for($i = 0; $i < 4; $i++){
$res = mysql_query("insert into auto (car,colors) values ('$car[$i]','$color[$i]')");
}
Tested and working 经过测试和工作
<?php
DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');
DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_NAME', 'xxx');
$mysqli = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die("could not connect");
$car = array("BMW","Rolls-Royce","Lamborghini","Mustang");
$color = array("red","green","blue","yellow");
for($i = 0; $i < 4; $i++){
$res = mysqli_query($mysqli,"insert into auto (car,colors) values ('$car[$i]','$color[$i]')");
}
?>
If this is your actual code that you posted: 如果这是您发布的实际代码:
You forgot the $
signs for your car
and color
variables. 您忘记了
car
和color
变量的$
符号。
car = array("BMW","Rolls-Royce","Lamborghini","Mustang");
^-- // here
color = array("red","green","blue","yellow");
^-- // and here
<?php
include("connect.php");
mysql_select_db("cars",$conec);
$car = array("BMW","Rolls-Royce","Lamborghini","Mustang");
$color = array("red","green","blue","yellow");
?>
I think it is generally frowned upon to put PHP arrays in MySQL since they can only be read by PHP. 我认为将PHP数组放在MySQL中通常不被接受,因为它们只能被PHP读取。 You should run
你应该跑
$car = json_encode($car);
$color = json_encode($color);
Then when you want to pull the info from the database run 然后,当您想从数据库中提取信息时运行
$car = json_decode($mysqlcarvarhere);
$color = json_decode($mysqlcolorvarhere);
Also, on your second bit of code, that will insert '$car[$i]' into that database instead of the VALUE of $car['$i'] into the database. 另外,在第二段代码中,这会将'$ car [$ i]'插入数据库,而不是将$ car ['$ i']的值插入数据库。 To fix this you should change your query to:
要解决此问题,您应该将查询更改为:
$res = mysql_query("insert into auto (car,colors) values ('" . $car[$i] . "','" . $color[$i] ."')");
Also, generic "you should switch to PDO/MySQLi or else you'll get hacked" speech here. 同样,在这里也有一般性的“您应该切换到PDO / MySQLi,否则您将被黑”。
I would recommend you to use Prepared Statement , for all kinds of reasons . 出于各种原因 ,我建议您使用“ 准备的语句” 。
Assuming a connection is already made to the database, here is one way to do it : 假设已经与数据库建立了连接,这是一种实现方法:
<?php
include("connect.php");
$cars = array("BMW","Rolls-Royce","Lamborghini","Mustang");
$colors = array("red","green","blue","yellow");
$db = new mysqli($host, $user, $password, $database);
$stmt = $db->prepare("INSERT INTO auto(car, color) VALUES(?, ?)");
// Here I deliberately assume that all car has every color variant
foreach($cars as $car) {
foreach($colors as $color) {
$stmt->bind_param('s', $car);
$stmt->bind_param('s', $color);
$stmt->execute();
}
}
$stmt->close();
?>
It's just a rough idea how to solve it with Prepared Statement, you could always wrap it up in a function or a class. 如何使用Prepared Statement解决它只是一个粗略的主意,您始终可以将其包装在一个函数或一个类中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.