Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL未插入到列中_Php_Mysql_Html_Database_Forms - Fatal编程技术网

Php MySQL未插入到列中

Php MySQL未插入到列中,php,mysql,html,database,forms,Php,Mysql,Html,Database,Forms,我的代码如下: <html><body> <?php $con = mysql_connect("localhost","will","blahblah"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("blahblah", $con); $sql="INSERT INTO links (link, notes, username) V

我的代码如下:

    <html><body>
<?php
$con = mysql_connect("localhost","will","blahblah");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("blahblah", $con);

$sql="INSERT INTO links (link, notes, username)
VALUES
('$_POST[link]','$_POST[notes]','$_POST[username]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 link added";

mysql_close($con)
?>
</body></html>


POST变量的索引必须与表单项的名称匹配

因此,要么写:

或使用
$\u POST[电子邮件]


适应其他变量。

POST变量的索引必须与表单项的名称匹配

因此,要么写:

或使用
$\u POST[电子邮件]


适应其他变量。

id
属性在提交表单时没有意义。您可能需要交换
名称
id
属性

目前,提交的是
$\u POST['name']
$\u POST['email']
$\u POST['password']
,而不是
$\u POST['username']
$\u POST['notes']


您的代码也容易受到攻击。

id
属性在提交表单时毫无意义。您可能需要交换
名称
id
属性

目前,提交的是
$\u POST['name']
$\u POST['email']
$\u POST['password']
,而不是
$\u POST['username']
$\u POST['notes']


您的代码也容易受到攻击。

我发现您的代码至少有三个问题:

首先将字符串注入SQL查询时,必须使用以下方法对其进行转义:


第三个,在PHP代码中,必须使用输入字段的
名称
属性
——而不是它们的
id
属性

考虑到您的HTML代码如下所示:

<input type="text" name="name" id="username" />
<input type="text" name="email" id="link" />
<input type="text" name="password" id="notes" />
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);
$name = mysql_real_escape_string($_POST['name']);

$sql="INSERT INTO links (link, notes, username)
VALUES ('$email','$password','$name')";

注意:输入字段和表中的字段应使用相同的名称,这会使代码更容易理解。

我发现您的代码至少有三个问题:

首先将字符串注入SQL查询时,必须使用以下方法对其进行转义:


第三个,在PHP代码中,必须使用输入字段的
名称
属性
——而不是它们的
id
属性

考虑到您的HTML代码如下所示:

<input type="text" name="name" id="username" />
<input type="text" name="email" id="link" />
<input type="text" name="password" id="notes" />
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);
$name = mysql_real_escape_string($_POST['name']);

$sql="INSERT INTO links (link, notes, username)
VALUES ('$email','$password','$name')";
注意:输入字段和表中的字段应使用相同的名称,这将使代码更容易理解。

替换为:

$sql="INSERT INTO links (link, notes, username)
VALUES
('$_POST[link]','$_POST[notes]','$_POST[username]')";
与:

请注意,您试图在查询中使用的POST变量与表单上的变量完全不同

替换它:

$sql="INSERT INTO links (link, notes, username)
VALUES
('$_POST[link]','$_POST[notes]','$_POST[username]')";
与:


请注意,您试图在查询中使用的POST变量与表单上的变量完全不同。

美元POST中的项目是按名称属性而不是id索引的。

美元POST中的项目是按名称属性而不是id索引的。

您遇到了什么错误?(顺便说一句,用户名输入是“name”而不是“username”)错误是怎么说的?没有错误,只是没有插入它…您遇到了什么错误?(顺便说一句,用户名输入是“name”而不是“username”)错误是什么意思?没有错误,只是没有插入它…首先不是问题。公平使用优先不是问题。合理使用