0

hi all,

i had this code working yesterday (to an extent any way lol). the first part of the code seems to be working correctly an tells me i am connected to the db. but then i am getting a an error saying no db selected.

here is the code

$name = $_POST['name'];
$desc = $_POST['description'];
$price = $_POST['price'];
$image = $_POST['image'];
$prod_type = $_POST['prod_type'];
$made = $_POST['made'];
$dist = $_POST['distribute'];

$servername = "mysql5.000webhost.com";
$db = "a6382499_product";
$user_name = "a6382499_sonic";
$password = "phpfreaks1";


// create connection to db
try {
    $conn = new PDO("mysql:host=$servername;$db", $user_name, $password);
// PDO error mode set to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected to database";
}
// Set veriable to catch error created
catch(PDOException $error)
    {
    echo "Connection failed: " . $error->getMessage();
}

    echo "<br />";
try{
    $sql = "INSERT INTO Products (NAME, DESCRIPTION, PRICE, IMAGE, TYPE, MADE, DISTRIBUTE)
    VALUES ('$name', '$desc', '$price', '$image', '$prod_type', '$made', '$dist')";
    $conn->exec($sql);
    echo "New record added";
}
catch(PDOException $error)
{
    echo $sql . "<br />" . $error->getMessage();
}

here is the error message i am getting

Connected to database
INSERT INTO Products (NAME, DESCRIPTION, PRICE, IMAGE, TYPE, MADE, DISTRIBUTE) VALUES ('rssbsbsb', 'etbabzdfvaebsebeabzdfb', '55', '', '', '3tagareg', 'aregagaerg')
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected

as you can see i am getting the connected to db message but then the error says no db selected.

any help would be great thanks.

2
Contributors
2
Replies
9
Views
2 Years
Discussion Span
Last Post by kevwood
0

always such simple things destroy the code lol. thanks for the help!

updated line now looks like this and works perfect

$conn = new PDO("mysql:host=$servername;dbname=$db", $user_name, $password);

Edited by kevwood

This question has already been answered. 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.