dudledok
dudledok

Reputation: 2880

How to include php echo within mysql update?

So I am updating my mysql database using php. Below is the end of the UPDATE, and if I have a string instead of echo $row[embedcode]"); it works fine, and this echo sets data on the page just fine retrieving the right value, but within this UPDATE it doesn't work.

...WHERE `embedcode` = echo $row[embedcode]");

I have tried using ". ." around it and adding its own php tag around it but I'm not sure what needs to be done.

Upvotes: 1

Views: 276

Answers (6)

dhavald
dhavald

Reputation: 554

to prevent SQL injection, you can pass that value as a parameter if you are using PDO or MySQLi.

For example,

$stmt = $dbConnection->prepare('...WHERE `embedcode` = :embedcode');
$stmt->execute(array(':embedcode' => $row[embedcode]));

See this for details.

Upvotes: 0

Smile
Smile

Reputation: 2758

Let say for example...

("UPDATE `tblProfile` SET `profilename` = 'abc' WHERE `embedcode` = '".$row['embedcode']."'");

Upvotes: 0

ppseprus
ppseprus

Reputation: 548

" ... WHERE `embedcode=` '" .$row[embedcode]. "';");

Upvotes: 1

span
span

Reputation: 5624

You don't use echo, perhaps it should be:

...WHERE `embedcode=` . $row[embedcode]");

Not that if $row[embedcode] is a string you have to put quotes around it.

Upvotes: 0

Aris
Aris

Reputation: 5057

WHEREembedcode= . $row[embedcode]); will set the value.

There is not need for echo inside the sql statement. echo is used for displaying something from php to the webbrowser.

Upvotes: 0

woz
woz

Reputation: 10994

Just use this:

...WHERE `embedcode` = " . $row[embedcode]);

There is no need for echo.

As a side note, you should probably parameterize or at least sanitize any strings that go into a MySQL query to prevent SQL injection and other bad things.

Upvotes: 6

Related Questions