Ask your own question, for FREE!
Computer Science 11 Online
Extrinix:

(PHP and MySQL) How do I feed data from a form, to a PHP file, then back to MySQL?

Extrinix:

@ultrilliam

Timmyspu:

@smokeybrown

Extrinix:

Oh, and how do I call it back through JavaScript?

Ultrilliam:

Well, first things first, you need to submit the form to a PHP file, this can be done by setting the URL, or instead handling form input with JavaScript, this is a bit of a loaded question so I'm not going to go into that with detail in my initial post here. Next part is the PHP part, first things first is you need to have a MySQL server, and have the MySQLi extension with PHP, usually by default this is installed, so moving on from that. First make the connection to the server: ```php $mysqli = new mysqli("SERVER", "USER", "PASSWORD", "DATABASE"); ``` Fill these in with credentials, usually the server is on the local server so you use `localhost` as your server. Moving on, next you would want to have a table created in the DB, let's say your table is just simply 2 rows, `username` and `password` with a table name of `users`, lets insert something into that using a prepared statement, assuming $username, and $password were defined earlier. You can get information from GET and POST by using `$_GET` and `$_POST` ```php $statement = $mysqli->prepare('INSERT INTO users(username, password) VALUES (?,?)'); $statement->bind_param("ss", $username, $password); $statement->execute(); ``` To access the information, you would then call either another PHP script, or the same PHP script that looks for a specific $_GET or $_POST parameter indicating what the intended action is, and then get the information, using the `fetch_assoc` command. ```php $result = $mysqli->query("SELECT * FROM users WHERE username = ".$username); $row = $result->fetch_assoc(); ``` and then you would be able to access that information with `$row["row_name"]`, though strictly speaking, you should sanitize your inputs which the above example does not, otherwise you leave yourself open to MySQL Injection attacks. You can read more about the documentation of the above PHP here: https://www.php.net/manual/en/book.mysqli.php https://www.php.net/manual/en/mysqli.quickstart.connections.php https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php https://www.php.net/manual/en/reserved.variables.get.php https://www.php.net/manual/en/reserved.variables.post.php

Extrinix:

Thank you @ultrilliam

Extrinix:

Also I can serialize and post the form data @ultrilliam , should have mentioned that

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!