2016-01-24 66 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更好的資源。