-1

Using a PHP file and MySQLI, data is passed from an Android app to a MySQL database. However, it seems that certain characters entered such as " " and "&" will cause wrong inputs. I have been able to use the Java replace function to fix " "'s but I'm sure there are many special characters available which will cause similar errors. For example: www.example.com?Input1=Test&Input.

I am wondering if there is a fix to prevent MySQLI from interpreting the input in this way.

Also, if it helps this is my PHP code:

<?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost','createyo_james','password','createyo_TestDatabase');

//Output any connection error
if ($mysqli->connect_error) {
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

//values to be inserted in database table
$NewsStory = $_GET["NewsStory"];
$Summary1 = $_GET["Summary1"];
$Summary2 = $_GET["Summary2"];
$Summary3 = $_GET["Summary3"];
$Picture1URL = $_GET["Picture1URL"];
$Picture2URL = $_GET["Picture2URL"];
$Picture3URL = $_GET["Picture3URL"];
$Picture4URL = $_GET["Picture4URL"];
$Picture5URL = $_GET["Picture5URL"];
$Tags = $_GET["Tags"];
$Body = $_GET["Body"];
$Sources = $_GET["Sources"];

var_dump($_GET["NewsStory"]);


$query = "INSERT INTO Articles (NewsStory, Summary1, Summary2, Summary3, Picture1URL, Picture2URL, Picture3URL, Picture4URL, Picture5URL, Tags, Body, Sources) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$statement = $mysqli->prepare($query);
var_dump($query);
//bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
$statement->bind_param('ssssssssssss', $NewsStory, $Summary1, $Summary2, $Summary3, $Picture1URL, $Picture2URL, $Picture3URL, $Picture4URL, $Picture5URL, $Tags, $Body, $Sources);

if($statement->execute()){
    print 'Success! ID of last inserted record is : ' .$statement->insert_id .'<br />'; 
}else{
    die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
$statement->close();

?>
jamesgates1
  • 73
  • 11
  • possible duplicate of [Using MySQLI to insert special characters to be stored in a table](http://stackoverflow.com/questions/24845593/using-mysqli-to-insert-special-characters-to-be-stored-in-a-table) – Funk Forty Niner Jul 20 '14 at 14:19
  • @AlexanderO'Mara It's not really a "dead" link. Only 10k+ members can (still) see it. Plus, it was "world-visible" when I posted it. The OP deleted the question after I posted it. – Funk Forty Niner Jul 20 '14 at 14:27

1 Answers1

0

You have to encode the string in java properly because for example & is a "reserved" character in an url for the next parameter

Her is a good answer about urlencoding with java Java URL encoding of query string parameters

Community
  • 1
  • 1
Fritz
  • 831
  • 7
  • 23