0

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.

Edited by happygeek: user/pass etc SNIPPED

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by Biiim
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.