1.11M Members

No Database Selected - When it is?

 
0
 

Hey all,

Selecting a MYSQL Database is throwing Errors. It's all correct. However, My page seems to think otherwise..

<?php


################### Server Connection / CONST ######################


    define('DB_NAME', 'xtrapsp_Rhino'); 
    define('DB_USER', 'xtrapsp_rhino');
    define('DB_PASSWORD', 'Password');
    define('DB_HOST', 'localhost');


##################################################################


    // Note that whatever is enclosed by $_POST[""] matches the form input elements
    // $Value_name = $_POST["Form_Name"]; 

    $value_customer_name = $_POST["customer_name"];
    $value_customer_name_letterhead = $_POST["customer_name_letterhead"];

        // Connect to our DB with mysql_connect(<server>, <username>, <password>)    
        $sql_connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        mysql_select_db(DB_NAME , $sql_connection);

        $sql = "INSERT INTO Customers(
                    customer_name,
                    customer_name_letterhead
            ) VALUES (
                '$value_customer_name',
                '$value_customer_name_letterhead'
            )";

        if (!mysql_query($sql)) {
            die('Error: ' . mysql_error());
        }    

    mysql_query(mysql_real_escape_string($sql), $sql_connection);
    mysql_close($sql_connection);
?>

As you can see the script itself is very basic. It will submit data to two rows inside a table called Customers.

Database details:

localhost - xtrapsp_Rhino - Customers

Picture:

http://i.imgur.com/pvPBP.png

It all lines up... I don't see the error.

 
0
 

Is your id column set to auto_increment ? You can add additional checks on the connect and select_db and see if those are executing correctly too.

 
0
 

I have id column named customer_id, is it wise for me to rename it to just id?

I know the database connects with those details as I did it in a seperate file..

This is pretty much a re-construction of the previous one which threw up a different error. This is just cleaner coded.

 
1
 

No, name doesn't matter. I can only see it is an int, and set to not null. If the auto_increment attribute is not specified, then you need to provide a value for it in your insert query. ANyway, this is still recommended while testing:

$sql_connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
mysql_select_db(DB_NAME , $sql_connection) or die(mysql_error);
 
0
 

Access denied for user 'xtrapsp_rhino'@'localhost' to database 'xtrapsp_Rhino'

So it's to do with the login details :P

edit: ok I got it to connect. and its auto incrementing values. But its not taking whatever is in the form field?

http://i.imgur.com/vTomQ.png

 
1
 

Echo the created query before you execute it. You are using method="post" in your form? Show your input tags.

 
0
 

I just added the method and it worked. Thanks Priaeas, You've pretty much got my template perfect for the rest of the work.

:)

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article