-1

For some reason, this code is not inserting into my database, It was working a few days ago, I swear. Now, nothing. Does anyone have any ideas as to why it's not working? It's just giving any errors.

$con=mysqli_connect($host,$username,$password,$dbname) or die('Error->' .mysqli_error($con));

function quote2entities($string,$entities_type='number')
{
    $search                     = array("\"","'");
    $replace_by_entities_name   = array(""","'");
    $replace_by_entities_number = array(""","'");
    $do = null;
    if ($entities_type == 'number')
    {
        $do = str_replace($search,$replace_by_entities_number,$string);
    }
    else if ($entities_type == 'name')
    {
        $do = str_replace($search,$replace_by_entities_name,$string);
    }
    else
    {
        $do = addslashes($string);
    }
    return $do;
}
echo mysqli_error($con);

$c = quote2entities($_POST['company']);
$mem = quote2entities($mem);
$cp = quote2entities($cp);
$vcc = quote2entities($vcc);
$addy = quote2entities($addy);
$contact = quote2entities($contact);
$date = quote2entities($date);
$type = quote2entities($type);


echo $c ;
echo $contact;
echo $vcc;
$query = "INSERT INTO project (company) VALUES (".$c.")";
mysqli_query($con, $query);
mysqli_close($con);

2 Answers2

1

Change VALUES (".$c.") to VALUES ('".$c."')

the variable needs to be inclosed inside single quotes.

You also could use single quotes ('$c') or ('.$c.') as other options.

These I already knew, however you can consult: When to use single quotes, double quotes, and backticks in MySQL to see the different combinations.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

This code should never ever be used.
As it makes not a slightest sense: for some reason you are using HTML encoding when dealing with database. Which is obviously wrong. Not to mention that you ought to use prepared statements instead of interpolating values into query.

Please get rid of your current approach and learn the right way here: How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345