lonesoac0 Posted October 2, 2015 Share Posted October 2, 2015 Hello all, I am interested in creating a very simple form that onclick it adds a new record to my table. Can I accomplish this task with the basic code that I have on the line with "Button Type" ? I am totally open to different methods too. <?php $servername = "localhost"; $username = "bleh"; $password = "password"; $dbname = "logger"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // set the PDO error mode to exception. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // sql to select last record inserted. foreach($conn->query('select status from comins_and_goins order by id DESC limit 1') as $row) { $var = $row['status']; } if ($var == 1) { echo "You are going to work or somewhere else for a while."; echo '<button type="button" onclick="MySQL query that inserts record.">Click Me</button>'; } else{ echo "You are back home! I missed you!"; } } catch (PDOExecption $e) { echo $sql . "<br>" . $e->getMessage(); } $conn = null; ?> On a side note, I also feel that I need to acknowledge the foreach statement is a little redundant for the MySQL syntax that I am executing on the same line. I figured, it works and I just want my new toy operational ASAP. Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted October 2, 2015 Solution Share Posted October 2, 2015 Javascript cannot interact directly with PHP code or with your database. Use a form. <form action="" method="post"> <button type="submit" name="comehome">Click Me</button> </form>then if (isset($_POST["comehome"])) { // insert the record } // continue on as normalIt's not onclick because that requires AJAX and you said you wanted this thing done quickly. Which is a bad mentality for a programmer to have but whatever. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.