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.

new PDO("mysql:host=" . $this->host . ";dbname=" . $dbn, $usr, $pwd, array(

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);
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.