I am working on a commenting system. I need to be able to post (Insert into database) and the post automatically has to be visible on the site as well.
I have two PHP files, one which is the main page (index.php) and one to connect to the database and fetch (connect_to_database.php).
From connect_to_database.php I connect to the database, and try to fetch row. Then Ajax script is supposed to print into a div the information.
I am able to post, and I can check that the information is added to the database, but I do not see any of the database rows printed.
Could the problem be with the Ajax script?
Thanks for your help,
connect_to_database.php
...
$connect=mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database);
//$connect=mysqli_connect("localhost", "commenter","phyBq2JT", "udb_commenter" );
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_select = mysqli_select_db($connect,'udb_commenter');
$sql="SELECT * FROM $TABLE_NAME";
$result=mysqli_query( $connect, $sql);
$array = mysqli_fetch_row($result);
echo json_encode($array);
?>
index.php Here is only the part of the Ajax code. Above it, I have a working connection to the database.
<div class="output-post" id="output">
<script language="JavaScript" id="print" type="text/javascript" src="jquery.js">
$(function ()
{
$.ajax({
url: 'connect_to_database.php',
data: "",
dataType: 'json',
success: function(data)
{
var id = data[0];
var name = data[1];
var email = data[2];
var type = data[3];
var message = data[4];
var date = data[5];
$('.output-post').html("<b>id: </b>"+id+"<b> name: </b>"+name+email+type+message+date);
}
});
});
</script>
</div>
The sql table:
CREATE TABLE IF NOT EXISTS `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(40) NOT NULL,
`email` varchar(60) NOT NULL,
`type` varchar(40) NOT NULL,
`message` text NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
);
console.log(data)says?