douping7105
2015-04-05 07:46
浏览 45
已采纳

无法使用php将数据输入数据库

I am not able to enter data into my database. Please help and let me know what's wrong with my code.

    <?php
    require("dbconn.php");
    if(isset($_POST['submit']))
    {
        $fname = $_POST['fname'];
        $lname = $_POST['lname'];
        $username = $_POST['username'];
        $password = $_POST['pass1'];
        $hash = crypt($password);
        $admin = 0;
        $sqlq = "INSERT INTO user (fname, lname, username, password, admin) VALUES('".$fname."', '".$lname."', '".$username."', '".$hash."', '".$admin."');";
        $result = mysql_query($sqlq);
        if(!$result)
        {
            die("Fatal Error: Unable to insert into database");
        }
    }
?>

dbconn.php is shown as below

    <?php
$servername = "localhost";
$susername = "root";
$spassword = "123456";
$dbname = "alphara";
$conn = mysql_connect($servername, $susername, $spassword, $dbname);
if ($conn->connect_error) {
    die("Connection failed with database! " . $conn->connect_error);
} 

?>

please help me with what is wrong. The echoed query runs successfully in phpmyadmin.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dpiw16824 2015-04-05 08:12
    已采纳

    mysql_connect will not take the database name. So try the following commands for connection.

    <?php
    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_connect_db($dbname, $link);
    echo 'Connected successfully';
    mysql_close($link);
    ?>
    
    已采纳该答案
    打赏 评论
  • dongyang2229 2015-04-05 07:58

    Can you please try this one in your dbconn.php

    <?php
    $connection = mysql_connect('localhost', 'mysql_user', '123456');
    
    mysql_select_db('alphara', $connection );
    ?>
    
    打赏 评论

相关推荐 更多相似问题