Jump to content

Recommended Posts

Thanks guys.

 

So i looked up the  usage of mysql_real_escape_string(), and changed my code, as below, but it has given me errors now.

 

&_POST['customername'] = mysql_real_escape_string($_POST['customername']);
&_POST['town'] = mysql_real_escape_string($_POST'[town']);
&_POST['testimonial'] = mysql_real_escape_string($_POST['testimonial']);

$sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images)
VALUES
('$_POST[customername]','$_POST[town]','$_POST[testimonial]','$_POST[sort_order]','$imgname')";

Now change dto

 


function check_input($value)
{
// Stripslashes
if (get_magic_quotes_gpc())
  {
  $value = stripslashes($value);
  }
// Quote if not a number
if (!is_numeric($value))
  {
  $value = "'" . mysql_real_escape_string($value) . "'";
  }
return $value;
}

$customername = check_input($_POST['customername']);
$town = check_input($_POST['town']);
$testimonial = check_input($_POST['testimonial']);

&_POST['customername'] = mysql_real_escape_string($customername);
&_POST['town'] = mysql_real_escape_string($town);
&_POST['testimonial'] = mysql_real_escape_string($testimonial);

$sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images)
VALUES
('$_POST[customername]','$_POST[town]','$_POST[testimonial]','$_POST[sort_order]','$imgname')";

You're not properly quoting your array indexes.

 

You also added that fancy new function and you aren't using it.

 

-Dan

Thanks Dan, but my script calls the function check_input when the info gets posted in the customer name, town and testimonial fields....doens't it?

What do you mean about properly quoting the arrays? Should it be " instead of '?

Ok, got rid of some code and now have

$customername = check_input($_POST['customername']);
$town = check_input($_POST['town']);
$testimonial = check_input($_POST['testimonial']);

$sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images)
VALUES
('$_POST[customername]','$_POST[town]','$_POST[testimonial]','$_POST[sort_order]','$imgname')";

}

but still have the error as stated before

// ASSUMES DB CONNECTION ALREADY ESTABLISHED . . .

function check_input($value) {
// Stripslashes
if (get_magic_quotes_gpc()) {
	$value = stripslashes($value);
}
// Quote if not a number
if (!is_numeric($value)) {
	$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}

$_POST = array_map('check_input', $_POST);

$sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images)
VALUES
('$_POST[customername]','$_POST[town]','$_POST[testimonial]','$_POST[sort_order]','$imgname')";

Many Thanks for that mod!

 

1: still got the errors

function check_input($value)
{
// Stripslashes
if (get_magic_quotes_gpc())
  {
  $value = stripslashes($value);
  }
// Quote if not a number
if (!is_numeric($value))
  {
  $value = "'" . mysql_real_escape_string($value) . "'";
  }
return $value;
}

$_POST = array_map('check_input', $_POST);

$sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images)
VALUES
('$_POST[customername]','$_POST[town]','$_POST[testimonial]','$_POST[sort_order]','$imgname')";

 

2. How do you get to know about all of these arrays?

I look at php.net and others but i can never get anything to stick in my head.

I can learn by being shown, but never out of a 'book' as such

Exactly what errors are they? Paste 'em in.

Sorry mate.....still the same one caused by the '

 

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Steve'',''Brum'',''fsdhewty\'f'','12','uploaded_images/transparent.gif')' at line 3

 

EDIT: Sorry, not the ' causing errors now, but not sure what is

Debugging tips:

 

1)  Turn on error reporting.  You had & instead of $, error reporting would have told you this.

 

2)  Print your variables.  This means printing $sql and looking at it.  See where the problem is.

 

-Dan

I see what's going on here now. The function encloses string values in single quotes before returning them, so they're getting double quoted.

 

Rewrite the query string as:

$sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images)
VALUES
({$_POST['customername']}, {$_POST['town']'}, {$_POST['testimonial']}, {$_POST['sort_order']}, $imgname)";

Thanks for help Pika.

 

Now getting error saying..... Error: Unknown column 'uploaded_images' in 'field list'

 

function check_input($value)
{
// Stripslashes
if (get_magic_quotes_gpc())
  {
  $value = stripslashes($value);
  }
// Quote if not a number
if (!is_numeric($value))
  {
  $value = "'" . mysql_real_escape_string($value) . "'";
  }
return $value;
}

$_POST = array_map('check_input', $_POST);

$sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images)
VALUES
({$_POST['customername']}, {$_POST['town']}, {$_POST['testimonial']}, {$_POST['sort_order']}, $imgname)";

}
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "<p align=center><b>1 testimonial added</b></p>";

mysql_close($con);

 

 

Change this line:  die('Error: ' . mysql_error());

 

To this:  die("<br>Query: $sql<br>Error: " . mysql_error() . '<br>');

 

And see what the query string looks like now.

 

EDIT: Fixed typo.

Cheers mate, All error messages gone and it says 1 record added, but there is no record added

 

No, sorry mate,.....working fine.

my mistake, i had to change the max records from 10, to 100, as the record i put in was 11 lol

 

Many, many thanks mate.

 

U have slayed my oncoming headache lol

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.