PHP/MySQL INSERTPHP is a very flexible language. Maybe too flexible. There are a lot of ways to skin a cat. Some ways are a lot better than others, and some have security vulnerabilites. After a day of monitoring web development forums it's quickly become apparent that beginners get very confused about MySQL. In this post I will focus on inserting data into a database. There are a few different ways to insert data into a database. Imagine we are capturing some user data. This is how I would do it: <?php mysql_query("INSERT INTO `table` SET date=NOW(), name='".escape($_POST['name'])."', surname='".escape($_POST['surname'])."', email='".escape($_POST['email'])."', ") or trigger_error("SQL", E_USER_ERROR); ?> Now there are a few things going on here:
SecurityThe escape function is VERY important as it will prevent basic sql injection attacks. A lot of beginners forget this. You should avoid printing error messages to the screen, as this can aid hackers. So try not to use "or die(mysql_error())". Instead use a custom error handler as demonstrated above. 18/05/2009 permalink | Posted in web development | 7 Comments » Leave a reply |
About meAdam Jimenez is a freelance web developer who has been professionally developing websites since 2000.Find me
Projects
Archive |