PHP date() - 插入MySQL数据库。布卢姆(假)

时间:2018-03-28 22:44:51

标签: php html mysql

以下代码是我的作业。但我遇到了障碍,现在无法完成。

当我手动将“$ query4”结果插入mysql时,它插入正常。

但这不起作用:“$ result4 = mysqli_query($ conn,$ query4);”

当我将“work_start”转换为日期格式时,没有任何改变。它不想工作。我想做一个会计程序。我可以注册用户,并添加月份,但不能添加天数。

所有这些都在不同的表格中。 Mysql看起来像:

berkalkulator
  -users
     -user name (primary key)
     -password
   -month
     -user name (foreign key)
     -month name (primary key)
  -workdays =munkaido
      -user name (foreign key)   (varchar)
      -month name (foreign key)  (varchar)
      -start date                (datetime)
      -finish date               (datetime)

提前致谢。

<html>
<head>
    <meta name="author" content="" />
    <meta http-equiv="Content-Language" content="hu" /> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script language="Javascript" type="text/javascript" src="functions.js" src="jquery-3.3.1.js"></script>
</head>
<body>
<?php
session_start();
$servername = "localhost";
$username = "root"; 
$password = ""; 

$conn = mysqli_connect($servername, $username, $password, "berkalkulator"); 

if ($conn->connect_error)  
{ 
    die("Kapcsolódási hiba" . $conn->connect_error); 
}  
if(isset($_POST['month']))
{
    $_SESSION['month']=$_POST['month'];
}


if(isset($_SESSION['month']))
{
    if(isset($_POST['add_new_day']))
    {
    $month_name=$_SESSION['month'];

    $start_date = new DateTime($_POST['work_start']);
    $start = $start_date->format('Y-m-d H:i:s');


    $finish_date= new DateTime($_POST['work_finish']);
    $finish = $finish_date->format('Y-m-d H:i:s');


    $query3= "SELECT user_name FROM munkaido WHERE user_name LIKE '" . $_SESSION['name'] ."' AND 
                                                                    month_name LIKE '" . $month_name ."' AND 
                                                            Date_work_start LIKE '" . $start . "' AND
                                                               Date_work_finish LIKE '" . $finish . "';";
    $result3=mysqli_query($conn,$query3);
    //var_dump($result3); --> object(mysqli_result)#4 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }
    $row3 =mysqli_fetch_array($result3);
    if($row3==0)
    {
        $query4="INSERT INTO `munkaido` (`user_name`, `month_name`, `Date_work_start`, `Date_work_finish`) 
        VALUES('" . $_SESSION['name'] . "' ,'" . $month_name . "','" . $start . "','" . $finish . "');";
        //echo $query4;
        $result4=mysqli_query($conn,$query4);
        var_dump($result4); //bool(false)???
        echo
        "
        <script> alert('Sikeres munkaidő felvétel')</script>
        ";
        //header("Refresh:0");
    }
    else
    {
        echo
        "
        <script> alert('Már van ilyen dátum, ilyen idővel az adatbázisodban')</script>
        ";
    }
    }
}


?>


<button id='megnyit' onclick='lathatova_teszi()'>+</button>
<form name='munkaido_hozzad' method=post>
    <div id='ezt' style='visibility: hidden'>
        Munka start:<input id="Date_work_start" name="work_start" type="datetime-local">
        Munka vége:<input id="Date_work_finish" name="work_finish" type="datetime-local">
        <br/>
        <input type='submit' name='add_new_day' value='Új nap hozzáadása' >
    </div>
</form>
</body>
</html>

0 个答案:

没有答案