INSERT into DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mdneyet
Forum Newbie
Posts: 1
Joined: Thu Sep 11, 2008 6:58 am

INSERT into DB

Post by mdneyet »

I've gotten everything to work on my testing computer but when I moved it to my web server I've had problems.

I have success getting to my database but I can't post into the table "items" in the DB. The following code is on my processing page.

<?php
$host="localhost";
$user="xxxxx";.
$pass="xxxxx"
$dbase="XXXXX
$connect=mysql_connect($host,$user,$pass);
if (!$connect) {
mysql_close($connect);
echo "Cannot connect to the database!";
}
else {
echo "Successfully Connected to the database!";
}
?>
<?php
$catNu = $_POST['catNu'];
$title = $_POST['title'];
$player = $_POST['player'];
$team = $_POST['team'];
$description = $_POST['description'];
$price = $_POST['price'];
?>
<?php
$query = "INSERT INTO items (
catNu, title, player, team, description, price
) VALUES (
'{$catNu}', '{$title}', '{$player}', '{$team}', '{$description}', '{$price}'
)";
if (mysql_query($query, $connect)) {
// success
header("Location: MoodaManiaDESM.php");
exit;
}else {
//display message
echo "<p>Data Creation Failed!!</p>";
echo "<p>" . mysql_error() . "</p>";
}
?>
<?php mysql_close($connect); ?>

I get the following messages when trying to post info:

Successfully Connected to the database!

Data Creation Failed!!

No database selected
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: INSERT into DB

Post by Ziq »

You should select database. You can do this mysql_select_db() function

Code: Select all

 
//...
else {
echo "Successfully Connected to the database!"; 
mysql_select_db('DATABASE_NAME') or die ('Can\'t use select DB : ' . mysql_error());
}
//...
 
Post Reply