1

I am building some new script for add long text with whitespace to MySQL. I need to send array elements using encode from JSON to MySQL table. encode also working fine. My question is when I tried to insert data MYSQL is reject data.

$day_details['description']=escape_string($_POST['description']);
$data_[]=$day_details;
$mysql_desc=json_encode($data_);

$sql="INSERT INTO 'table' 'description' VALUES ('".$mysql_desc."')";
if($conn->query($sql)){

}

syntax error

1 Answers1

4

You have a few issues:

  1. Table and column names should not be enclosed in single quotes, but backticks if required (see this Q&A).
  2. The column name should be in parentheses.
  3. Finally, you need to escape the data after converting to JSON. But better yet, use a prepared query, then you don't need to escape the data at all:
    $day_details['description']=$_POST['description'];
    $data_[]=$day_details;
    $mysql_desc=json_encode($data_);

    $sql="INSERT INTO `table` (`description`) VALUES (?)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('s', $mysql_desc);
    if($stmt->execute()){
        // do something
    }
Nick
  • 138,499
  • 22
  • 57
  • 95