php INSERT INTO 数据库表,从表单不工作。SQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7105406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:01:07  来源:igfitidea点击:

INSERT INTO database table, from form not working. SQL

phpsqldatabase

提问by daniel blythe

lets get straight to my problem, the code I have written here does not write to my database and I cannot figue out why. At the moment I am simply trying to get to grips with php and sql so there is no point to this form other than learning. Here is the error i am getting(the first sentence 'connected to database' is from my if statement):

让我们直接解决我的问题,我在这里编写的代码没有写入我的数据库,我不知道为什么。目前我只是试图掌握 php 和 sql,所以除了学习之外,这种形式没有任何意义。这是我得到的错误(第一句话“连接到数据库”来自我的 if 语句):

"Connected to databaseError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''test' ('name') VALUES ('daniel')' at line 1"

“连接到数据库错误:您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,以了解在第 1 行的 ''test' ('name') VALUES ('daniel')' 附近使用的正确语法”

The code I have may look a little confusing as some of it is from w3schools and some is from a friend. I cannot figure out why this code isn't working, I have tried many variations of the syntax based on loads of articles I have found online and on stackoverflow but none seem to work. I fear that maybe I am not even connectec to the database, although my if statement tells me otherwise, so that could be a problem?

我的代码可能看起来有点混乱,因为其中一些来自 w3schools,一些来自朋友。我无法弄清楚为什么这段代码不起作用,我已经根据我在网上和 stackoverflow 上找到的大量文章尝试了许多语法变体,但似乎都没有。我担心也许我什至没有连接到数据库,尽管我的 if 语句告诉我不是这样,所以这可能是一个问题?

Hopefully if this gets solved this question will clarify database connection and writing to a database from a form in one hit. Thanks in advance guys and here's my code.

希望如果这个问题得到解决,这个问题将澄清数据库连接并一次性从表单写入数据库。在此先感谢大家,这是我的代码。

HTML

HTML

<form action="insert.php" method="post">
Name: <input type="text" name="namefield" />
<input type="submit" />
</form>

PHP (insert.php)

PHP(插入.php)

<?php
$dbhost  = 'localhost';
$dbname  = 'carbon_db';
$dbuser  = 'username';
$dbpass  = 'password'; 

$con = mysql_connect($dbhost, $dbuser, $dbpass);

if($con == FALSE)
{
    echo 'Cannot connect to database' . mysql_error();
}
else
{
    echo 'Connected to database';
}

mysql_select_db($dbname, $con);


$sql="INSERT INTO 'test' ('name') 
VALUES ('$_POST[namefield]')";

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

mysql_close($con)
?>

回答by codaddict

Drop the quotesaround the table name or change them to back ticks:

去掉表名周围的引号或将它们更改为反引号

Change:

改变:

$sql="INSERT INTO 'test' ('name') VALUES ('$_POST[namefield]')";

To:

到:

$sql="INSERT INTO test ('name') VALUES ('$_POST[namefield]')";

Or

或者

$sql="INSERT INTO `test` ('name') VALUES ('$_POST[namefield]')";

回答by Mihai Iorga

It's often best to use backticks for MySQL as like any other storage engines it has it's own reserved names and it's own reserved insert practices.

通常最好对 MySQL 使用反引号,就像任何其他存储引擎一样,它有自己的保留名称和自己的保留插入实践。

try with

尝试

$sql = "INSERT INTO `test` (`name`) VALUES ('".$_POST['namefield']."')";

回答by DaveRandom

Change the single quotes surrounding the table name and the column name to backticks. Or get rid of them all together.

将表名和列名周围的单引号更改为反引号。或者一起摆脱它们。

$sql="INSERT INTO `test` (`name`)  VALUES ('{$_POST['namefield']}')";

Also, don't reference associative arrays ($_POST) directly in a string without using {}syntax or breaking up the string - what you have done there issues an E_NOTICEand should be avoided.

此外,不要$_POST在不使用{}语法或分解字符串的情况下直接在字符串中引用关联数组 ( ) - 您在那里所做的事情会发出E_NOTICE并且应该避免。

Read thisthoroughly - you'd be amazed what you can (and can't) legally do in PHP strings...

仔细阅读本文- 您会惊讶于您可以(和不能)在 PHP 字符串中合法地做什么...

回答by Sleeperson

try using ` instead of ' when refering to table/column names

在引用表/列名称时尝试使用 ` 而不是 '

$sql="INSERT INTO `test` (`name`) 
VALUES ('$_POST[namefield]')";

回答by Andreas ?gren

Remove the single quotes around your sql statement and replace with back-tics (not sure even they are necessary):

删除 sql 语句周围的单引号并用 back-tics 替换(甚至不确定它们是否必要):

$sql="INSERT INTO `test` ('name') 
VALUES ('$_POST[namefield]')";