我是php的新手..这是我的问题:我有2个爆炸的字符串,我想让第2个爆炸的字符串成为第1个爆炸字符串的值,这个结果我想插入我的数据库,但插入时没有任何问题。
例如,在下面的代码中,我希望$ date是$ layer的值。 $ layer的第一个单词是$ date的第一个单词,第二个单词到第二个单词的值等等。
$layer1=$_POST['layer'];
$date=$_POST['date'];
$layer=explode(",",$layer1);
$date=explode(",",$date);
foreach ($layer as $layer2=>$date )
{
//my problem is how to make the date a value to my layer?
$con=mysqli_connect("localhost","xxxx","xxx") or die ('unable to connect server');
mysqli_select_db($con, "xxxxxxxx") or die ('cant connect database');
$x="INSERT INTO xxxxx (Layer, date ) VALUES ('$layer2', '$date') ";
$result=mysqli_query($con,$x) or die ('cant proceed query');
//i want to appear look like this on database:
LAYER DATE
layer1 date1
layer2 date2
layer3 date3
say my data inputted from form is this one:
$_POST['layer']=layer1,layer2,layer3
$_POST['date']=date1,date2,date3
答案 0 :(得分:0)
$con = mysqli_connect("localhost","xxxx","xxx", "xxxxxxxx");
$layer_arr = explode(",",$_POST['layer']);
$date_arr = explode(",",$_POST['date']);
foreach ($layer_arr as $key => $layer )
{
$date = $date_arr[$key];
$date = mysqli_real_escape_string($con, $date);
$layer = mysqli_real_escape_string($con, $layer);
$sql = "INSERT INTO xxxxx (Layer, date ) VALUES ('$layer', '$date') ";
$result=mysqli_query($con,$x) or trigger_error(mysqli_error($con));
}
请注意,您不必手动格式化字符串,而是必须在查询中使用占位符,因此使用PDO而不是mysqli。