Add to column in table using Checkbox

Hi guys. I was wondering if there was a way if I could check the checkbox and it would somehow detect the date of when it was checked and automatical add it to a certain column and row. here is what iv done so far

Table
[php]

<?php $con=mysqli_connect("localhost","root","","User_db"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql="INSERT INTO Assessments (Title, Lecturer, Start, Due, Completed) VALUES ('$_POST[Title]','$_POST[Lecturer]','$_POST[Start]','$_POST[Due]','$_POST[Completed]')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error()); } header( "Location: Assessment_Calendar.php" ) ; mysqli_close($con); ?>

[/php]

Adding shedules
[php]

Title Name:
Module Lecturer:
Start Date:
Due Date:

[/php]

Shedules
[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 ""; while($row = mysqli_fetch_array($result)) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
Title Lecturer Start Due Completed
" . $row['Title'] . "" . $row['Lecturer'] . "" . $row['Start'] . "" . $row['Due'] . "" . $row['Completed'] . "
"; mysqli_close($con); ?> [/php]

In the form I left out the “completed” column which I added on the table. I want to use the checkbox to insert the date on the “completed” column. is there any way using php/javascript that I could possibly detect the detect the date and add it to the column “Completed”.

Assuming the Completed field has a type of datetime, this should work:

[php]

<?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); ?>

[/php]

Actually, I think that should be:

[php]

<?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); ?>

[/php]

Parse error: syntax error, unexpected ‘$strCompleted’ (T_VARIABLE), expecting ‘(’ on line 10. Iv tried everything it still gives an error

Try this:

[php]$sql=“INSERT INTO Assessments (Title, Lecturer, Start, Due, Completed)
VALUES
(’” . $_POST[Title] . “’,’” . $_POST[Lecturer] . “’,’” . $_POST[Start] . “’,’” . $_POST[Due] . “’,” . $strCompleted . “)”;
[/php]

Better?

This syntax is invalid, missing parentheses around if statement, missing bracket before else:

[php]
$strCompleted = $_POST[Completed];
if $strCompleted == “Completed”{
$strCompleted = “NOW()”;
else{
$strCompleted = “’’”;
}
[/php]

It should be:

[php]
$strCompleted = $_POST[‘Completed’];
if ($strCompleted == “Completed”) {
$strCompleted = “NOW()”;
}
else {
$strCompleted = “’’”;
}
[/php]

Or you could just use a ternary

[php]$strCompleted = (isset($_POST[‘Completed’]) && $_POST[‘Completed’] == ‘Completed’ ? “NOW()” : “’’”);[/php]

Ok thanx it works now no syntax error but a ’ gets added to the column Completed. I want it to be blank until I mark the checkbox, where if I mark the checkbox, the date of when I mark it is what should be added to the compelted column

Post your full (current) code again.

I also want to point out that this is highly insecure. Do you know about prepared statements?

http://www.php.net/mysqli_prepare

Sponsor our Newsletter | Privacy Policy | Terms of Service