doomli3721 2014-01-03 23:48
浏览 72
已采纳

将日期数组插入MySQL

I am coding a kind of simple registration form for a company for employees to select the dates the will be available for work. I am having trouble when a user selects multiple dates, it writes 0000-00-00 in the database. I would like it to make two records of both dates.

This is my basic form code:

<form method="post" action="action.php">
<input name="UserID" type="hidden" value="3">
<input type="checkbox" name="Datum[]" value="2014-01-04">4 january
<input type="checkbox" name="Datum[]" value="2014-01-06">6 january
<input name="submit" type="submit" />
</form>

My action.php looks like this:

<?php

include("config.php");

$UserID=$_POST['UserID'];
$Datum=$_POST['Datum'];


?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test</title>
</head>

<body>

 <?php 

       $res = mysql_query("insert into workingdays (UserID, Datum) 
                 values ('$UserID', '" . implode(',', $_POST['Datum']) ."')");

                if(!$res){
                echo "Error! <a href='/mysql'>Go back.</a>";
                }
                else {
                echo "Succesful. <a href='/mysql'>Go back.</a>";
                }

         ?>


</body>
</html>

I use two tables.

wp_users (the actual wordpress table):

ID | display_name | Email

workingdays:

ID | UserID | Date

The UserID of 'workingdays' and the ID of wp_users are joined. I would like to see every option of the checkbox get its own row.

How can I achieve this?

  • 写回答

2条回答 默认 最新

  • dongzhang8680 2014-01-03 23:53
    关注

    First foreach the $_POST['Datum'] then mysql insert.

    foreach ($_POST['Datum'] as $date){
       $res = mysql_query("insert into workingdays (UserID, Date) 
                values ('$UserID', '$date')");
    
                if(!$res){
                echo "Error! <a href='/mysql'>Go back.</a>";
                }
                else {
                echo "Succesful. <a href='/mysql'>Go back.</a>";
                }
    }
    

    I hope this is what you meant..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测