-6

I have this following PHP that it will insert data from excel to database, but it won't update it.

Please guide me how to make it done? that If I change something, then it will update into database table rows too.

foreach($dataArr as $val){
$query = $db->query("INSERT INTO employees SET fname = '" . $db->escape($val['1']) . "', lname = '" . $db->escape($val['2']) . "', email = '" . $db->escape($val['3']) . "', phone = '" . $db->escape($val['4']) . "', company = '" . $db->escape($val['5']) . "'");
}

Thanks in advance

5
  • 5
    Please, google database update. It is not rocket science. Commented Dec 19, 2016 at 9:53
  • I did, I couldn't make it. I'm new in PHP. I highly appreciate if you help me thanks Commented Dec 19, 2016 at 9:54
  • 4
    Refer to any tutorial on PHP/MySQL. You currently have an INSERT statement, you're looking for an UPDATE statement. Commented Dec 19, 2016 at 9:55
  • 1
    Use UPDATE keyword Commented Dec 19, 2016 at 10:02
  • Possible duplicate of inserting data into mysql database using php Commented Dec 19, 2016 at 10:17

2 Answers 2

1

Replace

INSERT INTO employees 

with

UPDATE employees 

Update Syntax

UPDATE [LOW_PRIORITY] [IGNORE] table_references
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
Sign up to request clarification or add additional context in comments.

Comments

0

Not insert use update

$query = $db->query("Update employees SET fname = '" . $db->escape($val['1']) . "', lname = '" . $db->escape($val['2']) . "', email = '" . $db->escape($val['3']) . "', phone = '" . $db->escape($val['4']) . "', company = '" . $db->escape($val['5']) . "'" [WHERE where_condition]);
}

3 Comments

I got this error Parse error: syntax error, unexpected 'where' (T_STRING), expecting ',' or ')' for this line: foreach($dataArr as $val){
u should change [WHERE where_condition] like where id=$id
do you have id?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.