Automatically add date to database table when button clickde

Ask about a PHP problem here.
Post Reply
azmal101
Posts: 5
Joined: Wed Mar 27, 2013 3:32 pm

Automatically add date to database table when button clickde

Post by azmal101 »

Adding Assessment Form PHP

<?php
 $con=mysqli_connect("localhost","root","","User_db");
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

 $result = mysqli_query($con,"SELECT * FROM Assessments");

 echo "<table border='0' cellpadding='40'
cellspacing='0'>
 <tr>
 <th>Title</th>
 <th>Lecturer</th>
 <th>Start</th>
 <th>Due</th>
 <th>Completed</th>

 </tr>";

 while($row = mysqli_fetch_array($result))
   {
   echo "<tr>";
   echo "<td>" . $row['Title'] . "</td>";
   echo "<td>" . $row['Lecturer'] . "</td>";
   echo "<td>" . $row['Start'] . "</td>";
   echo "<td>" . $row['Due'] . "</td>";
   echo "<td>" . $row['Completed'] . "<form action='' align='center'>
<input type='checkbox' name='Completed' value='Completed'/><br/>
<input type='submit' name='Completed' value='Completed'/>
</form></td>";
   echo "</tr>";
   }
 echo "</table>";

 mysqli_close($con);
 ?> 

Inserting Assessments to table

<?php
 $con=mysqli_connect("localhost","root","","User_db");
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

$strCompleted = $_POST['Completed'];
if ($strCompleted == "Completed") {
	

$strCompleted = "NOW()";
}
else {
	

$strCompleted = "";
}

 $sql="INSERT INTO Assessments (Title, Lecturer, Start, Due, Completed)
 VALUES
 ('$_POST[Title]','$_POST[Lecturer]','$_POST[Start]','$_POST[Due]','$strCompleted')";

 if (!mysqli_query($con,$sql))
   {
   die('Error: ' . mysqli_error());
   }
    header( "Location: Assessment_Calendar.php" ) ;


 mysqli_close($con);
 ?>

Hi guys I was wondering if, using JQuery or anything possible, I could could click on the "Completed" button and it would check the date on which it was created and it would enter that detail in the "Completed" column
In my Assessment table in the sql database[/quote]
User avatar
killfrog47
Posts: 106
Joined: Tue Mar 12, 2013 2:52 am
Location: Tempe, AZ
Contact:

Re: Automatically add date to database table when button cli

Post by killfrog47 »

azmal101 wrote:Adding Assessment Form PHP

<?php
 $con=mysqli_connect("localhost","root","","User_db");
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

 $result = mysqli_query($con,"SELECT * FROM Assessments");

 echo "<table border='0' cellpadding='40'
cellspacing='0'>
 <tr>
 <th>Title</th>
 <th>Lecturer</th>
 <th>Start</th>
 <th>Due</th>
 <th>Completed</th>

 </tr>";

 while($row = mysqli_fetch_array($result))
   {
   echo "<tr>";
   echo "<td>" . $row['Title'] . "</td>";
   echo "<td>" . $row['Lecturer'] . "</td>";
   echo "<td>" . $row['Start'] . "</td>";
   echo "<td>" . $row['Due'] . "</td>";
   echo "<td>" . $row['Completed'] . "<form action='' align='center'>
<input type='checkbox' name='Completed' value='Completed'/><br/>
<input type='submit' name='Completed' value='Completed'/>
</form></td>";
   echo "</tr>";
   }
 echo "</table>";

 mysqli_close($con);
 ?> 

Inserting Assessments to table

<?php
 $con=mysqli_connect("localhost","root","","User_db");
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

$strCompleted = $_POST['Completed'];
if ($strCompleted == "Completed") {
	

$strCompleted = "NOW()";
}
else {
	

$strCompleted = "";
}

 $sql="INSERT INTO Assessments (Title, Lecturer, Start, Due, Completed)
 VALUES
 ('$_POST[Title]','$_POST[Lecturer]','$_POST[Start]','$_POST[Due]','$strCompleted')";

 if (!mysqli_query($con,$sql))
   {
   die('Error: ' . mysqli_error());
   }
    header( "Location: Assessment_Calendar.php" ) ;


 mysqli_close($con);
 ?>

Hi guys I was wondering if, using JQuery or anything possible, I could could click on the "Completed" button and it would check the date on which it was created and it would enter that detail in the "Completed" column
In my Assessment table in the sql database
I know this reply is a little late but what you can do is make the "Completed" column in the database a DATETIME type or TIMESTAMP type. That way it will just log the current time of when the data was submitted to the database.
Post Reply