使用 PHP 更新数据库时出错
我目前正在尝试使用 PHP 将数据插入 MySQL 数据库。为了提供帮助,我正在关注 这个教程。
到目前为止,我已采取以下步骤:
- 通过 phpMyAdmin 在我的 WAMPSERVER 上手动创建一个名为“core_group_me”的数据库
创建一个名为“form.php”的表单页面(代码如下):
<代码> <头> <标题> <正文>
创建了一个用于更新“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在下面的行中,使用您的 mysql 用户名而不是 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.
Eg:
and also edit this line:
into:
我相信 Francois 提到了 sql 中的错误。为了避免只收到死消息,您可以添加类似 http://php.net 的内容/manual/de/function.mysql-error.php。
由于应用程序安全性变得越来越重要,您应该转义字符串并检查输入的内容。我也不会向用户显示错误消息,我个人会将其写入日志文件
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.
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
使用以下结构创建数据库:
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:
不要将其复制并粘贴到您的文件中..只需尝试理解它..
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:
Don't copy and paste it into your file..Just try to understand it..
不确定这个答案是否足够切题。但是,如果您是一名初级 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)