我无法将数据输入我的数据库。请帮忙,让我知道我的代码有什么问题。
<?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如下所示
<?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);
}
?>
请帮我解决错误。 echoed查询在phpmyadmin中成功运行。
答案 0 :(得分:1)
mysql_connect不会获取数据库名称。因此,请尝试以下命令进行连接。
<?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);
?>
答案 1 :(得分:0)
请你在dbconn.php
中试试这个<?php
$connection = mysql_connect('localhost', 'mysql_user', '123456');
mysql_select_db('alphara', $connection );
?>