使用php更新MySQL中的日期行和时间行

时间:2017-03-20 20:19:29

标签: php mysql

当我尝试更新表格中的日期和时间时,我收到以下错误:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   在'date ='7890-06-06'time = '12:34:00''第3行附近

我不完全确定为什么我这样做,因为我在sql代码中使用''并且表中的格式都是正确的。

PHP代码:

<?php

    include 'database_conn.php';

    if(!$conn)
    {
        echo "Error connecting to database";
    }

    if (mysqli_connect_errno()) 
    {
        echo "<p>Connection failed:".mysqli_connect_error()."</p>\n";
    }

    $eventid = isset($_GET['eId']) ? $_GET['eId'] : NULL; 
    $title = isset($_REQUEST['titleField']) ? $_REQUEST['titleField'] : '';
    $time = isset($_REQUEST['timeField']) ? $_REQUEST['timeField'] : '';
    $date = isset($_REQUEST['dateField']) ? $_REQUEST['dateField'] : '';
    $location = isset($_REQUEST['locationField']) ? $_REQUEST['locationField'] : '';
    $description = isset($_REQUEST['descriptionField']) ? $_REQUEST['descriptionField'] : '';

    //echo "Event: $eventid";
    //echo "User: $uid";
    //echo "Comment: $comment";
    echo "$date";
    echo "$time";       

    $sql = "UPDATE Events
            SET name = '$title'                        
                date = '$date'
                time = '$time'
                location = '$location'
                description = '$description'

            WHERE eventid = '$eventid'
            ";

    $results = mysqli_query($conn, $sql)
         or die(mysqli_error($conn));

    if($results)
    {
        echo "Event successfully edited";
    }

    mysqli_close($conn);

?>

不确定表示表格的最佳方式,但现在是:

#   Name    Type
1   eventid int(11)
2   name    varchar(255)
3   date    date
4   time    time
5   location    varchar(255)
6   description varchar(1000)

1 个答案:

答案 0 :(得分:0)

你应该在集合语句之间添加逗号分隔',':

   $sql = "UPDATE Events
                SET name = '$title',                        
                    date = '$date',
                    time = '$time',
                    location = '$location',
                    description = '$description'

                WHERE eventid = '$eventid'
                ";