2016-11-04 122 views
1

我是新手,請耐心等待我想創建一個將數據添加到MariaDB的HTML表單。基本!但我不能夠PHP7-MariaDB插入數據表格

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 
Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<meta charset="utf-8" /> 
<head> 
<title>PAGINA CARICAMENTO DATI</title> 
</head> 

<body> 
<table border="0"> 
    <tr> 
    <td align="center">Inserisci i dati richiesti</td> 
    </tr> 
    <tr> 
    <td> 
     <table> 
     <form method="post" action="input.php"> 
     <tr> 
      <td>Nome</td> 
      <td><input type="text" name="name" size="20"> 
      </td> 
     </tr> 
     <tr> 
      <td>Cognome</td> 
      <td><input type="text" name="surname" size="20"> 
      </td> 
     </tr> 
     <tr> 
      <td>Città</td> 
      <td><input type="text" name="city" size="20"> 
      </td> 
     </tr> 
     <tr> 
      <td></td> 
      <td align="right"><input type="submit" 
      name="submit" value="Sent"></td> 
     </tr> 
     </form> 
     </table> 
     </td> 
    </tr> 
</table> 
</body> 
</html> 

和PHP的部分是:

<?php 
$host='localhost'; 
$user='root'; 
$password='password'; 
$database='esempio'; 

$connection = mysqli_connect($host,$user,$password,$database); 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 


$name = $_POST['name']; 
$surname = $_POST['surname']; 
$city = $_POST['city']; 

printf($name); 
printf($surname); 
printf($city); 

$sql="INSERT INTO people (ID,Name,Surname,City)VALUES(default,$name,$surname,$city)"; 
printf($sql); 
if(!mysqli_query($connection,$sql)){ 
printf("Errore: %s\n",mysqli_error($connection)); 
} 
mysqli_close($connection); 
?> 

MariaDB的有4列:

  1. ID指數INT(11)否無AUTO_INCREMENT更改更改 Drop Drop
  2. 名稱tinytext utf8_general_ci沒有沒有更改修改刪除刪除
  3. 姓TINYTEXT utf8_general_ci否無 更改更改刪除刪除
  4. 市TINYTEXT utf8_general_ci 否無更改更改刪除刪除
+0

你收到了什麼錯誤和在哪一行? –

+0

'字段列表'中的未知列'pippo' –

+0

您可以發佈表示錯誤發生在 –

回答

1

值需要它們的字符串被引用。

VALUES('','$name','$surname','$city') 

注:由於您的ID列是AI,取出default

但是,這將需要您轉義您的數據出於兩個原因。

  • 如果這些值中的任何值包含MySQL將會抱怨的字符; 即:撇號。
  • 打開SQL注入。

改爲使用準備好的語句。

檢查的查詢還錯誤:

和錯誤報告:

您也應該檢查空輸入。

的另一件事情是要確保你所做列類型的正確選擇。 tinytext可能不是你想在這裏使用的,但仍然可以工作;當使用字符串文字時,varchar通常是首選。

諮詢:


HTML堅持己見:

  • <form>不能是<table>的孩子。
+0

嗨@Fred -ii-你的代碼工作完美,但現在我當然要研究你的鏈接,所以我可以改進我的代碼。你能爲我推薦任何書或其他資源嗎?請考慮我真的需要低層次的方法。 –

+0

@PietroOttati salve Pietro,我很高興聽到它爲你工作。您可以選擇接受答案,以便將其標記爲已解決。否則,其他人可能會試圖發佈更多。這裏是http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work然後返回到答案並做同樣的事情。這是一個看起來不錯的網站http://www.dreamincode.net/forums/topic/54239-introduction-to-mysqli-and-prepared-statements/和http://codular.com/php-mysqli –

+0

@PietroOttati This網站有很多好東西,在那裏http://www.mysqltutorial.org/mysql-prepared-statement.aspx並按照其中的鏈接。 –