无法更新 MySQL 数据库

发布于 2024-09-14 10:58:22 字数 555 浏览 11 评论 0原文

我正在尝试学习 mysql,并且在更新/向表添加数据时遇到一些问题,

这是我的代码,运行此页面后,当我转到 phpmyadmin 查看新数据是否显示时,我在那里看不到它。

<?php

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

if(!$conn) {  die("Could not connect");   }
$dbname = "test";
mysql_select_db($dbname, $conn);

mysql_query("INSERT INTO 'test'.'table1' 
               ('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8') 
             VALUES  
               ('test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7', 'test8')"); 

mysql_close($conn);

?>

谁能告诉我这有什么问题吗?

I am trying to learn mysql and having some problem with updating/adding data to the table

this is my code and after running this page when I go to phpmyadmin to see if the new data showed up, i dont see it there.

<?php

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

if(!$conn) {  die("Could not connect");   }
$dbname = "test";
mysql_select_db($dbname, $conn);

mysql_query("INSERT INTO 'test'.'table1' 
               ('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8') 
             VALUES  
               ('test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7', 'test8')"); 

mysql_close($conn);

?>

Can anyone tell me whats wrong with this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

墨小墨 2024-09-21 10:58:22

问题在于字段名称周围的引号。 MySQL 不允许使用单引号引起来的字段名称。它们要么必须是裸露的 (A1),要么是在反引号内 (`A1`),因此您的查询应重写为:

INSERT INTO table1 (A1, A2, A3, A4, A5, A6, A7, A8)
VALUES ('test1', 'test2', 'test3', 'test4', etc.....);

The problem is the quotes around the field names. MySQL doesn't allow single-quoted field names. They either have to be bare (A1), or within backticks (`A1`), so your query should be rewritten as:

INSERT INTO table1 (A1, A2, A3, A4, A5, A6, A7, A8)
VALUES ('test1', 'test2', 'test3', 'test4', etc.....);
赴月观长安 2024-09-21 10:58:22

请务必进行错误检查,看看 mysql 连接是否也有问题:

if(!mysql_select_db($dbname, $conn)) {
   echo  mysql_error($conn);
}else {

    if(!mysql_query("INSERT INTO  'test'.'table1' ('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8') VALUES ('test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7', 'test8')")) {
      echo  mysql_error($conn);
   } 
}

Be sure to do error checking to see if the mysql connection is having trouble too:

if(!mysql_select_db($dbname, $conn)) {
   echo  mysql_error($conn);
}else {

    if(!mysql_query("INSERT INTO  'test'.'table1' ('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8') VALUES ('test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7', 'test8')")) {
      echo  mysql_error($conn);
   } 
}
幽梦紫曦~ 2024-09-21 10:58:22

首先,确保数据库选择正确:

mysql_select_db($dbname, $conn) or die('Could not select the database');

您的查询中可能存在一些错误,这是未添加数据的原因,请尝试在此之后添加 or die(mysql_error()) mysql_query 函数查看可能的错误:

mysql_query("your query....") or die(mysql_error()); 

也无需指定数据库名称:

'test'.'table1'

只需在查询中使用 table1 即可。

打开错误报告也很有用:

ini_set('display_errors', true);
error_reporting(E_ALL);

First of all, make sure that database was selected fine:

mysql_select_db($dbname, $conn) or die('Could not select the database');

There are chances that there is some error in your query, a reason why data is not added, try adding or die(mysql_error()) after this mysql_query function to see the possible error:

mysql_query("your query....") or die(mysql_error()); 

Also no need to specify the database name:

'test'.'table1'

Just use table1 in your query.

Turning on error reporting is also useful:

ini_set('display_errors', true);
error_reporting(E_ALL);
棒棒糖 2024-09-21 10:58:22

以下是在使用 MYSQL(或任何 SQL DB)时为您提供的一般提示。如果你不知道如何做某事,或者你做错了事情。使用为您提供的 GUI 工具来完成这项工作,然后检查生成的代码。

输入PHPMyAdmin,进行插入,然后复制粘贴代码,看看你做错了什么。

Here is a tip for you in general when working with MYSQL (Or any SQL DB). If you don't know how to do something, or you are getting things wrong. Use the GUI tools provided for you to do the job, then check the resulting code.

Enter PHPMyAdmin, Do an insert, and then copy and paste the code to see what you are doing wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文