How To Test Php File For Getting Form Data
How to Insert Data From a Form in PHP to a MySQL Database
Past Sue Smith
i Hemera Technologies/AbleStock.com/Getty Images
Inserting data into MySQL from a grade on a website using PHP is something that developers oft do. The chore is not complicated, so even if yous exercise not have advanced programming skills y'all should be able to carry information technology out with no trouble. The main technologies involved are HTML, PHP and SQL. To implement an SQL insertion on a database from your PHP scripts, you demand to observe a few simple steps. You will utilize two files, 1 for the HTML class and another to process the data.
Stride 1
Create an HTML class within your page as follows:
This is a simplistic example form to demonstrate the principle of capturing and inserting data. Save your folio with either ".html" or ".php" extension.
Step ii
Create a PHP script to process your grade data. Using the post-obit outline, create a file and save it "insert_data.php" to match the value in your HTML form tag "action" attribute:
//capture the passed data
$inserted_text = $_POST["formtext"];
?>
When the user presses the submit button on the HTML grade, the data is captured from any fields in the form and passed to the PHP "insert_data" script in the POST variable. You can therefore admission the inserted data from within the PHP script.
Step 3
Make a connexion to the MySQL database and insert the data. The detail of your SQL will depend on your ain database, but the following outline illustrates the technique:
//connect
mysql_connect("localhost", "db_user", "db_pass");
mysql_select_db("db_name");
//insert
$insert_query = "INSERT INTO tablename (column1) VALUES ('".$inserted_text."')";
$insertion_result = mysql_query($insert_query);
This PHP lawmaking takes the captured data passed from the form page and inserts it into a MySQL database as part of an SQL insert statement. Alter the script to friction match the connexion details and structure of your ain database.
Step 4
Give the user feedback following their data insertion. Using the post-obit outline, output HTML to permit the user know whether their data has been processed:
//check whether the information insertion was successful
if(!$insertion_result)
echo "
Pitiful! Something went wrong.
";
else
echo "
Thanks! Your form has been processed.
";
You can include within your "insert_data" script whatever other HTML elements you similar. Call back to include a valid HTML folio structure so that the result will be presented reliably in the user'southward browser.
Stride v
Upload your HTML page and PHP scripts to your Web server. Browse to the folio with the form on it and exam information technology. Bank check that it carries out the insertion on your database past logging into information technology and making sure the inserted content is being added to the appropriate tabular array. Also check that the PHP script presents the feedback to the user when the insertion has been carried out. If the insertion does non function correctly, check your syntax and the values you are using for connecting to the database.
References
Tips
- If your from has multiple elements in it, yous may want to validate it using a JavaScript role. Many of these are available for free download.
Warnings
- HTML forms can sometimes be attacked by spam, which can fill up your database with nonsense values. Using a Captcha is a common defence force.
Writer Bio
Sue Smith started writing in 2000. She has produced tutorials for companies including Apex Computer Training Software and articles on computing topics for various websites. Smith has a Master of Arts in English linguistic communication and literature, every bit well as a Primary of Science in it, both from the University of Glasgow.
How To Test Php File For Getting Form Data,
Source: https://itstillworks.com/insert-form-php-mysql-database-8194224.html
Posted by: hadlockemenceapery2002.blogspot.com
0 Response to "How To Test Php File For Getting Form Data"
Post a Comment