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行」,沒有別的。
因爲'mysql_connect'不返回對象。請參閱手冊。 –
我從W3Schools獲得了大部分文檔: http://www.w3schools.com/php/php_mysql_insert.asp – zoid230
您是否在最後看到'new mysqli'和'i'。你的代碼裏有這個'i'嗎? –