0

Hello Friend I need Help About Mysql. My Database Look Like This
Table name = users

Rows

id = "7" | YourLastIP = ""

MY Query is:

UPDATE users SET `YourLastIP` = `XX.XX.XX.XX` WHERE `id` = `7`

I want to insert but it also wont work for me please guide me

 INSERT INTO Users `YourLastIP` = (`XX.XX.XX.XX`) WHERE `id` = `7`

Please Help me How to Insert A value if table value not known.

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
Yaser Ranjha
  • 107
  • 2
  • 9
  • 1
    So do you want to insert or update? Which one? For the `update` you have to put the value `XX.XX.XX.XX` in `''` like this: `UPDATE users SET YourLastIP = 'XX.XX.XX.XX' WHERE id = 7` – Mahmoud Gamal Apr 04 '13 at 14:02

3 Answers3

1

You are using backticks which means in MySQL that you're meaning a column name, not text. Use single quotes ' instead.

fancyPants
  • 50,732
  • 33
  • 89
  • 96
0

First, the problem is that you have wrap the value with backtick, which is suppose to be single quote since it is a string literal.

UPDATE users SET `YourLastIP` = 'XX.XX.XX.XX' WHERE `id` = '7' 

Second, use INSERT ... ON DUPLICATE KEY UPDATE if you want to UPDATE existing ID or otherwise INSERT if it does not exist.

INSERT INTO users(ID, YourLastIP)
VALUES (7, 'ip_here')
ON DUPLICATE KEY UPDATE YourLastIP = 'ip_here'

As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
0

Make the column NULLABLE in definition

Then,

CREATE TABLE PRO(ID INT, YOURLASTIP VARCHAR(15));
INSERT INTO PRO values(4, "xxx.xxx.xxx.xxx");
Santhosh
  • 1,771
  • 1
  • 15
  • 25