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.
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);
?>
Can you please try this one in your dbconn.php
<?php
$connection = mysql_connect('localhost', 'mysql_user', '123456');
mysql_select_db('alphara', $connection );
?>