使用 PHP 更新数据库时出错

发布于 2024-11-18 23:52:28 字数 1618 浏览 3 评论 0原文

我目前正在尝试使用 PHP 将数据插入 MySQL 数据库。为了提供帮助,我正在关注 这个教程。

到目前为止,我已采取以下步骤:

  • 通过 phpMyAdmin 在我的 WAMPSERVER 上手动创建一个名为“core_group_me”的数据库
  • 创建一个名为“form.php”的表单页面(代码如下):

    <代码>
    <头>
    <标题>
    
    
    <正文>
    
    用户名:
    > <输入类型=“文本”名称=“id”大小=“30”/>
    > 名字:
    > <输入类型=“文本”名称=“名字”大小=“30”/>
    > 姓氏:
    > <输入类型=“文本”名称=“姓氏”大小=“30”/>
    > 电子邮件:
    > <输入类型=“文本”名称=“电子邮件”大小=“30”/>
    > 地点:
    > <输入类型=“文本”名称=“位置”尺寸=“30”/>
    > 专长:
    > <输入类型=“文本”名称=“专业知识”大小=“30”/>

    >
  • 创建了一个用于更新“core_group_me”数据库的页面,名为“update.php”(代码如下):

    <前><代码>

也就是说,当我打开“form.php”页面时,在指定的位置输入信息字段并提交所有内容,我收到以下错误:“更新数据库时出错”。我对 PHP 编程和使用 MySQL 数据库非常很陌生,所以说实话,我完全不知道我哪里出了问题。任何帮助将不胜感激!

I'm currently attempting to insert data into a MySQL database using PHP. To help, I am following this tutorial.

Thus far, I have taken the following steps:

  • Manually created a database via phpMyAdmin named "core_group_me" on my WAMPSERVER
  • Created a form page named "form.php" (code below):

    <html>
    <head>
    <title></title>
    </head>
    
    <body>
    <form method="post" action="update.php">
    
    Username:<br />
    
    <input type="text" name="id" size="30" /><br />
    
    First Name:<br />
    
    <input type="text" name="firstname" size="30" /><br />
    
    Last Name:<br />
    
    <input type="text" name="lastname" size="30" /><br />
    
    Email:<br />
    
    <input type="text" name="email" size="30" /><br />
    
    Location:<br />
    
    <input type="text" name="location" size="30" /><br />
    
    Expertise:<br />
    
    <input type="text" name="expertise" size="30" /><br /><br />
    
    <input type="submit" value="Update Database" />
    
    </body>
    </html>
    
  • Created a page to update the "core_group_me" database named "update.php" (code below):

    <?php
    
    $id = $_POST['id'];
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $location = $_POST['location'];
    $expertise = $_POST['expertise'];
    
    mysql_connect ("localhost", "core_group_me", "") or die ('Error: ' .mysql_error());
    mysql_select_db ("members");
    
    $query="INSERT INTO members (id, firstname, lastname, email, location, expertise)VALUES ('".$id."', '".$firstname."', '".$lastname."', '".$email."', '".$location."', '".$expertise."')";
    
    mysql_query($query) or die ('Error updating database');
    
    echo "Database Updated With: " .$id. " " .$firstname. " ".$lastname. " ".$email. " " .$location. " " .$expertise ;
    
    ?>
    

That being said, when I open up the "form.php" page, input information in the specified fields and submit everything, I receive the the following error: "Error updating database". I'm very new to programming in PHP and working with MySQL databases, so I honestly have absolutely no idea where I've gone wrong. Any help would be much appreciated!

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

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

发布评论

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

评论(4

心作怪 2024-11-25 23:52:28

在下面的行中,使用您的 mysql 用户名而不是 core_group_me,第三个参数是您的密码,如果您没有密码,则将其保留为空白,否则写下您的密码。

mysql_connect ("localhost", "core_group_me", "") or die ('Error: ' .mysql_error());

例如:

mysql_connect ("localhost", "root", "123456") or die ('Error: ' .mysql_error());

并将此行:编辑

mysql_select_db ("members");

为:

mysql_select_db ("core_group_me");

In the below line, instead of core_group_me use your mysql user name and the third parameter is your password, if you don't have password then leave it as blank otherwise write your password.

mysql_connect ("localhost", "core_group_me", "") or die ('Error: ' .mysql_error());

Eg:

mysql_connect ("localhost", "root", "123456") or die ('Error: ' .mysql_error());

and also edit this line:

mysql_select_db ("members");

into:

mysql_select_db ("core_group_me");
落墨 2024-11-25 23:52:28

我相信 Francois 提到了 sql 中的错误。为了避免只收到死消息,您可以添加类似 http://php.net 的内容/manual/de/function.mysql-error.php

mysql_query($query) or die ('Error updating database<br />' . mysql_errno() . ": " . mysql_error());

由于应用程序安全性变得越来越重要,您应该转义字符串并检查输入的内容。我也不会向用户显示错误消息,我个人会将其写入日志文件

I believe Francois mentioned the error in the sql. To avoid just getting the die message you could add something like http://php.net/manual/de/function.mysql-error.php.

mysql_query($query) or die ('Error updating database<br />' . mysql_errno() . ": " . mysql_error());

Since application security is getting more important you should escape your strings as well as check the contents of the inputs. I also wouldn't display a error message to the user, I personally would write it to a log file

作死小能手 2024-11-25 23:52:28

使用以下结构创建数据库:

CREATE TABLE stackoverflow.members (
id INT NOT NULL AUTO_INCRMENT PRIMARY KEY ,
用户名 VARCHAR( 30 ) NOT NULL ,
名字 VARCHAR( 30 ) NOT NULL ,
姓氏 VARCHAR( 30 ) NOT NULL ,
电子邮件 VARCHAR( 100 ) NOT NULL ,
位置 VARCHAR( 30 ) NOT NULL ,
专业知识 VARCHAR( 30 ) NOT NULL
) 引擎 = MYISAM ;

和 update.php:

<?php

 $username = $_POST['id'];
 $firstname = $_POST['firstname'];
 $lastname = $_POST['lastname'];
 $email = $_POST['email'];
 $location = $_POST['location'];
 $expertise = $_POST['expertise'];

 mysql_connect ("localhost", "root", "yourpassword") or die(mysql_error());
 mysql_select_db ("stackoverflow");

 $query="INSERT INTO members (id,username, firstname, lastname, email, location, expertise) VALUES ('','$username', '$firstname', '$lastname', '$email', '$location', '$expertise')";

 mysql_query($query) or die (mysql_error());

 echo "Database Updated With: " .$username. " , " .$firstname. " , ".$lastname. " , ".$email. " , " .$location. " , " .$expertise ;

 ?>

不要将其复制并粘贴到您的文件中..只需尝试理解它..

Create database using this structure:

CREATE TABLE stackoverflow.members (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
username VARCHAR( 30 ) NOT NULL ,
firstname VARCHAR( 30 ) NOT NULL ,
lastname VARCHAR( 30 ) NOT NULL ,
email VARCHAR( 100 ) NOT NULL ,
location VARCHAR( 30 ) NOT NULL ,
expertise VARCHAR( 30 ) NOT NULL
) ENGINE = MYISAM ;

and update.php:

<?php

 $username = $_POST['id'];
 $firstname = $_POST['firstname'];
 $lastname = $_POST['lastname'];
 $email = $_POST['email'];
 $location = $_POST['location'];
 $expertise = $_POST['expertise'];

 mysql_connect ("localhost", "root", "yourpassword") or die(mysql_error());
 mysql_select_db ("stackoverflow");

 $query="INSERT INTO members (id,username, firstname, lastname, email, location, expertise) VALUES ('','$username', '$firstname', '$lastname', '$email', '$location', '$expertise')";

 mysql_query($query) or die (mysql_error());

 echo "Database Updated With: " .$username. " , " .$firstname. " , ".$lastname. " , ".$email. " , " .$location. " , " .$expertise ;

 ?>

Don't copy and paste it into your file..Just try to understand it..

各自安好 2024-11-25 23:52:28

不确定这个答案是否足够切题。但是,如果您是一名初级 PHP 程序员,那么学习 MySqli 或 PDO 扩展来处理 MySql 可能是明智之举,因为从 PHP 5.5.0 开始,mysql 扩展已被删除。 (https://www.php.net/mysql_query)
MySqli 扩展的使用方式与 MySql 扩展类似。
因此,除非您只是修改旧项目(即使如此,您可能需要考虑升级代码)

Not sure if this answer is on topic enough. But if you are a beginning PHP programmer it might be wise to study MySqli or PDO extension to handle MySql, because as of PHP 5.5.0 the mysql extension is removed. (https://www.php.net/mysql_query)
The MySqli extension can be used in a similar way as the MySql extension.
So unless you are just modifying an old project (and even then, you might want to consider upgrading your code)

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