0

I have a db.php file which connection is established here to the database

<?php 
   $host = "localhost"; 
   $user = "root";
    $pass = ""; 
    $db_name = "aigsonlinedb"; 
    $con = new mysqli($host,$user,$pass,$db_name);
    function formatDate($date){
    return date('g:i a', strtotime($date));
    }
    ?>

"Index.php" file here is a form where the data should be sent and retrieved from the database

      <?php 
      include 'db.php';
      ?>
    <!DOCTYPE html> 
    <html>
    <head>
        <title>Chat System in PHP</title>
    <link rel="stylesheet" href="style.css" media="all"/>
    <script>
        function ajax(){

        var req = new XMLHttpRequest();

        req.onreadystatechange = function(){

        if(req.readyState == 4 && req.status == 200){

        document.getElementById('chat').innerHTML = req.responseText;
        } 
        }
        req.open('GET','chat.php',true); 
        req.send();

        }
        setInterval(function(){ajax()},1000);
    </script>
    </head>

    <body onload="ajax();">

    <div id="container">
        <div id="chat_box">
        <div id="chat"></div>
        </div>
        <form method="POST" action="index.php">
        <input type="text" name="name" placeholder="enter name"/> 
        <textarea name="msg" placeholder="enter message"></textarea>
        <input type="submit" name="submit" value="Send it"/>

        </form>
        <?php 
        if(isset($_POST['submit'])){ 

        $name = $_POST['name'];
        $msg = $_POST['msg'];

        $query = "INSERT INTO chat (name,msg) VALUES ($name','$msg')";

        $run = $con->query($query);

        if($run){
            echo "<embed loop='false' src='chat.wav' hidden='true'       autoplay='true'/>";
        }
        }
        ?>
   </div>
   </body>
    </html>

chat.php where the data is fetched from the database

<?php 
    include 'db.php';

    $query = "SELECT * FROM chat ORDER BY id DESC";
    $run = $con->query($query);
    while($row = $run->fetch_array()) :
        ?>
            <div id="chat_data">
                <span style="color:green;"><?php echo $row['name']; ?></span> :
                <span style="color:brown;"><?php echo $row['msg']; ?></span>
                <span style="float:right;"><?php echo formatDate($row['date']);        ?></span>
            </div>
            <?php endwhile;?>

The only problem is the data can not be sent to the database.

Reshad Zazai
  • 95
  • 1
  • 10

1 Answers1

0

In the end of String ; vor SQL Statements. And PHP variables in double quotes and extra double quotes in values for define the datatype of sql