I am working on a php form using POST and use dynamic HTML tables. I truncate the DB table every time before inserting the data in to the table which seems fine.
Select query when the page loads (to update UI part)
$query = 'SELECT * FROM TABLENAME';
$result = mysqli_query($dbConnection, $query);
$rows = array();
if(!$result)
{
//log error
}
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
print_r( $row);
$rows[] = $row;
}
}
Insert part
if(isset($_POST['u']))
{
foreach($_POST['u'] as $key => $value)
{
$ky = $_POST['x'][$key];
$query = "INSERT INTO TABLENAME (ID,KY) VALUES ($value, '$ky')";
$result = mysqli_query($dbConnection, $query);
}
}
However, upon posting, when using select query, it seems to not get the latest records inserted, I made sure to check the records are inserted properly in PhpMyadmin.
When I reload the page, it works fine. Only thing is it's not getting the updated records when it's POSTed, but works the subsequent times.
I thought it could be due to connection object and tried to use different connections objects with no luck.
INFO: Not sure if it has any impact, the database is a wordpress and I created new table in this database for this.
Thanks in advance...
The problem is that you need to do a SELECT
query after your INSERT
query in order to get the full data set.
You might consider posting to a separate page, and then redirect the user back to the original page after the INSERT
. This will cause the SELECT
to run again, since the page reloads.