Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数据插入数据库时,php不填充任何行2_Php_Mysql_Apache_Webserver_Raspberry Pi2 - Fatal编程技术网

将数据插入数据库时,php不填充任何行2

将数据插入数据库时,php不填充任何行2,php,mysql,apache,webserver,raspberry-pi2,Php,Mysql,Apache,Webserver,Raspberry Pi2,我正在尝试开发一个web服务器,用于掷骰子和为我们的龙与地下城小组发送消息。我可以很好地创建和删除表,而且我相信我的视图功能正在工作。但是,它表示在其结果中返回0行。因此,我认为我的insert data sql查询存在问题 插入数据php页面: <?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'anthony'; $conn = mysql_connect($dbhost, $dbuser, $dbpass);

我正在尝试开发一个web服务器,用于掷骰子和为我们的龙与地下城小组发送消息。我可以很好地创建和删除表,而且我相信我的视图功能正在工作。但是,它表示在其结果中返回0行。因此,我认为我的insert data 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();


?>

对不起,我不是php专家,我才刚刚开始为web开发,正在学习。但据我所知,mysql已被弃用,建议使用MySQLi。可能不是这样,但我注意到您在代码中使用的是OO和过程风格,我在编写的代码中只使用过一种方法。可能不是你想要的答案,但我无法发表评论


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

对不起,我不是php方面的专家,我刚刚开始为web开发,正在不断学习。但据我所知,mysql已被弃用,建议使用MySQLi。可能不是这样,但我注意到您在代码中使用的是OO和过程风格,我在编写的代码中只使用过一种方法。可能不是你想要的答案,但我无法发表评论


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

因为
mysql\u connect
不返回对象。请参阅手册。我的大部分文档都是从W3Schools获得的:您是否看到最后带有
I
new mysqli
。你的代码中有这个
i
吗?没有,我在任何地方的代码中都不使用mysqli。如果你不想使用
mysqli
-好的。使用
mysql
,但要注意
mysql
扩展在php5.5中已被弃用,并在php7中删除。所以很快你的
mysql
代码就无法工作了。如果您使用的是
mysql
,那么就不要参考
mysqli
的手册,因为
mysql\u connect
不会返回对象。请参阅手册。我的大部分文档都是从W3Schools获得的:您是否看到最后带有
I
new mysqli
。你的代码中有这个
i
吗?没有,我在任何地方的代码中都不使用mysqli。如果你不想使用
mysqli
-好的。使用
mysql
,但要注意
mysql
扩展在php5.5中已被弃用,并在php7中删除。所以很快你的
mysql
代码就无法工作了。如果您使用的是
mysql
,那么就不要参考
mysqli
的手册。
if ($conn->query($sql) == TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
<?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
?>