TAS TAS - 4 years ago 89
PHP Question

SQL Insert and Select (Simple)

I believe it have to be simple but I'm totally green in SQL.
I will include pictures of database so it will be easier to imagine.
I use it in my PHP code that is accessed by Flash to enter data into database.

This is my code which doesn't work:

$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password' INSERT INTO users (contactlist) VALUES ('$xmlcontactlist1')";

I want the data from variable $xmlcontaclist1 were entered to 'contactlist' column but to specific User (based on their Name and Password). Somehow when the code was doing something it was creating NEW empty space in database with just contactlist instead of adding it for each user.
Database Screenshot

Answer Source

Try this one:

$sql = "UPDATE users 
           SET contactlist = '$xmlcontactlist1' 
         WHERE username = '$username' 
           AND password = '$password'";

But this is a bad practice. You can get SQL injections with this code. Read this post here to prevent this: How can I prevent SQL-injection in PHP?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download