1

I have a problem with inserting data text dynamically from one table to another table which can have a single quotes (').

The link here and here cannot be used in my case because the the data is dynamic (from table).

Here's my code in php:

...
//select from master table with some condition inputted by user
$sql = "SELECT prod_name, prod_price FROM master_prod WHERE cat_prod ='$category'";

//get all data and insert to another table
$rs5 = $db->GetAll($sql);
foreach ($rs5 as $row) {
    $rs = $db->Execute("INSERT INTO trans_temp (name, price) VALUES ('$row[prod_name]','$row[prod_price ]') ");
}
...

My problem is, the prod_name data can have single quotes ('), and when it happens, it won't inserted to table trans_temp because the INSERT code have single quotes ('$row[prod_name]').

Ex.: INSERT INTO trans_temp (name, price) VALUES ('ICE BON BON 5'S 80GR (1C=24)','130')

Can someone help me? Thank you for any answer.

1 Answer 1

1

PDO is probably much better solution, but you can also use '' for escaping:

$rs5 = $db->GetAll($sql);
foreach ($rs5 as $row) {
    $t1 = str_replace("'", "''", $row['prod_name']);
    $t2 = str_replace("'", "''", $row['prod_price']);
    $rs = $db->Execute("INSERT INTO trans_temp (name, price) VALUES ('$t1','$t2') ");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.