2017-04-20 157 views
-1

我試圖插入數據到數據庫表中我已經複製了PHPmyadmin給我插入數據到數據庫的代碼。我也研究過其他類似的問題,發現我一直在使用back ticks,我沒有使用保留字來阻止這個工作。 的代碼如下:PHP沒有插入數據庫

<?php 
// Connect to MYSQL 
$connect = mysqli_connect("localhost", "username", "password", 
"Test_downtime"); 
if (!$connect) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 
echo "Connected successfully\n"; 
$sql = mysqli_query("INSERT INTO `Test_downtime`.`Downtime` (`ID`, 
`Server_Name`, `Average_downtime`, `Last_downtime`) VALUES (NULL, \'Test\', 
\'3224\', \'2222\')"); 
if (mysqli_query($sql)) { 
    echo $servername . "has been updated"; 
} 
else{ 
    echo "Error: " ."<br>" . mysqli_error($sql); 
} 
?> 

我想,如果在這裏沒有人有任何想法,這是爲什麼不插入。

+1

'mysqli_query()'需要第一個參數數據庫連接。更好地使用準備好的語句 – Saty

+0

,你可能不想逃避那些引號。 – Strawberry

+1

不要執行查詢兩次! – Saty

回答

0

mysqli_query(連接,查詢)需要兩個paramenters

<?php 

    $connect = mysqli_connect("localhost", "username", "password","Test_downtime"); 

     if (!$connect) 
     { 
      die("Connection failed: " . mysqli_connect_error()); 
     }else 
     { 
      echo "Connected successfully\n"; 
     } 


    $sql = mysqli_query($connect, "INSERT INTO `Downtime` (ID, 
    Server_Name, Average_downtime, Last_downtime) VALUES (NULL, 'Test', 
    3224,2222); "); 

    if ($sql == true) 
    { 
     echo $servername . "has been updated"; 
    } 
    else 
    { 
     echo "Error: " ."<br>" . mysqli_error($sql); 
    } 
    ?> 
+0

謝謝你編輯完成後,你編輯完美 – Nasher