Face
Face

Reputation: 21

PHP won't insert chat messages

i have a MySQL database and all, some PHP to insert values, the rest work, but my chat does not.

my host doesnt allow me to see the MySQL error logs so i wont be able to see whats wrong through that.

    <?php
session_start();
include 'default.php';
include 'SteamAuthentication/steamauth/userInfo.php';

$db = getDB();

if (!isset($_SESSION['steamid'])) {
    echo jsonErr('You are not logged in.');
    return;
}

$text = isset($_POST['text']) ? $_POST['text'] : null;

if (is_null($text) || strlen($text) === 0) {
    echo jsonErr('The required text for the message was not sent correctly or was left blank. Please refresh and try again.');
    return;
}

$steamUserID = $steamprofile['steamid'];

$stmt = $db->prepare('INSERT INTO chat (steamUserID, `text`, `date`, `time`) VALUES (:userid, :text, CURDATE(), CURTIME())');
$stmt->bindValue(':userid', $steamUserID);
$stmt->bindValue(':text', $text);
$stmt->execute();

echo jsonSuccess(array('message' => 'Message has been sent!'));
?>

as a note, my other PHP file can insert into my database just fine, so its not the database configuration, is there an issue with my code?

Upvotes: 2

Views: 127

Answers (1)

jmattheis
jmattheis

Reputation: 11125

This is because you have sometimes ` sighs wrapped around the column names and sometimes not, so you have now 2 options:

  1. remove the ` signs

    $stmt = $db->prepare('INSERT INTO chat (steamUserID, text, date, time) VALUES (:userid, :text, CURDATE(), CURTIME())');
    
  2. Or add that to the table name and all column names

     $stmt = $db->prepare('INSERT INTO `chat` (`steamUserID`, `text`, `date`, `time`) VALUES (:userid, :text, CURDATE(), CURTIME())');
    

Upvotes: 1

Related Questions