I've started to build some very basic shoutbox \ Chat in php. It uses just a basic mysql table with (int) id as unique index and (text) message columns. It works but it stores only numbers in the database. I've searched everywhere for a solution but can't find one yet. I suspect the problem resides either in the mysql database or the query, but maybe something is wrong with code.
Here is the code:
<?php
// ****************
// Database Settings
// ****************
$sql_hst = "localhost";
$sql_db = "chatbox";
$sql_usr = "chatbox";
$sql_pwd = "chatbox";
// ****************
// Core Functions
// ****************
function connectdb()
{
//conection:
global $sql_srv,$sql_usr,$sql_pwd,$sql_db;
$link = mysqli_connect($sql_srv,$sql_usr,$sql_pwd,$sql_db) or die("Error " . mysqli_error($link));
return $link;
}
function getMessages()
{
$result = mysqli_query(connectdb(),"SELECT message FROM messages");
while($row = mysqli_fetch_array($result))
{
echo $row['message'];
echo "<br>";
}
}
function postMessage()
{
if (isset($_POST['message']))
{
mysqli_query(connectdb(), "INSERT INTO messages (message) VALUES (".$_POST['message'].")");
//echo $_POST['message']; //test post data
}
}
connectdb();
postMessage();
//END OF MAIN PHP
?>
<!DOCTYPE html>
<html>
<head><title>Chatbox</title>
<style>
body { background-color: gray; }
input{width:375px;display:block;border: 1px solid #999;height: 25px;--moz-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);}
.message_box{ background-color: white; height: 500px; width: 100%;}
.message_form{width: 100%;}
.message_input{width: 100%;}
.message_submit{}
</style>
</head>
<body>
<div class="message_box"><?php getMessages(); ?></div>
<form class="message_form" action="chatbox.php" method="post">
<input class="message_input" type="text" name="message">
<!-- <input type="submit"> -->
</form>
</body>
</html>