-1
$SQL="SELECT first_name FROM people WHERE fname = '$fname' INSERT INTO (first_name) VALUES (fname)";

Anything wrong with this? Trying to insert a value from a user defined variable into a mysql table

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\wamp\www\Pxxxx\process.php on line 44

This is the error

$fname is a user defined variable first_name is the column I'm trying to insert it into and it's in a table called people

7
  • Do a transaction if you want 2 statments. Commented Dec 17, 2013 at 19:49
  • Separate the two statements with a ; Commented Dec 17, 2013 at 19:49
  • 3
    What are you trying to insert? Are you trying to insert the result of the SELECT? Do you mean: INSERT INTO (first_name) SELECT first_name FROM people WHERE fname = '$fname'? Commented Dec 17, 2013 at 19:50
  • 3
    @Floris: For security reasons, you can't run 2 statements in one mysql_query call (or whatever he's using). Commented Dec 17, 2013 at 19:50
  • Never actually knew that! @OP: it's not quite clear what you're trying to accomplish here. Commented Dec 17, 2013 at 19:54

2 Answers 2

3

You have the order inverted. It seems like you are looking for INSERT .. SELECT syntax (see MySQL documentation here: http://dev.mysql.com/doc/refman/5.6/en/insert-select.html)

INSERT INTO target_table (first_name)
SELECT fname
FROM people
WHERE fname = ?

It was unclear from your example what the name of the table you were trying to insert data into is, so I just listed it as target_table here.

Sign up to request clarification or add additional context in comments.

Comments

1

Your SQL statement has to be reordered like this:

"INSERT  INTO people (fname) SELECT '$fname' FROM dual;"

This will select the value of $fname from the pseudo table "dual" and insert the value into "people".

Maybe this is more suitable:

"INSERT INTO people (fname) VALUES ('$fname');"

This snippet show you a simple insert statement.

Note: Please have a look for SQL Injection at Wikipedia. The code you are writing is open for these kinds of attacks. If you are writing PHP code, have a look for Prepared Statements and mysqli to prevent these attacks.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.