Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (shaik0124):

how to insert html form data into mysql using json

jaynator495 (jaynator495):

This is simple, however i will not be able to assist you for give or take 10 minutes

jaynator495 (jaynator495):

Since your not online, im just going to get into this...

jaynator495 (jaynator495):

Lets assume you know nothing of mysql/html/php as im not sure if you do or do not.

jaynator495 (jaynator495):

Alright, lets do html first

jaynator495 (jaynator495):

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.

jaynator495 (jaynator495):

Now then your going to need inputs to put in the data.

jaynator495 (jaynator495):

``` <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.

jaynator495 (jaynator495):

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> ```

jaynator495 (jaynator495):

``` <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.

jaynator495 (jaynator495):

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"); ?> ```

jaynator495 (jaynator495):

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.

jaynator495 (jaynator495):

``` <?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.

jaynator495 (jaynator495):

Silly me, forgot to space out the html... ah oh well..

OpenStudy (shaik0124):

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

OpenStudy (shaik0124):

anyway thanks for ur help

jaynator495 (jaynator495):

OH im sorry i misunderstood!

jaynator495 (jaynator495):

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?

OpenStudy (shaik0124):

yes the data should be converted to json then it should be stored in database

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!