简体   繁体   中英

Connecting to a database using php script

I have created a form in HTML and the action is set to a php script. I'm pretty new to php and was wondering if someone could help me out with it? 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!

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_ functions because are deprecated, use mysqli_ functions or PDO instead.

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. 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. 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_* . This functions are deprecated and instead you should use PDO or mysqli_* (they are not hard to learn, just try).

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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