pkarthicbz pkarthicbz - 5 months ago 22
PHP Question

session error in php forms

This is my code:-

<html>
<body>
<center>
<h2>working time</h2>
<form action="" method="post">
<button name="starttime" class="starttime">starttime</button>
<?php
session_start();
$name = $_SESSION["name"];
if(isset($_POST["starttime"])){
$date = date('Y-m-d');
$date1 = date('H:i:s');
require "database.php";
$n = "";
$d = "";
$sql = "SELECT name, ddate FROM karthick.date WHERE name = '$name' AND ddate = '$date'";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$n = $row["name"];
$d = $row["ddate"];
}
}
if($n == $name && $d == $date){
echo'<script>
alert("Time already registered");
</script>';
}else{
$sql = "INSERT INTO karthick.date (name, ddate, starttime) VALUES ('$name', '$date', '$date1')";
if($conn->query($sql) === TRUE){
echo "starttime inserted";
}else{
echo "error";
}
}
}
?>
</form>
<form action = "" method="post">
<button name="endtime" class="endtime">Endtime</button>
<?php
session_start();
$name = $_SESSION["name"];
if(isset($_POST["endtime"])){
$date = date('Y-m-d');
$date1 = date('H:i:s');
require "database.php";
$n = "";
$d = "";
$sql = "SELECT name, ddate, endtime FROM karthick.date WHERE name = '$name' AND ddate = '$date'";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$e = $row["endtime"];
}
}
if($e != '00:00:00'){
echo'<script>
alert("Time already registered");
</script>';
}else{
$sql = "UPDATE karthick.date SET endtime='$date1' WHERE name = '$name' AND ddate = '$date'";
if($conn->query($sql) === TRUE){
echo "Endtime inserted.<br>";
}else{
echo "error";
}
$sql1 = "UPDATE karthick.date SET worked = (endtime - starttime) WHERE name = '$name' AND ddate = '$date'";
if($conn->query($sql1) == TRUE){
echo "worked time updated";
}else{
echo "error";
}
}
}
?>
</form>
<form action="user.php" method="post">
<button name= "logout" class="logout">Logout</button>
</form>
</body>
</html>


when i run this code i shows me a error named


Notice:A session had already been started - ignoring session_start()


But the code works fine. Is there any way to hide the error from showing?

enter image description here

Answer

Only one time session_start() is needed on a page (on top). You have it two times in your page so remove the second one.

Suggestion:-

Always use some error reporting code.

example:-

error_reporting(E_ALL); // check all type of errors
ini_set('display_errors',1);// display those errors 

Always add this to your php script in the beginning. It will help you a lot. Thanks.

Comments