Looking for a good tutorial on how to update a mysql database using a php form?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Updating data can be pretty simple. Let’s start with a form, for starters:
This form will send the data over to our
submit.phpscript where we can handle it, and pass it into our database. Since our form method is “post,” all of our values will be sent through thePOSTsuper array in PHP (this is not the case if you are using file uploaders). So within oursubmit.phppage, we can print the ID and Value values like this:You’ll want to be careful about passing user-submitted values directly into your queries, so it’s nice to clean up the data using a function like
mysql_real_escape_string():The next thing we’ll want to do is place these in a query:
This is a good time not state that I don’t encourage you to use this example code in a live environment. You’ll want to look up sql-injections, and how to avoid them. The code I’m providing here is merely an example. After our values are entered, the query that will be ran actually looks like this:
Now, in order to run this, we need to be connected to a database.
All we’re doing here is storing our mysql-user-account credentials in arrays, and passing them into a connect-function. This code should be pretty self-explanatory, but let me know if it’s at all unclear.
Once you’ve got that, you’re ready to run your query. Remember that we stored it in an array called
$sql:That’s it. You did it! The data, assuming nothing went wrong, is now updated in your database. There are numerous ways you can increase the information provided back to the user via this script. Also worth noting is that you’ll want to sanitize your data before even allowing the script to run – if it’s not acceptable data (somebody trying to inject their own queries) you’ll want to spit it back.
Check out the MySQL Functions in the PHP Documentation for more goodies, and be sure to return here when you have more specific questions!