我是一个新手,所以请耐心等待,我想创建一个HTML表单,添加数据到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>
AND 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列:
- ID Index int(11)无AUTO_INCREMENT Change变化Drop Drop
- 名称tinytext utf8_general_ci否无更改更改Drop Drop
- 姓氏tinytext utf8_general_ci否无Change Change Drop Drop
- 城市文本utf8_general_ciChange Drop Drop
字符串值需要加引号。
VALUES('','$name','$surname','$city')
注意:由于您的ID列是AI,请删除default
。
但是,出于两个原因,这将要求您转义数据。
- 如果这些值中的任何一个包含MySQL会抱怨的字符;。: 撇号。 打开SQL注入
请使用预处理语句。
- http://php.net/manual/en/mysqli.prepare.php
- https://en.wikipedia.org/wiki/Prepared_statement
检查查询中的错误:
- http://php.net/manual/en/mysqli.error.php
和错误报告:
- http://php.net/manual/en/function.error-reporting.php
您还应该检查空输入。
- http://php.net/manual/en/function.empty.php
另一件事是确保您选择了正确的列类型。tinytext
可能不是您想在这里使用的,但仍然可以工作;当使用字符串字面值时,varchar
通常是首选。
咨询:
- http://dev.mysql.com/doc/refman/5.7/en/string-type-overview.html
- TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT最大存储容量
HTML stickler:
-
<form>
不能是<table>
的子节点。