Php - sql insert problem

Let me start off by stating that some of my sql inserts are not working. Their are some that work just fine. Inserts are the only query’s I am have problems with. My select, delete and update query’s work fine. I am also using the free hosting service provided by 000WebHosting. I’m hoping it’s just something I’ve over looked.

I have been editing the bellow code for over 3 hrs trying to find out why the query won’t insert into my database. I have made sure that all DB field names are spelled correctly. I have echoed all the variables used in the insert query to the screen and used the sql feature in PHPMyAdmin to make sure I don’t have any syntax errors in my query.

After all that, nothing is inserted into the DB.

Code:
$content = “Body”;
$subject = “Subject”;
$time_posted = time(); // timestamp

echo "Subject: " . $subject . ". Body: " . $content . ". User ID: " . $div_id . ". Time Posted: " . $time_posted . ". ";

$query = “INSERT INTO mail (subject, content, sender_id, receiver_id, date_sent, msg_received, msg_deleted) VALUES ($subject, $content, 0, $user_id, $time_posted, 0, 0)”;

$result = $db->query($query);

if (!$result) {
echo "Error description: " . mysqli_error($databbase) . “”;
}

My DB structure is like this:
Field / Type
mail_id / int(12)
subject / varchar(25)
content / longtext
sender_id / int(9)
receiver_id / int(9)
date_sent / int(12)
msg_received / tinyint(1)
msg_deleted / tinyint(1)

mail_id is indexed and auto-incremented.

The error I get after the insert:
Error description: 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 ‘Subject, body, 0, 2, 1484783003, 0, 0)’ at line 1

I’m at a lost and and help would be great.

Regards
Guyzer

Changing the php version with 000webhost control panel under general do anything?
Change to like 5.4 etc

1 Like

Thanks for the reply however I figured what the issue was. I forgot to put single quotes around the variables.

It should look like this:
$query = "INSERT INTO mail (subject, content, sender_id, receiver_id, date_sent, msg_received, msg_deleted) VALUES ('$subject', '$content', 0, '$user_id', '$time_posted', 0, 0)";

2 Likes