views:

35

answers:

2

I've been building a website on a subdomain of my companies hosting package. It's got PHP MySQL operations happening in the background, i.e. INSERTs, SELECTs etc.

They are the most basic of basic SQL statements and they all work fine.

However, I recently copied EVERYTHING from my subdomain and uploaded it all to my clients own hosting package run by 1&1 and now, my INSERT statements do not work. SELECTs, DELETE statements all work fine, but not my INSERTS.

I think it's one of two problems, either it's a permissions error on the new database or I have some glaring error in my code that my last database didn't seem to mind so much.

Here's my insert code:

<?php
include 'data.php';

$b_name = mysql_real_escape_string($_POST['businessName']);
$b_town = mysql_real_escape_string($_POST['placeName']);
$b_code = mysql_real_escape_string($_POST['postCode']);
$latlng = mysql_real_escape_string($_POST['latLong']);

mysql_connect($host, $user, $pass) or die ("Wrong Information");

mysql_select_db($db) or die("Wrong Database");

$newloc = preg_replace("/^.*\(([^)]*)\).*$/", '$1', $latlng);

$result = mysql_query("INSERT INTO reseller_addresses VALUES (NULL ,  '$b_name',  '$b_code',  '$b_town',  '$newloc');") or die ("Broken Query");

echo "<script>setTimeout(\"self.parent.location.reload(true);\", 1000);</script><font color=\"#fbf7d7\">$businessName Successfully Added to the Database</font>";

mysql_close();
?>

It's concerning adding a geographic location into a database from a google map/form setup.

Unfortunately I can't authorise a live preview because the administration area where the trouble is happening is secure.

Thanks in advance for any help.

EDIT: I've passed the same files back and forth my web server and my clients and they all work on mine, but not my clients.

EDIT: I've explained the actual problem badly here, sorry. Basically, a new record is inserted into the database, but it's empty.

A print_r($_POST); reveals:

Array ( [businessName] => test [placeName] => test [postCode] => test [latLong] => (51.152495, -1.440411) [Submit] => Submit Place )

Which IS everything I've submitted

A: 

Please replace the line that says

$result = mysql_query("INSERT INTO ...") or die ("Broken Query");

with the following:

$query = "INSERT INTO ...";
echo $query;
$result = mysql_query($query) or die (mysql_error());
$warnings = mysql_query("SHOW WARNINGS");
if ( mysql_num_rows($warnings) ) {
    while ( $row = mysql_fetch_assoc($warnings) ) {
        echo "\nMySQL Warning: ".$row['Message'];
    }
} else {
    echo "\nNo warnings";
}

and tell us what is output.

Also, please add error_reporting(E_ALL & ~E_STRICT); immediately after include 'data.php'; , and see if you get any error messages.

Please also give us the output of SHOW CREATE TABLE reseller_addresses.

Hammerite
Thanks for your answer but since I wrote the question I've discovered that the problem is not with my query but with my mysql_real_escape_string. Removing this makes the query work fine, however, on the flip side, how am I going to prevent attacks without this?
Daniel Hanly
Are you certain that the problem is with `mysql_real_escape_string()`? Do the following: `$b_name = mysql_real_escape_string($_POST['businessName']); echo "Before: ".$_POST['businessName']."\nAfter: ".$b_name;` and tell us what is output.
Hammerite
I just changed it to addslashes() and it worked
Daniel Hanly
Daniel Hanly
I have no experience of using the `mysql_` functions, as I've only ever used `mysqli_`. All I can suggest is changing to `mysqli_` and seeing if that fixes it. (Others may suggest using PDO instead, and that's good too, but may be considerably more refactoring work than just changing to `mysqli`, depending on the size and complexity of your codebase.)
Hammerite
A: 

I did a series of tests and the problem lay with the mysql_real_escape_string line. I removed this and it worked. I still need some injection protection however, so I added in addslashes.

I have no idea why it acted in this way and I hope my fix wasn't temporary.

Daniel Hanly