0

I wrote this page to receive from a signup page. It redirects, doesn't show any error but phpmyadmin doesn't recognize the database i tried creating, i.e I can't even see the database in phpmyadmin. Whats wrong?

<html>
<body>
<?

    $con = mysql_connect_db("localhost", "user", "");
    $create_db = mysql_query('CREATE DATABASE USER', $con);
    if($create_db)
    {
        echo "wored";
    }

    $dbcreate = mysql_query('CREATE DATABASE USER',$con);
    mysql_select_db($dbcreate);

    $create = "CREATE TABLE USER1 (userid INT(20) AUTOINCREMENT NOT NULL, username VARCHAR(15) PRIMARY KEY NOT NULL, password VARCHAR(15) NOT NULL)";

    mysql_query($create);

    $query="INSERT INTO USER1(username,password) VALUES('$_POST[username]', '$_POST[password]');
    $result=mysql_query($query);

    mysql_close($con);

?>

<?php
    header('location:loginpage.html');
?>
Kamiccolo
  • 7,758
  • 3
  • 34
  • 47
Samuel Agbede
  • 380
  • 1
  • 4
  • 14
  • You're making a number of extremely serious mistakes here. Why aren't you using a [development framework](http://codegeekz.com/best-php-frameworks-for-developers/)? This would make it a lot harder to trip up over basic things like [proper SQL escaping](http://bobby-tables.com/php) to avoid **severe** [SQL injection bugs](http://bobby-tables.com/) caused by recklessly inserting `$_POST` data into queries, not properly hashing user passwords, and using the woefully obsolete `mysql_query` interface. – tadman Feb 22 '14 at 04:11
  • In other words "Whats wrong?", pretty much everything. Starting from creating the database and table here. – Tero Lahtinen Feb 22 '14 at 07:22
  • are you using Cpanel to host your site? if so Cpanel doesnt allow you to create databases like that see my answer here for the way to do it: http://stackoverflow.com/questions/6960630/php-script-automatically-creating-mysql-database/21175022#21175022 – Lan Jan 16 '14 at 23:31

2 Answers2

0

You should check your PHP error logs as what you are trying to do is not even valid PHP so there is no way anything relating your database will work.

First of all, try mysql_connect() instead of mysql_connect_db. Also make sure your database user has the correct privileges (meaning that they can actually create a database).

Also, check your errors with mysql_error(), otherwise it will be difficult to know what failed in the database.

Having said that, you should read up on how to submit forms with PHP and its best practices. Creating the user table after submitting the form is definitely not something you want to do. Create your table first and then insert rows into it as the users sign up. Also, you never want to store the user's password. Read up on how to use Bcrypt to save user passwords.

Community
  • 1
  • 1
Julian
  • 8,808
  • 8
  • 51
  • 90
0

In your mysql_connect(), you have to use username of mysql. The default username of mysql is 'root'. So the connect should be like this

mysql_connect('localhost','root','');

I fell your concept is wrong. Each time user comes to your signup page, it comes to this page. So each time the query 'create database user' is run. There is no need to run this query each time. So your first create database. Then retrieve values from signup page.

The php script for create database is:

<?php

mysql_connect('localhost','root','');
$query=mysql_query('create database user');
$qu=mysql_query('use user');
if($query)
{
echo'Database is created';
}
else
{
echo'Database is not created';
}
?>

   Now the database 'user' is created. 
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
John
  • 723
  • 1
  • 10
  • 12