0

The following code is my homework. But I run in to a obstacle and can't finish it now.

When I insert "$query4" result manually into the mysql, it inserts fine.

But this is not working: "$result4=mysqli_query($conn,$query4);"

When I converted the "work_start" to date format, nothing changed. It didn't want to work. I would like to make an accounting program. I can register user, and add months, but can't add days.

All of them are in different tables. Mysql looks like:

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)

Thanks in advance.

<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>
  • you might be outputting before header; check for errors and for the query. – Funk Forty Niner Mar 28 '18 at 22:52
  • check your conditionals – Funk Forty Niner Mar 28 '18 at 22:54
  • What is the exact error you're getting? – Paulo Hgo Mar 28 '18 at 23:44
  • I didn't get error, just didn't want to insert into the database. It looked like everything ok, expect result4. Finally, I found the problem. I insert user, and month names with special characters (like 'á', 'é'). And when I wanted to add new days, It was required to insert the names again. In this code, unfortunately I forget to set the utf8 while insert. So the names weren't the same with the main tables names. That was the reason why it didn't insert into. Thank you for your helping :) – Veres Richard Mar 29 '18 at 00:13

0 Answers0