Here is a snippet of my code that I worked on, although it is published in the database every time you visit a page. I tried adding a "unique key" using rand () in the database. But the code still captures the "rand () on the page" and the post without the "Submit" from the form. Anyway, this is how I insert the data into Mysql. My database creates a timestamp of time and day () and order by "id".
// Select from database working $statement = $database->prepare('SELECT * FROM pressroom ORDER BY id DESC LIMIT 0 , 30' ); $statement->execute(); $count = $statement->rowCount(); if( $count > 0 ) { $R = $statement->fetchAll( PDO::FETCH_ASSOC ); for( $x = 0; $x < count($R); $x++ ) { echo "<td><br>"; echo "<center><b>" . $R[ $x ]['name'] . "</th>"; echo ": <left></b>" . $R[ $x ]['comment'] . "</th>"; echo "<center>" . $R[ $x ]['date'] . "<hr></td>"; echo "</tr>"; } } // Make sure that the 'values' are in the same order or as your table $query = "INSERT INTO pressroom(first_name, last_name, Password ) VALUES ('table1','table2','table3')"; //connection prepare try { $database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $results = $database->query($query); //Prints results that are added to table print_r($results); } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); }
source share