查询将“Array”而不是value插入数据库

时间:2015-12-03 05:09:25

标签: php mysql arrays pdo

尝试使用复选框设置调度程序,当我检查多个时隙时,我希望将每个时间插入到我的表中。

我在我的数据库中设置了时间段作为枚举,所以这只是以复选框形式显示所有时间。

    if (resultCode == RESULT_OK) {
        if (requestCode == CAMERA_REQUEST) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

            try {
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                File outFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "myimage.jpg");
                FileOutputStream fos = new FileOutputStream(outFile);
                photo.compress(Bitmap.CompressFormat.JPEG, 100, fos);

                fos.flush();
                fos.close();
                getImages();

此代码应该将每个选中的项插入到数据库中,并在时间段中显示所有相同的信息。

echo "<table>";
echo "<tr>";				
  echo("<td><input type=checkbox name=time_slot[] value='$option'>$option</td>");
echo "</tr>";
echo "</table>";
if(isset($_POST['submit'])) { $ts = $_POST['time_slot']; $db = new PDO ("mysql:host=localhost; dbname=zzzzz", "zzzzz", "12345"); for ($i=0; $i<sizeof($ts);$i++) { $query = "INSERT INTO timeslot (date, duration, firstName, lastName) VALUES ('".$_POST['year']."-".$_POST['month']."-".$_POST['day']."', '$ts', '".$_SESSION['firstName']."', '".$_SESSION['lastName']."')"; $state = $db->prepare($query); $state->execute(); print_r($state); } print_r($ts);输出print_r($state)的每个循环。

INSERT INTO timeslot (date, duration, firstName, lastName) VALUES ('2020-04-20', 'Array','John', 'Doe')输出我检查过print_r($ts)

的所有时间段

如何解决此问题?谢谢。

2 个答案:

答案 0 :(得分:2)

如果你循环$ ts,你可能想要使用$ i来获取每个循环上该索引的值。 '" . $ts[$i] . "'

$query = "INSERT INTO timeslot (date, duration, firstName, lastName) VALUES ('".$_POST['year']."-".$_POST['month']."-".$_POST['day']."', '" . $ts[$i] . "', '".$_SESSION['firstName']."', '".$_SESSION['lastName']."')";

答案 1 :(得分:0)

使用以下时间段获取逗号分隔值:

$ts = implode(",",$_POST['time_slot']);