Girish Oemrawsingh Girish Oemrawsingh - 3 months ago 8
MySQL Question

How can I insert a '+' symbol into a MySQL database with PHP?

This is my PHP script

<?php

require_once('db.php');

$data = $_GET['data'];
$data = mysqli_real_escape_string($conn, $data);

$otherdata = $_GET['otherdata'];
$otherdata = mysqli_real_escape_string($conn, $otherdata);

$query = "INSERT INTO dataone (data, otherdata) VALUES (?, ?)";

$stmt = mysqli_prepare($conn, $query);

mysqli_stmt_bind_param($stmt, "ss", $data, $otherdata);
mysqli_stmt_execute($stmt);

$affectted_rows = mysqli_stmt_affected_rows($stmt);

if($affectted_rows == 1) {

echo 'data submitted';

mysqli_stmt_close($stmt);

mysqli_close($conn);

} else {
echo 'Error occurred<br/>';
echo mysqli_error();
mysqli_stmt_close($stmt);
mysqli_close($conn);
}

?>


It works fine, but when I add a + symbol in the url, it doesn't get inserted as I wanted it to.

This is what I tried: http://example.com/data.php?data=10+2&otherdata=test

But that just inserts data like 102 instead of 10+2

How can I include the '+' symbol in my database?

Answer

You must use urlencode()

 $otherdata = urlencode ($_GET['otherdata']);
Comments