2

I am trying to insert values into a database table, a row is inserted but blank no values are inserted. Only the order_id which is the primary key with auto increment increase.

php code:

<?php
        $user_get = mysql_query("SELECT * FROM users"); 
        while($row_user = mysql_fetch_assoc($user_get)){
            if($row_user['username'] == $_SESSION['username']){
                $row_user['first_name'] = $res1;
                $row_user['last_name'] = $res2;

                $store_order ="INSERT INTO oko (user, product) VALUES ('$res1', '$res2')";
                mysql_query($store_order);
            }
        }
?>
4
  • 2
    Are you sure $res1 and $res2 have a value? Print $store_order before you execute the query; Commented Mar 19, 2013 at 21:51
  • 2
    LOL seems you'll be having a hard time to pick the right answer from the three answers provided :D Commented Mar 19, 2013 at 21:54
  • :D i know crazy choice :D Commented Mar 19, 2013 at 22:01
  • like my question as well ppl :P Commented Mar 19, 2013 at 22:05

2 Answers 2

5

Your assignments are backwards. I think you meant to:

$res1 = $row_user['first_name'];
$res2 = $row_user['last_name'];
Sign up to request clarification or add additional context in comments.

Comments

3

Don't you mean:

$res1 = $row_user['first_name'];
$res2 = $row_user['last_name'];

You could also update the SELECT to have a WHERE clause that checks $_SESSION['username'].

You could also just do an INSERT/SELECT:

INSERT INTO oko (user, product)
SELECT
    first_name, last_name
FROM
    users
WHERE
    username = '$_SESSION["username"]'

Your code is vulnerable to injection. You should use properly parameterized queries with PDO/mysqli

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.