how to insert html form data into mysql using json
This is simple, however i will not be able to assist you for give or take 10 minutes
Since your not online, im just going to get into this...
Lets assume you know nothing of mysql/html/php as im not sure if you do or do not.
Alright, lets do html first
First thing you are going to want is the form... ``` <form method="post"></form> ``` For security reasons you want to use the method POST, not GET.
Now then your going to need inputs to put in the data.
``` <form method="post"> <input type="The type of input element this is, it defaults to txt, but its recommended to put it in anyway." name="The Value Name that Will Be Sent To The Server" placeholder="Placeholder To Display" required> </form> ``` The required indicates that the field NEEDS to be entered to be submitted.
But how are we going to submit this data now? with a submit button! ``` <form method="post"> <input type="The type of input element this is, it defaults to txt, but its recommended to put it in anyway." name="The Value Name that Will Be Sent To The Server" placeholder="Placeholder To Display" required> <button type="submit" name="Name to be sent to the server">Text In Button</button> </form> ```
``` <form method="post"> <input type="text" name="data" placeholder="data" required> <button type="submit" name="submit-data">submit data</button> </form> ``` Alright, now then... this is the form i will be using to send the data to the php script that will insert it into the database, this script is assuming its inline... meaning this will be a php file, alternatively you can use a action attribute to specify where to send the data to.
Next your going to want the php script, lets get started on that. We will be using the mysqli method. ``` <?php //Connect to the database. $mysqli = mysqli_connect("localhost", "username", "password", "dbname"); ?> ```
Now then, what if we want to know if an error occurs? ``` <?php //Connect to the database. $mysqli = mysqli_connect("localhost", "username", "password", "dbname"); if (mysqli_connect_errno($mysqli)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } ?> ``` mysqli_connect_errno() will allow us to check for any issue, and will result in true if theres an error.
``` <?php //Connect to the database. $mysqli = mysqli_connect("localhost", "username", "password", "dbname"); //Check for any errors connecting to the db, and echo the error if (mysqli_connect_errno($mysqli)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } //Check for data being sent //The isset command basically checks to see if the variable is set... //The $_POST variable is the variable that contains all data sent through the POST method, anything in the [""] part specifys what data you are talking about at the time. if(isset($_POST['submit-data'])) { if (!($stmt = $mysqli->prepare("INSERT INTO table_name(collum_name) VALUES (?);"))) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; } //"s" stands for statement, for each variable being sent, if its a statement you want it to be an s, i for integer, and b for boolean. if (!$stmt->bind_param("s", $_POST['data'])) { echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; } if (!$stmt->execute()) { echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; } else { echo "Insert Success"; } } ?> ``` This however may be to complex, so lets shrink this script down to the absolute essentials ``` <?php $mysqli = mysqli_connect("localhost", "username", "password", "dbname"); if(isset($_POST['submit-data'])) { $stmt = $mysqli->prepare("INSERT INTO table_name(collum_name) VALUES (?)"); $stmt->bind_param("s", $_POST['data']); $stmt->execute(); } ?> ``` And the html. ``` <html> <head></head> <body> <form method="post"> <input type="password" name="pass" placeholder="Your Password" required> <button type="submit" name="btn-signup">Sign Me Up</button> </form> </body> </html> ``` And there you go, sending form data to the server to be inserted into a mysql database.
Silly me, forgot to space out the html... ah oh well..
i know php mysql but not json the process u explained is to insert form data into database using php but u didnt use json i want to insert the data into database through json
anyway thanks for ur help
OH im sorry i misunderstood!
The process is the same, unless ofcs you want to take the form data, convert it to the JSON, then post it to the server?
yes the data should be converted to json then it should be stored in database
Join our real-time social learning platform and learn together with your friends!