user656925
user656925

Reputation:

How do I insert a value to a specefic row?

I know how to do an insert

database::query("INSERT INTO cr VALUES ('$flname', '$email', '$pass', '$ext')");

and how to find a row

database::query("SELECT * FROM cr WHERE email='$email'")

but how do I do both, i.e. select a specefic row and then insert/update that value.

Upvotes: 0

Views: 66

Answers (4)

wonk0
wonk0

Reputation: 13972

Inserting data into a specific row requires that this row exists; in that case you perform an UPDATE which usually has a WHERE condition just like a SELECT has.

To avoid SQL injection you want to use mysql_real_escape_string() on your variables; for numeric data it is also a good idea to cast to the desired type. Another option would be using prepared statements.

Upvotes: 1

Senad Meškin
Senad Meškin

Reputation: 13756

database::query("update cr set FileName='$flname', Email='$email', Pass='$pass', Ext='$ext' where email='$email');

Upvotes: 1

Jacek Kaniuk
Jacek Kaniuk

Reputation: 5229

if you only want to change field in existing row:

UPDATE cr SET col = 'val' WHERE id = x

or if you wan't to insert or update:

REPLACE cr VALUES ($id, '$flname', '$email', '$pass', '$ext')

in the latter you have to put unique key on id (or other column)

there is also

INSERT INTO cr (...) ON DUPLICATE KEY UPDATE col = 'val'

Upvotes: 0

brian_d
brian_d

Reputation: 11385

What you want is to UPDATE an existing row. To avoid injection, you escape variables with mysql_real_escape_string and/or by preparing and binding parameters.

Upvotes: 1

Related Questions