简体   繁体   English

使用php mysql创建数据库时出错

[英]Error creating database using php mysql

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, ie I can't even see the database in phpmyadmin. 它重定向,没有显示任何错误,但是phpmyadmin无法识别我尝试创建的数据库,即,我什至看不到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');
?>

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. 您应该检查PHP错误日志,因为您尝试执行的操作甚至都不是有效的PHP,因此与数据库相关的任何事情都将无法正常工作。

First of all, try mysql_connect() instead of mysql_connect_db. 首先,尝试使用mysql_connect()而不是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. 另外,使用mysql_error()检查您的错误,否则将很难知道数据库中发生了什么错误。

Having said that, you should read up on how to submit forms with PHP and its best practices. 话虽如此,您应该阅读如何使用PHP提交表单及其最佳实践。 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. 阅读有关如何使用Bcrypt保存用户密码的信息。

In your mysql_connect(), you have to use username of mysql. 在mysql_connect()中,必须使用mysql的用户名。 The default username of mysql is 'root'. mysql的默认用户名是“ 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脚本是:

<?php

mysql_connect('localhost','root',''); mysql_connect('localhost','root','');
$query=mysql_query('create database user'); $ query = mysql_query('创建数据库用户');
$qu=mysql_query('use user'); $ qu = mysql_query('使用用户');
if($query) 如果($查询)
{ {
echo'Database is created'; echo'创建数据库';
} }
else 其他
{ {
echo'Database is not created'; echo'未创建数据库';
} }
?> ?>

   Now the database 'user' is created. 

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

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