Reputation: 1
How to insert in database a link with a html special chars like &
(ampersand) symbol ? When I inserted it, the output is cut out..
For example: this link
https://www.youtube.com/watch?v=TCnxD9V9lGE&index=24&list=PLNVssP8zTtVk5asCmP703gEvR_lG-Ur-S
and the output after I've inserted it:
The &
(ampersand) symbol and the rest is cut out.
This is my code:
$data = "LINK with & (ampersand)";
$data = mysqli_real_escape_string($connect_db, $data);
Upvotes: 0
Views: 1161
Reputation: 403
You should consider using prepared queries. This will prevent most problems with special characters being inserted into databases. PDO is the best way (imo) to use prepared statements. This is roughly how your code would look using PDO:
$connect_db = new PDO('mysql:dbname=yourdb;host=127.0.0.1;charset=utf8', 'root', '');
$url = //your url with ampersands
$insertquery = $connect_db->prepare("INSERT INTO table1 (urlstring, ...) VALUES (:url,...)");
$insertquery->bindParam(':url',$url);
$insertquery->execute();
Getting familiar with prepared queries will also help prevent SQL injection. Once you're a bit more confident with PHP, take a look at this guide for PDO: https://phpdelusions.net/pdo
Upvotes: 2