Php开始报道'意外'错误

时间:2017-03-15 12:37:09

标签: php mysql mysqli

我正在和我的学校项目聊天。 Php代码分为两个带代码的文件。第一个文件是文件,其中是聊天窗口和输入消息的文本输入。这是第一个文件的PHP代码:

<?php
            $connection = mysql_connect('localhost', 'root', 'root');
            if (!$connection) {
                die('<b>Can\'t connect to database server. Error message: ' . mysql_error() . '</b>');
            }

            $database = mysql_select_db('vaclavik', $connection);
            if (!$database) {
                $command = 'CREATE DATABASE vaclavik';
                if (mysql_query($command, $connection)) {
                    echo '<b>Database wasn\'t found. New one has been created. </b>';
                    mysql_close($connection);
                    $connection = new mysqli('localhost', 'root', 'root', 'vaclavik');
                    $command = 'CREATE TABLE chat(datum DATETIME, zprava TEXT)';
                    if (mysqli_query($connection, $command)) {
                        die('New table has been created!');
                    }
                    else {
                        die('Can\'t create new table!');
                    }
                }
                else {
                    die('Database wasn\'t found and new one can\'t be created!');
                }
            }
            mysql_close($connection);

            $connection = mysqli_connect('localhost', 'root', 'root', 'vaclavik');

            $command = 'SELECT * FROM chat';
            $result = mysqli_query($connection, $command);
            if(empty($result)) {
                $command = 'CREATE TABLE chat(datum DATETIME, zprava TEXT)';
                if (mysqli_query($connection, $command)) {
                        die('New table has been created');
                    }
                    else {
                        die('New table can\'t be created');
                    }
            }

            if (mysqli_num_rows($result) > 0) {
                while($row = $result->fetch_assoc()) {
                    echo '<b>' . $row["datum"] . ":</b> " . $row["zprava"] . "<br />";
                }
            }
            else {
                echo '<b>No message found. Write something! :)</b>';
            }
            mysqli_close($connection);
        ?>

此代码有效。然后我创建了表单来输入消息。它使用php代码重定向到另一个文件,以将消息插入数据库。问题是,如果我把代码没有错误检测,它什么都不做。当我在那里放置一些错误保护时,php开始报告在代码中出现意外字符的Parse错误。通常&#39; {&#39;并且&#39;回声&#39;。

这是第二个文件的代码:

<?php
$message = $_POST["chatinput"];
$connection = mysqli_connect('localhost', 'root', 'root', 'vaclavik');
$command = "INSERT INTO chat (datum, zprava) VALUES (\'" . date("Y-m-d, h:i:sa") . "\', \'" . $message . "\')";
mysqli_query($connection, $command);
mysqli_close($connection);
echo "<script>window.location = \"../chat.php\"</script>";
?>

0 个答案:

没有答案