I've looked up many different answers to the question I'm asking but none have them have seemed to work and im completely confused as to why. Here the the problem: When ever I enter the information into the form on the website, I get the message Error: No database Selected. here is the code:

<?php
//move the connect to database and select database to the header place.

$dbhost = 'SNIPPED';
$dbuser = 'SNIPPED';
$dbpass = 'SNIPPED';
$mydb = 'SNIPPED';
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully! yay! ';

mysql_select_db($mydb, $conn) or die('OH BOO ERROER' . mysql_error() );

$SQL = " CREATE TABLE k (";
$SQL = $SQL . " id INT NOT NULL AUTO_INCREMENT, ";
$SQL = $SQL . " FirstName VARCHAR(50), ";
$SQL = $SQL . " JobTitle VARCHAR(75), ";
$SQL = $SQL . " Email VARCHAR(40), ";
$SQL = $SQL . " PhoneNum VARCHAR(10), ";
$SQL = $SQL . " category VARCHAR(50), ";
$SQL = $SQL . " PRIMARY KEY(id) );";
$shala = mysql_db_query($kylansdb,"$SQL",$conn);
if (!$shala)
{
die('Error: ' . mysql_error());
}
$sql="INSERT INTO k (FirstName, JobTitle, Email, PhoneNum)
VALUES
('$_POST[FirstName]','$_POST[JobTitle]','$_POST[Email]','$_POST[PhoneNum]')";


echo "1 record added";

mysql_close($conn);
?>

If you have any ideas I'm wide open to them. Also, if there are any earrors in the code that may be making the "Error: No database selected" thing happen, I would be happy to learn of them.

Recommended Answers

All 2 Replies

mysql_db_query is DEPRECATED
just use mysql_query

$shala = mysql_query($SQL);

I've looked up many different answers to the question I'm asking but none have them have seemed to work and im completely confused as to why. Here the the problem: When ever I enter the information into the form on the website, I get the message Error: No database Selected. here is the code:

<?php
//move the connect to database and select database to the header place.

$dbhost = 'SNIPPED';
$dbuser = 'SNIPPED';
$dbpass = 'SNIPPED';
$mydb = 'SNIPPED';
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
if(!$conn){
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully! yay! ';

mysql_select_db($mydb, $conn) or die('OH BOO ERROER' . mysql_error() );

$SQL = " CREATE TABLE k (";
$SQL = $SQL . " id INT NOT NULL AUTO_INCREMENT, ";
$SQL = $SQL . " FirstName VARCHAR(50), ";
$SQL = $SQL . " JobTitle VARCHAR(75), ";
$SQL = $SQL . " Email VARCHAR(40), ";
$SQL = $SQL . " PhoneNum VARCHAR(10), ";
$SQL = $SQL . " category VARCHAR(50), ";
$SQL = $SQL . " PRIMARY KEY(id) );";
$shala = mysql_db_query($kylansdb,"$SQL",$conn);
if (!$shala)
{
die('Error: ' . mysql_error());
}
$sql="INSERT INTO k (FirstName, JobTitle, Email, PhoneNum)
VALUES
('$_POST[FirstName]','$_POST[JobTitle]','$_POST[Email]','$_POST[PhoneNum]')";


echo "1 record added";

mysql_close($conn);
?>

If you have any ideas I'm wide open to them. Also, if there are any earrors in the code that may be making the "Error: No database selected" thing happen, I would be happy to learn of them.

I don't see the var $kylansdb being set anywhere in that code, is that why it errors?
Should just change to mysql_query($query,$conn); like pzuurveen said

Also you should escape them post vars to prevent mysql injection

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.