Jump to content

Recommended Posts

I can't seem to find what I am doing wrong here.  I am new to coding php and admit that there is a lot that I do not know.  I would be happy with any help anyone could give me.  The code executes down to the query and then gives me back an error of 'No Database Selected'.  I'm getting fairly desperate.  Thanks in advance for your ideas on how to fix this.

 

My database information is:

Database: nwmarria_customers

Table: email_list

Fields: id int(11) auto-increment primary key

        first_name varchar(30)

        last_name varchar(30)

        email varchar(50)

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Northwest Marriage Center - Email Subscription</title>
  <link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<div id="wrapper">
<div id="header">
<div id="logo"><a href="index.html"><img height="95" alt="NW Marriage Center" src="images/logo.gif" width="500" ></a></div>
<div id="icons"><a href="#"></a> <a href="mailto:contact@marriagecenter.com"><img height="32" alt="contact us" src="images/icon-email.gif" width="25" ></a> <a href="index.html" ><img height="33" alt="home page" src="images/icon-home.gif" width="35" ></a></div>
    <div class="clear"></div>
    <div id="menu">
  <ul>
      	<li><a href="index.html"><span>home</span></a>
        <li><a href="services.html"><span>services</span></a>
        <li><a href="events.html"><span> events</span></a>
        <li><a href="aboutus.html"><span>about us</span></a>
        <li><a href="links.html"><span>links</span></a>
        <li><a href="topics.html"><span>topics</span></a>
        <li><a href="mission.html"><span>mission</span></a>
        </li>
      </ul>
    </div>
</div>
<div id="body3">
  <p>Enter your first name, last name, and email to be added to the <strong>Northwest Marriage Center</strong> mailing list.</p>
  <p>Your privacy is important to us.  We never sell, share, or give away our email list.</p>

<?php

  error_reporting(E_ALL); 

//Set up variables    
  $db_dbase="nwmarria_customers";
  $db_host="localhost";
  $db_pass="password";
  $db_user="nwmarria_dbuser";

if (isset($_POST['submit'])) {
    $first_name = $_POST['firstname'];
    $last_name = $_POST['lastname'];
    $email = $_POST['email'];
    $output_form = 'no';

    if (empty($first_name) || empty($last_name) || empty($email)) {
      // We know at least one of the input fields is blank 
      echo 'Please fill out all of the email information.<br />';
      $output_form = 'yes';
    }
  }
  else {
    $output_form = 'yes';
  }

  if (!empty($first_name) && !empty($last_name) && !empty($email)) {
    $dbc = mysql_connect($db_host,$db_user,$db_pass,$db_dbase)
       or die('Error connecting to MySQL server.');
      
$query = "INSERT INTO email_list (id,first_name, last_name, email) VALUES (0,'$first_name','$last_name','$email')";
    mysql_query($query,$dbc)
      or die ('Data not inserted.<br />'.mysql_error());

    echo 'Customer added.';

    mysql_close($dbc);
  }

  if ($output_form == 'yes') {
?>

  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="firstname">First name:</label>
    <input type="text" id="firstname" name="firstname" /><br />
    <label for="lastname">Last name:</label>
    <input type="text" id="lastname" name="lastname" /><br />
    <label for="email">Email:</label>
    <input type="text" id="email" name="email" /><br />
    <input type="submit" name="submit" value="Submit" />
  </form>

<?php
  }
?>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/144110-error-no-database-selected/
Share on other sites

???

mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] )

 

theres no db in it!!

 

<?php
$db_dbase="nwmarria_customers";
  $db_host="localhost";
  $db_pass="password";
  $db_user="nwmarria_dbuser";

if (isset($_POST['submit'])) {
    $first_name = $_POST['firstname'];
    $last_name = $_POST['lastname'];
    $email = $_POST['email'];
    $output_form = 'no';

    if (empty($first_name) || empty($last_name) || empty($email)) {
      // We know at least one of the input fields is blank 
      echo 'Please fill out all of the email information.<br />';
      $output_form = 'yes';
    }
  }
  else {
    $output_form = 'yes';
  }

  if (!empty($first_name) && !empty($last_name) && !empty($email)) {
    $dbc = mysql_connect($db_host,$db_user,$db_pass)
       or die('Error connecting to MySQL server.');


    $db_selected = mysql_select_db($db_dbase,  $dbc );
if (!$db_selected) {
   die ('Can\'t use foo : ' . mysql_error());
}


?>

4 u concern try type  $dbname.. is better than $db_dbase

I'm not sure why the code came across that way.  It does have a database in it.  Here is the actual piece of code (I changed it to dbname as you indicated):

 

 if (!empty($first_name) && !empty($last_name) && !empty($email)) {
    $dbc = mysql_connect($db_host,$db_user,$db_pass,$dbname)
       or die('Error connecting to MySQL server.');

if (!empty($first_name) && !empty($last_name) && !empty($email)) {
    $dbc = mysql_connect($db_host,$db_user,$db_pass)
       or die('Error connecting to MySQL server.');
    mysql_select_db($dbname) or die ('Error connecting to DB: ' . $dbname);

 

Is the proper way to connect to a database.

 

mysql_select_db

 

EDIT:

Decided to add info on the mysql_connect

 

Description

resource mysql_connect ([ string $server=ini_get("mysql.default_host") [, string $username=ini_get("mysql.default_user") [, string $password=ini_get("mysql.default_password") [, bool $new_link=false [, int $client_flags=0 ]]]]] )

 

As you can see the 4th parameter is not a database, it is "new_link".

The 4th parameter of mysql_connect() is not the database. Wherever you got that code from it is incorrect. For mysql, you must execute a mysql_select_db() statement as landavia and premiso have posted or you must include the database name with your table name in all your query statements.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.