2015-05-02 88 views
-2

好,所以我可以連接和查看數據庫與我的PHP代碼,但我不能插入數據到它。有我用phpmyadmin測試的查詢,它能夠插入新數據到我的表不能插入數據到MySQL與PHP

INSERT INTO `members` (`id` , `username` , `email`) 
VALUES (123456789, 'russi', '[email protected]') 

然後我試圖把它變成我的實際php文件

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = "blablabla"; 
$dbname = "test_database"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
$sql = "INSERT INTO 'members' ('id', 'username', 'email') 
VALUES (2339978, 'vladtheimpalor', '[email protected]')"; 
$sql = "SELECT id, username, email FROM members"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     echo "id: " . $row["id"]. " - username: " . $row["username"]. " -email:" . $row["email"]. "<br>"; 
    } 
} else { 
    echo "0 results"; 
} 
$conn->close(); 
?> 

所以選擇功能的作品,但插入沒有。

+1

您的INSERT查詢立即被一個SELECT查詢重寫和永遠不會執行。 –

回答

2

要覆蓋你的$sql可變W/O執行它。再說,你不應該使用的列單引號,但反引號(見When to use single quotes, double quotes, and backticks in MySQL

變化

$sql = "INSERT INTO 'members' ('id', 'username', 'email') 
VALUES (2339978, 'vladtheimpalor', '[email protected]')"; 
$sql = "SELECT id, username, email FROM members"; 
$result = $conn->query($sql); 

$sql = "INSERT INTO `members` (`id`, `username`, `email`) 
VALUES (2339978, 'vladtheimpalor', '[email protected]')"; 
$result = $conn->query($sql); 
$sql = "SELECT id, username, email FROM members"; 
$result = $conn->query($sql); 
+0

由於某種原因仍然不會工作,所以我將不得不與我的教授檢查什麼在那裏 – alkokarko

+0

嘗試閱讀'$ conn-> error'(https:/ /php.net/manual/de/mysqli.error.php) – MrTux

+0

解決了這個問題,我在故事中沒有默認值。 – alkokarko

0

刪除單引號括起來的列名和表名:

$sql = "INSERT INTO members (id, username, email) 
VALUES (2339978, 'vladtheimpalor', '[email protected]')"; 

單引號僅用於焦炭領域。

此外,您永遠不會執行插入語句,因爲您覆蓋它。

1

更改您的插入到:

$sql = "INSERT INTO members (id, username, email) 
VALUES (2339978, 'vladtheimpalor', '[email protected]')"; 

,並致電查詢:

$sql = "INSERT INTO members (id, username, email) 
VALUES (2339978, 'vladtheimpalor', '[email protected]')"; 
//Here, you never execute your query 
$result = $conn->query($sql); 

$sql = "SELECT id, username, email FROM members"; 
$result = $conn->query($sql); 
+0

@Jens當然是有人喜歡下來的投票按鈕;-) –

1

當然它沒有工作!

你永遠不執行你的INSERT ...

<?php 
... 
$sql = "INSERT INTO 'members' ('id', 'username', 'email') 
VALUES (2339978, 'vladtheimpalor', '[email protected]')"; 
$conn->exec($sql); 
$sql = "SELECT id, username, email FROM members"; 
$result = $conn->query($sql); 
... 

:)

+0

沒有'exec'方法,是嗎? (https://php.net/manual/de/class.mysqli.php)單引號也是錯誤的。 – MrTux

0
$sql = "INSERT INTO 'members' ('id', 'username', 'email') 
VALUES (2339978, 'vladtheimpalor', '[email protected]')"; 
$sql = "SELECT id, username, email FROM members"; 
//missing the grave accent 
$sql = "INSERT INTO `members` (id, username, email) 
VALUES (2339978, 'vladtheimpalor', '[email protected]')"; 

$sql = "SELECT `id`, `username`, `email` FROM `members`"; 

/* This is the corrrected code */