When is use the following code without mysql_real_escape_string, works fine. I simply trying to grab a text string that may have apost. from an input form and format it to put in mysql table.
<?php
$filenamee = $_FILES["file"]["name"];
$filename =strval($filenamee);
echo "file name is".$filename;
$con=mysqli_connect("localhost","blasbott_admin","lubu1973","blasbott_upload");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$companyName = mysql_real_escape_string($_POST['companyName']);
// $companyName = mysql_real_escape_string($companyNamee);
//$companyName = mysql_real_escape_string($companyNamee);
$sql="INSERT INTO ads (companyName, webSite, picture)
VALUES ('$companyName','$_POST[webSite]','$filename')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo"<br>";
echo "1 record added";
mysqli_close($con);
?>
No, you shouldn't mix mysql and mysqli.
Use here instead of mysql_real_escape_string($var)
:
$con->real_escape_string($var);
You're at risk of MySQL injections. Never insert data directly to a database without some sort of projection first. It's a major security risk. Also use mysqli_real_escape_string instead, and note that your $_POST[webSite] is unprotected.
Also, your error means that your database details are not correct.
You must connect with database first before using Warning: mysql_real_escape_string()
A working code example that is a solution to this problem is available here:
http://www.w3schools.com/php/func_mysqli_real_escape_string.asp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With