Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP insert error - no database selected 1

Status
Not open for further replies.

malibu65k

Programmer
Sep 27, 2004
131
US
Not sure what I'm doing wrong. What does it mean by "No database selected" My code is telling what database to insert data to.
It's spelled correctly.
Here's the code...

<?php
$con = mysql_connect("localhost","admin","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

$DatabaseName = 'mypatterns';
mysql_select_db($DatabaseName, $con);

$PatternId=!isset($_POST['PatternId']);
$PatternNo=!isset($_POST['PatternNo']);
$Company=!isset($_POST['Company']);
$Type=!isset($_POST['Type']);
$Description=!isset($_POST['Description']);

$sql="INSERT INTO patterns (PatternId, PatternNo, Company, Type, Description)
VALUES
($PatternId,$PatternNo,$Company,$Type,'$Description')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)
?>

Thank you in advance for any help provided, it's greatly appreciated.
 
Why don't you debug a bit more, see why mysql is not selecting the DB.

Code:
mysql_select_db($DatabaseName, $con)[red]or die(mysql_error());[/red];

Hopefully that will provide some more info.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thanks! I had to add Privedges and now I get

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1,,,'')' at line 3

I'm using MySQL 5.5.8

line 3 is this...

if (!$con)

Any ideas?!

Thanks!

:)
 
The Error refers to your actual query now. Not your PHP code.

However I'm pretty user its choking because you are attempting to insert empty values. So all the query sees is a bunch of commas.
This:
Code:
$PatternId=!isset($_POST['PatternId']); 
$PatternNo=!isset($_POST['PatternNo']); 
$Company=!isset($_POST['Company']); 
$Type=!isset($_POST['Type']);
$Description=!isset($_POST['Description']);

Does not do what you think it does. You are simply setting your variables ($PatternId, $PatternNo etc...) to the opposite of whatever isset returns. Since the isset returns either true or false and not an actual value all your variables end up being basically boolean false.

If you want to usea shorthand if statemtn it would be sort of like this:

Code:
$PatternId=([red]isset($_POST['PatternId'][/red])? [COLOR=#EEEEEE #444444]$_POST['PatternId'][/color] : '[blue]Empty[/blue]');

Where the red section is your comparison, the gray section is the value you want to assign if your comparison is true, and the blue section is the value to assign if its false.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown. 

Behind the Web, Tips and Tricks for Web Development. 
[URL unfurl="true"]http://behindtheweb.blogspot.com/[/URL]
 
It should be, otherwise it would not complain about the query, but about not being able to connect to the DB.

A I pointed out above the most likely reason for the error, is the attempt at the comparisons of the $_POST variables.

He ends up with an insert statement with a bunch of commas.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Good day,

A nice way to do this type of connection is instead of :

$con = mysql_connect("localhost","admin","");

$sql="INSERT INTO patterns (PatternId, PatternNo, Company, Type, Description)
VALUES
($PatternId,$PatternNo,$Company,$Type,'$Description')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)

Use :

$mysqli = new MySQLi(“ServerName”, “username”, “password”, “databaseName”);
$mysqli->query("INSERT INTO patterns (PatternId, PatternNo, Company, Type, Description) VALUES (“.$PatternId.”,”.$PatternNo.”,”.$Company.”,”.$Type.”,'”.$Description.”');");
$mysqli->close();

First in PHP 5 and up the MySQLi I find a lot faster/more Convenient and doing it this way is you make the connection do the SQL and close it up quickly no hanging in there longer than needed(this helps with a that has a lot of traffic).
 
personally I find the mysqli extension very considerably slower than the original mysql extension.
further if you are going to use the mysqli extension that you really ought to take advantage of the placeholder methods.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top