user1542036
user1542036

Reputation: 453

Using form submission to update table records in php

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

Upvotes: 0

Views: 582

Answers (2)

Justin Wood
Justin Wood

Reputation: 10061

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.

Upvotes: 1

Kai Qing
Kai Qing

Reputation: 18833

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

Upvotes: 0

Related Questions