将数据插入数据库 php 没有行被填充 APACHE2

Inserting data into a database with php no rows being filled APACHE2

我正在尝试开发一个 Web 服务器,用于为我们的龙与地下城小组掷骰子和发送消息。我可以很好地创建和删除表,而且我相信我的视图功能正在运行。但是,它表示在其结果中返回了 0 行。所以我认为我的插入数据 sql 查询存在问题。

插入数据php页:

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'anthony';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
mysql_select_db( 'messages' );




    $number = 0;
   echo "Name: {$_POST['name']}<br />";
   echo "Subject: {$_POST['subject']}<br />";
   echo "Message: {$_POST['message']}<br /><br />";
   if(strcmp($_POST['die'],"D100") == 0 ){
       $number = (mt_rand ( 1 , 100 )* + $_POST['amount'] ) + $_POST['modifier'];
       echo $number;
   }
   if(strcmp($_POST['die'],"D20") == 0 ){
       $number = (mt_rand ( 1 , 20 )* + $_POST['amount'] ) + $_POST['modifier'];
       echo $number;
   }
   if(strcmp($_POST['die'],"D12") == 0 ){
       $number = (mt_rand ( 1 , 12 )* + $_POST['amount'] ) + $_POST['modifier'];
       echo $number;
   }
   if(strcmp($_POST['die'],"D10") == 0 ){
       $number = (mt_rand ( 1 , 10 )* + $_POST['amount'] ) + $_POST['modifier'];
       echo $number;
   }
   if(strcmp($_POST['die'],"D8") == 0 ){
       $number = (mt_rand ( 1 , 8 )* + $_POST['amount'] ) + $_POST['modifier'];
       echo $number;
   }
   if(strcmp($_POST['die'],"D6") == 0 ){
       $number = (mt_rand ( 1 , 6 )* + $_POST['amount'] ) + $_POST['modifier'];
       echo $number;
   }
   if(strcmp($_POST['die'],"D3") == 0 ){
       $number = (mt_rand ( 1 , 3 )* + $_POST['amount'] ) + $_POST['modifier'];
       echo $number;
   }



$sql = "INSERT INTO message_tbl (message_name, message_subject, message_txt, message_amount, message_die, message_modifier, message_roll)
VALUES ('".$_POST['name']."', '".$_POST['subject']."', '".$_POST['message']."', '".$_POST['amount']."', '".$_POST['die']."', '".$_POST['modifier']."', '".$number."')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$conn->close();


?>

这显示了我的滚动和以前表格中的所有信息,但是它不执行任何这些回显:

if ($conn->query($sql) == TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

我的浏览数据PHP页:

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'anthony';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
mysql_select_db( 'messages' );


$query = "SELECT * FROM message_tbl";
$result = mysql_query($query);
printf("Select returned %d rows.\n", $result->num_rows); //prints how many rows returned

echo "<table>";
while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
echo "id: " . $row["message_id"]. " " . $row["message_name"]. " " . $row["message_subject"]. " " . $row["message_txt"]. " " . $row["message_amount"]. " " . $row["message_die"]. " " . $row["message_modifier"]. " " . $row["message_roll"]. "<br>";
}

echo "</table>"; //Close the table in HTML

mysql_close(); //Make sure to close out the database connection
?>

我的视图数据页 returns 有 "select returned 0 rows",没有别的。

抱歉,我不是 php 方面的专家,我才刚刚开始进行 Web 开发,并且会边学边做。但据我所知,mysql 已被弃用,建议使用 MySQLi。情况可能并非如此,但我注意到您在代码中使用了 OO 和过程风格,无论我编写什么代码,我都只坚持使用一种方法。可能不是您想要的答案,但我无法发表评论。

此外,在 php 方面,我建议使用 php.net 作为比 w3schools 更好的资源。