Jump to content

Optimize Code. Is This The Optimum Way To Insert Data Into Tables?


Recommended Posts

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

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.