Making a shoutbox with PHP, MySql and jQuery – Revisited

M

Recommendation to all my readers who have trouble with their hosting not installing PDO: change your hosting.

This is just an update on a previous tutorial which can be found here.

In that particular tutorial I am using PDO class to manipulate the MySql database. Everything else stays the same, except the code in shout.php file.

For all readers that can not use PDO, change the code in shout.php to this:

/*** mysql hostname ***/
$hostname = 'localhost';

/*** mysql username ***/
$username = 'root';

/*** mysql password ***/
$password = '';

$dbname = 'demo';

mysql_connect($hostname, $username, $password);
mysql_select_db($dbname);

if($_POST['name']) {
    $name       = mysql_real_escape_string($_POST['name']);
    $message    = mysql_real_escape_string($_POST['message']);
    
    $sql = "INSERT INTO shoutbox (date_time, name, message)
            VALUES (NOW(), '".$name."', '".$message."')";

    /*** run the sql statement ***/
    if (mysql_query($sql)) {
        populate_shoutbox();
    }
} 


if($_POST['refresh']) {
    populate_shoutbox();
}


function populate_shoutbox() {
    $sql = "select * from shoutbox order by date_time desc limit 10";
    $rez = mysql_query($sql);
    echo '<ul>';
    while ($row = mysql_fetch_array($rez, MYSQL_ASSOC)) {
        echo '<li>';
        echo '<span class="date">'.date("d.m.Y H:i", strtotime($row['date_time'])).'</span>';
        echo '<span class="name">'.$row['name'].'</span>';
        echo '<span class="message">'.$row['message'].'</span>';
        echo '</li>';
    }
    echo '</ul>';
}

Now your shoutbox is working like a charm using mysql functions.


About the author

Zvonko Biškup

A passionate web developer with more than 13 years experience in PHP and other web related technology. My main interest is WordPress and new PHP frameworks.
Owner of Influendo Programming.
Enjoys exploring new stuff and possibilities, using new technologies and just having fun developing awesome stuff.

  • Pingback: Making a shoutbox with PHP, MySql and jQuery – Revisited | Sharebrain()

  • Pingback: Making a shoutbox with PHP, MySql and jQuery – Revisited | Sharebrain()

  • khawer

    Undefined index: name in C:\wamp\www\shoutbox\shoutbox\shout.php on line 16
    please help

    • Zvonko

      Probably, you renamed form field ‘name’ to something else in the form.

By Zvonko Biškup

Tags