0

Well I am pretty much trying to create database with some table, the values in the table and check them in phpMyAdmin. I am able to create the table and database, but not the values

2.) when I add the isset $_post['submit'] variable, when I click the submit button, nothing is getting created. Is there a syntax error I am not seeing?

<html>
    <body>

        <p> welcome to my Page
            Please insert the data below
            <br>
            <br>

        <form action="input.php" method="post">
            <p> Name: <input type="text" name="name">
            <p> Age: <input type="text" name="age">
            <p> Address: <input type="text" name="address">
            <p> Email: <input type="text" name="email">

                <input type="submit" value="Send!" name="submit">
        </form>



        <?php

        if(isset($_POST['submit'])){

        //connects to the sql database
        $con = mysql_connect("localhost", "willc86", "tigers330");
        if (!$con) {
            echo 'can not connect to Database' . "<br>";
        }

        //creates the database in mySQL
        $db = mysql_query("CREATE DATABASE form");
        if (!$db) {
            echo 'Did not create database';
        }

        //select the database and connect to it. on where you want to create tables.
        mysql_select_db("form", $con);

        //create the table once "form database" is selected
        $table = "CREATE TABLE users (
        Name varchar(30),
        Age varchar(30),
        Address varchar(30),
        Email varchar(30)
        )";
         mysql_query($table,$con);



        //insert the data from the form
        $value= "INSERT INTO users (Name, Age, Address, Email)
            VALUES ('$_POST[name]','$_POST[age]','$_POST[address]','$_POST[email]')";


        mysql_query($value,$con);

        mysql_close();

        }//end of submit

        ?>

    </body>
</html>
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
willcha65
  • 49
  • 1
  • 6
  • I would suggest moving away from mysql_connect http://ca2.php.net/function.mysql-connect. Use PDO instead :) – wribit Feb 14 '14 at 21:54
  • You probably want to use mysqli in the future. The mysql extension is deprecated. – bear Feb 14 '14 at 21:55

4 Answers4

0

Your form action is input.php, is your file called input.php as well? Otherwise you'd be executing input.php when you're submitting the form instead of executing the PHP on your page.

berentrom
  • 505
  • 1
  • 5
  • 12
0

I think user willc86 don't have access rights for create databases.

In second your script is incorrect, because it run for each "user add" operation and tried create database and table.

You can create it once in phpadmin and use in your script only insert.

newman
  • 2,689
  • 15
  • 23
0

No point in highlighting particular errors here as others are unlikely to have the exact same issue. Better to just give you the tools/means to debug the issue for yourself.

Instead of:

mysql_query($table,$con);

Try:

$res = mysql_query($table,$con);
if($res === false){
    throw new Exception(mysql_error($conn));
}

Better yet, use PDO.

Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
0

First of all your code is fine if this file name is input.php. There can be few reasons, one that you have incorrect credentials second that the user does not have a right to create table, for that go to Phpmyadmin and give user all rights.

Secondly and most importantly, use Prepared statements or mysqli otherwise you are vulnerable to SQL Injection

For that do go through this post

How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Hamza
  • 1,593
  • 2
  • 19
  • 31