0

我是 php 新手 .. 这是我的问题:我有 2 个展开的字符串,我想让第 2 个展开的字符串成为第一个展开的字符串的值,结果我想插入到我的数据库中,但是我插入没有任何问题。

例如,在下面的代码中,我希望 $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
4

1 回答 1

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。

于 2013-04-07T15:26:40.777 回答