2016-01-24 68 views
1

我正在尝试开发一个Web服务器,用于掷骰子和发送消息给我们的地下城和龙群。我可以创建和删除表格,并且我相信我的视图功能正在工作。但是它的结果是说0行正在返回。所以我相信我的插入数据sql查询存在问题。使用php插入数据到数据库中没有行被填充APACHE2

插入数据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 
?> 

我的视图数据页返回“选择返回0行”,没有别的。

+1

因为'mysql_connect'不返回对象。请参阅手册。 –

+0

我从W3Schools获得了大部分文档: http://www.w3schools.com/php/php_mysql_insert.asp – zoid230

+1

您是否在最后看到'new mysqli'和'i'。你的代码里有这个'i'吗? –

回答

0

对不起,我不是php的专家,我只是刚刚开始为Web开发,并且随着我的学习。但据我所知,mysql已被弃用,并推荐使用MySQLi。可能并非如此,但我注意到你在代码中使用OO和程序风格,我只用一种方法在我编写的任何代码中插入。可能不是你的答案,但我无法发表评论。

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