BrettHartel Posted December 29, 2012 Share Posted December 29, 2012 Thank you for taking the time to help me. I am trying to optimize my php code to work for speed. Even though my code doesn't contain a lot of information, I would like to learn properly. Currently, this is the code I am using. $sql="INSERT INTO eMail (User_ID, eMail) VALUES ('$User_ID','$_POST[eMail]')"; if (!mysqli_query($link,$sql)) { die('Error: ' . mysqli_error()); } Is there a better code I could use or is this optimized php already? Thank you, Brett Hartel Quote Link to comment https://forums.phpfreaks.com/topic/272470-optimize-code-is-this-the-optimum-way-to-insert-data-into-tables/ Share on other sites More sharing options...
requinix Posted December 29, 2012 Share Posted December 29, 2012 Optimizing for speed should be the very last thing you ever do in PHP. After the application is written, after it's tested and de-bugged, after you've run out of other improvements and features to do. In most cases you'll squeeze out a millisecond or two, and in the grand scheme of things that's wasted time. Here's a list of things you should do first: * Don't write inline SQL. Move to functions or better OOP * Don't do inline database connections. Move to functions or OOP * Don't put $_POST values, or $_GET or $_COOKIE or anything with input you haven't already validated in PHP, directly into SQL queries. It's called "SQL injection" and it is unforgivable * Don't die() * Don't die() with the MySQL error message Quote Link to comment https://forums.phpfreaks.com/topic/272470-optimize-code-is-this-the-optimum-way-to-insert-data-into-tables/#findComment-1401955 Share on other sites More sharing options...
trq Posted December 29, 2012 Share Posted December 29, 2012 It's a pretty simple query, I wouldn't worry to much about trying to optimise anything. Security wise though, your code sux fat ones. Never let user submitted data be used in a query like that. Without at least some sanitisation and maybe some validation you are leaving your application open to be compromised. Quote Link to comment https://forums.phpfreaks.com/topic/272470-optimize-code-is-this-the-optimum-way-to-insert-data-into-tables/#findComment-1401956 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.