2016-11-04 121 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并按照其中的链接。 –