Jump to content

need help with connecting to database


kevinritt

Recommended Posts

I keep getting a 'No database selected' error when I try to enter data. Here's the code for the dbconn.php file in the connections folder:

<?php
$hostname_dbconn = "localhost";
$database_dbconn = "pickering";
$username_dbconn = "name";
$password_dbconn = "password";
$dbconn = mysql_pconnect($hostname_dbconn, $username_dbconn, $password_dbconn) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

 

and here's the query:

<?php
require_once('connections/dbconn.php');

if ($_POST['submit']) 
{ 
// data from form
$date = $_POST['date'];
$comments = $_POST['comments'];

# THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE 
$query ="INSERT INTO detention (date,comments) VALUES ('$date','$comments')";
$result = mysql_query($query) or die ("Sql Error" .mysql_error());
echo 'data entered' <br />
<meta http-equiv="refresh" content="2; url=./index.php">'; 
}
else
{
?>
rest of page ...

I tried changing the path to the dbconn from this connections/dbconn.php to this /connections/dbconn.php

still cannot get it to work

Link to comment
Share on other sites

Hi kevinritt,

 

You're not selecting the database, use the mysql_select_db function to select your database. Are you sure you want to use mysql_pconnect?  Try using mysql_connect instead, mysql_pconnect opens a persistent connection to the database.

 

Hope this helps.

Link to comment
Share on other sites

:-[ I get the dope slap for today

 

Still working on it but missing something. Here's what I changed:

$connection = mysql_connect("$dbhost","$dbuser","$dbpasswd") 
or die ("Couldn't connect to server.");

$db = mysql_select_db("$database", $connection)
or die("Couldn't select database.");

Here's the query:

$query ="INSERT INTO detention (date,comments) VALUES ('$date','$comments')";
$result = mysql_query($query) or die ('Data not entered');

Link to comment
Share on other sites

didn't mean to remove that - I added

$query ="INSERT INTO detention (date,comments) VALUES ('$date','$comments')";
$result = mysql_query($query) or trigger_error(mysql_error(),E_USER_ERROR);

I get this error: Fatal error: No database selected in C:\wamp\www\Detention\index.php on line 10

Line 10 is the result variable $result

I'm sure that it's something simple that I'm missing but I don't see it

 

Here's the dbconn file:

<? 

/* -- Configure the Variables Below --*/
$dbhost = 'localhost';
$dbuser = 'user';
$dbpasswd = 'pw';
$database = 'pickering';

/* Database Stuff, do not modify below this line */
$connection = mysql_connect("$dbhost","$dbuser","$dbpasswd") 
or die ("Couldn't connect to server.");

$db = mysql_select_db("$database", $connection)
or die("Couldn't select database.");

?>

 

 

Link to comment
Share on other sites

Your dbconn file is using short open tags, so the code in it is not being executed and you apparently have a connection being made at some point, someplace else in your code, but not the mysql_select_db();

 

Only use full <?php tags to avoid wasting your time.

Link to comment
Share on other sites

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.