0

I have created a support form for some clients, they submit information to it and I see it in a panel I have created. In this panel I want to have the ability to check a box next to a record and hit an update button and it will update that record with a time stamp showing I have completed the work and what time. Here is the sample code

 echo "<table border='1'>
<tr>
<th>ICL</th>
<th>PAGE ID</th>
<th>SUBMISSION NAME</th>
<th>UPTDATES/BUGS</th>
<th>MISSING INFO</th>
<th>TIME OF SUBMISSION</th>
<th>COMPLETED</th>
<th>TIME COMPLETED</th>
</tr>";
echo "<form name='completed' action='insert4panel.php' method='post'>";
while($row = mysql_fetch_array($results))
  {
  echo "<tr>";
  echo "<td>" . $row['ICL'] . "</td>";
  echo "<td>" . $row['officename'] . "</td>";
  echo "<td>" . $row['submitname'] . "</td>";
  echo "<td>" . $row['feedback'] . "</td>";
  echo "<td>" . $row['missinginfo'] . "</td>";
  echo "<td>" . $row['TimeStamp'] . "</td>";
  echo "<td><input type='checkbox' name='complete'></td>";
  echo "<td>" . $row['completionstamp'] . "</td>";
  echo "</tr>";
  }
echo "<input type='submit' value='Update'>";
echo "</form>";
echo "</table>";

What happens when I hit submit I send it to this code

$timeissued=$_POST["complete"];
$date = date("m-d-y H:i:s");
mysql_query("INSERT INTO SITES (completed, completetime )
VALUES ('$timeissued' ,'$date')");

I know that I'm not telling it which record I want to add the timestamp or the check box to, so it isn't going to do it, however it creates a new record with a timestamp doesn't save the check box and leaves the rest of the fields blank. I'm sure the answer is simple

2 Answers 2

1

Modify your checkbox so that it is a value=1 attribute.

Change the name of said checkbox to name=complete[$id]

Now, on the PHP side of things, do something along the lines of...

<?php
$complete = $_POST["complete"];
$time = time();
foreach($complete as $key => $value) {
    $query = "UPDATE SITES
              SET completed=1, completedtime='$time'
              WHERE id = '$key'";
    // This query is not in any way secure...
    mysql_query($query);
}

This will loop through ALL of the checkboxes you hit and set the completed field in your database to 1, and the completedtime field to the current time stamp, which is an integer.

Though, you shouldn't be using mysql_* functions on account of them being deprecated. You should look into PDO.

Sign up to request clarification or add additional context in comments.

8 Comments

I can't get this one to update them in the database I updated the following. <td><input type='checkbox' name='complete[$id]' value='1'</td> $complete = $_POST["complete"]; $time = time(); foreach(complete as $key => $value) { $query = "UPDATE SITES SET completed='1', completedtime='$time' WHERE id = '$key'"; // This query is not in any way secure... mysql_query($query); } it puts a time stamp in of 0000-00-00 but not just on one record on all but the one I checked the box of
the time() method outputs an integer... The number of seconds since the UNIX epoch. PHP's date method will let you turn the time stamp into an actual date, which is what you can place in your database.
I just mean, it's not working, it's not updating completed to 1 nor is it creating the time stamp even when I use the date();
Are you getting any sort of error or warning? echo your sql call and place that into PHPMyAdmin or something similar if you have that available. is your table called SITES, or is it called Sites or something? SQL is case sensitive.
its SITES, I checked over everything first to make sure I didn't do anything like that. Everything matches. I get the feeling it has to do with this echo "<td><input type='checkbox' name='complete[$id]' value='1'></td>";
|
0

This is going to be difficult to answer without sounding condescending, so I'll keep it simple:

It's clear you're new to both PHP and database interaction.

It is making a new record because you're telling it to with ("INSERT INTO...") You should be using ("UPDATEsitesSET..."). see reference here: http://www.w3schools.com/php/php_mysql_update.asp

Look into PDO and discontinue all mysql_ functions. see reference: http://php.net/manual/en/book.pdo.php

You can give your checkbox a value of the record ID so the DB knows which to update. Also, you need to name the input with an array:

<input type="checkbox" name="complete[]" value="<?php echo $row['id']; ?>">

That way you can iterate through all set checkboxes on post and update all records checked.

I don't know how to properly explain all of this without launching into a tutorial on how to write PHP and interact with databases. The reference links should be enough to get started.

If you have specific questions feel free to ask. I will update my answer accordingly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.