Saving data from form to Database Table
-
I am looking to create an online job filing system for my company and to do so am trying to create this as a sort of intranet system with wordpress.
I have an inital for that I enter the job details in to and a tab next to this form which allows me to view all jobs which have been entered.
In order for the details to be saved and not lost after refresh I need the form input to be saved to the mySQL database table which I have created.
The code I have used:
The form:
<div class="addjob tab-pane show active" id="home" role="tabpanel" aria-labelledby="home-tab"> <h2>Add Job</h2> <form id="dataForm" name="dataform" method="POST" action="wp-content/themes/mytheme/database.php"> <span> <label>Job Date:</label> <input class="add-date" type="date" placeholder="Placeholder Text" name="adddate"> </span> <span> <label>Client Name:</label> <select name="addclient" class="limitedNumbChosen add-client" multiple="false"> <option value="1">ClientA</option> <option value="2">ClientB</option> </select> <div id="add-company" class="fas fa-plus"> Add New Client</div> </span> <span> <label>Contact Name:</label> <input class="add-contact" type="text" placeholder="John Doe"name="addcontact"> </span> <span> <label>From:</label> <input id="add_from" class="add-from" type="text" placeholder="Postcode from" name="addfrom"> </span> <span> <label>To:</label> <input class="add-to" type="text" placeholder="Postcode to" name="addto"> </span> <span> <label>Who Done Job:</label> <select name="subbie_name" class="limitedNumbSelect2 add-driver" multiple="true"> <option value="1">Dave</option> </select> <div id="add-subbie" class="fas fa-plus"> Add Subbie </div> </span> <span> <label>Income (£):</label> <input class="add-income" type="number" placeholder="£120" name="addincome"> </span> <span> <label>Driver Payment (£):</label> <input class="add-payment" type="number" placeholder="£100" name="addpayment"> </span> <span> <button id="submit" type="submit" value="submit">Submit</button> </span> </form> </div>
The PHP:
<?php //Register variables $adddate = $_POST['adddate'] $addcontact = $_POST['addcontact'] $addfrom = $_POST['addfrom'] $addto = $_POST['addto'] $addincome = $_POST['addincome'] $addpayment = $_POST['adddate'] $addsubbie = $_POST['addsubbie'] $addclient = $POST['addlient'] //connect with Database $host_name = 'xxx.hosting-data.io'; $database = 'xxx'; $user_name = 'xxx'; $password = 'xxx'; $connect = mysql_connect($host_name, $user_name, $password, $database); //Send to database if (mysql_errno()) { die('<p>Failed to connect to MySQL: '.mysql_error().'</p>'); } else { $wpdb = $connect->prepare("insert into add_job(adddate, addcontact, addfrom, addto, addincome, addpayment adddriver addcompany) values(?, ?, ?, ?, ?, ?, ?, ?,)"); $wpdb->bind_param("ssssiiss", $adddate, $addcontact, $addfrom, $addto, $addincome, $addpayment, $addsubbie, $addcoclient) ; $wpdb->execute(); echo "Job Submited" $wpdb->close(); $connection->close(); }
I have never tried to send info to a database. Any help would be much appreciated
- The topic ‘Saving data from form to Database Table’ is closed to new replies.