duanqiao1947 2013-12-16 19:26
浏览 64
已采纳

使用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, 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');
?>
  • 写回答

2条回答 默认 最新

  • dswu26846 2013-12-16 19:56
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?