简体   繁体   English

INSERT INTO数据库表,从表单不起作用。 SQL

[英]INSERT INTO database table, from form not working. SQL

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. 目前我只是试图掌握php和sql所以除了学习之外没有任何意义。 Here is the error i am getting(the first sentence 'connected to database' is from my if statement): 这是我得到的错误(第一句'连接到数据库'来自我的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" “连接到databaseError:您的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. 我的代码可能看起来有点混乱,因为其中一些来自w3schools,有些来自朋友。 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. 我无法弄清楚为什么这段代码不起作用,我已经尝试了很多基于我​​在网上找到的文章和stackoverflow的语法变体,但似乎都没有。 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? 我担心也许我甚至不是数据库的连接,虽然我的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(insert.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)
?>

Drop the quotes around 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]')";

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']."')";

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_NOTICE and should be avoided. 此外,不要在不使用{}语法或分解字符串的情况下直接在字符串中引用关联数组( $_POST ) - 您在那里发布了E_NOTICE ,应该避免。

Read this thoroughly - you'd be amazed what you can (and can't) legally do in PHP strings... 彻底阅读 - 你会惊讶于你可以(而且不能)在PHP字符串中合法地做什么......

try using ` instead of ' when refering to table/column names 在引用表/列名时尝试使用`而不是'

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

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]')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM