简体   繁体   English

使用php脚本连接到数据库

[英]Connecting to a database using php script

I have created a form in HTML and the action is set to a php script. 我已经创建了HTML表单,并且操作已设置为php脚本。 I'm pretty new to php and was wondering if someone could help me out with it? 我对php很陌生,想知道是否有人可以帮助我? I need to write a script to add the info from the form to a database. 我需要编写一个脚本,以将信息从表单添加到数据库。 I need to create the database and the table as well. 我还需要创建数据库和表。 I did a lot of reading on the net and I'm still unable to do it. 我在网上做了很多阅读,但仍然无法做到。 This is the script I have. 这是我的脚本。 Please tell me what mistakes I have made. 请告诉我我犯了什么错误。 Thank you for all the help. 感谢您的所有帮助。

 <?php

    $con=mysql_connect("example.com","peter","abc123","my_db");

    $sql="CREATE DATABASE user";

    if (mysql_query($con,$sql)) {
        echo "Database user created successfully";
    }

    $sql="CREATE TABLE Persons(PID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(PID),firstName CHAR(30),lastName CHAR(30),age INT, dateofbirth DATE, email CHAR(30)";

    if (mysql_query($con,$sql)) { 
       echo "connected to database"; 
    }

    $sql="INSERT INTO Persons (firstName, lastName, age, dateofbirth, email) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[dateofbirth]','$_POST[email]')";

    if (mysql_query($con,$sql)) { 
       echo "added to database"; 
    }

    mysql_close($con);

?> 

I tried all the suggested answers and still not able to do it. 我尝试了所有建议的答案,但仍然无法做到。 Can someone please provide the code to do that? 有人可以提供执行此操作的代码吗? I need to obtain data from a form and insert it into a database using php! 我需要从表单获取数据,然后使用php将其插入数据库!

Hi Try This Code, 嗨,尝试一下此代码,

   $con=mysql_connect("example.com","peter","abc123");

  $sql="CREATE DATABASE user";

  if (mysql_query($sql))
    {
        echo "Database user created successfully";
    }

1.- Don't use mysql_ 1.-不要使用mysql_ functions because are deprecated, use mysqli_ functions or PDO instead. 函数已弃用,请改用mysqli_函数或PDO。

2.- You have several error i guess, first of all you select a database my_db on the connection script, but you are created another database in the next line... it's very strange this behaviour. 2.-我猜您有几个错误,首先您在连接脚本上选择一个数据库my_db ,但是在下一行中创建了另一个数据库...这种行为非常奇怪。 If this script executes every time then you should change your code (you can't create a database and a table every time. 如果此脚本每次都执行,则应该更改代码(不能每次都创建数据库和表。

In the insert string you have an error with the post code, try this: 在插入字符串中,您的邮政编码有误,请尝试以下操作:

$sql="INSERT INTO Persons (firstName, lastName, age, dateofbirth, email) VALUES ('{$_POST['firstname']}','{$_POST['lastname']}','{$_POST['age']}','{$_POST['dateofbirth']}','{$_POST['email']}')";

Your CREATE TABLE query will fail because of syntax error. 由于语法错误,您的CREATE TABLE查询将失败。 You have to check queries results especially when next query depends on previous (and you're doing operations like creating databases/tables). 您必须检查查询结果,尤其是当下一个查询取决于上一个查询时(并且您正在执行创建数据库/表之类的操作)。

Next thing to change is mysql_* . 接下来要更改的是mysql_* This functions are deprecated and instead you should use PDO or mysqli_* (they are not hard to learn, just try). 不建议使用此功能,而应使用PDOmysqli_* (不难学习,只需尝试即可)。

And one more important change have to be done in your script. 并且必须在脚本中进行一项更重要的更改。 You're getting user input and adding it to query. 您正在获得用户输入并将其添加到查询中。 Don't do that! 不要那样做! You have to always assume that user is trying to hack you, so all inputed data have to be checked and filtered. 您必须始终假设用户正在尝试对您进行黑客攻击,因此必须检查和过滤所有输入的数据。 Also it's good to use prepared statements with such data. prepared statements与此类数据一起使用也是很好的。

if (mysql_query($con,$sql)){
            echo "Database user created successfully";
        } else {
            echo 'Error creating database - ' . mysql_error();
    }

Same thing for all your sql statements to see where you went wrong 您所有的sql语句都可以看到错误的地方

更改代码(mysql_query($sql))而不是(mysql_query($con,$sql))

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

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