Jump to content

[SOLVED] Error in query. No database selected


mr_badger

Recommended Posts

<?php

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
?> 

Link to comment
Share on other sites

When connecting make sure you have the mysql_select_db

 

mysql_connect('host', 'user', 'password');

@mysql_select_db("db_name") or die(mysql_error);

 

be wary of this, the @ sign says that you do not want any errors brought to your attention, then you go and ask it to produce one at the end, incorrectly

 

mysql_select_db("db_name") or die("cannot connect to the database" . mysql_error());

 

Link to comment
Share on other sites

Thanks for your replies, but it still hasn't solved the problem, here is the code.

 

<?php

// includes
include('conf.php');
include('functions.php');

// form not yet submitted
// display initial form with values pre-filled

  if (!$_POST['submit'])
{
       // check for record ID
       if ((!isset($_GET['id']) || trim($_GET['id']) == ''))
{
	   die('Missing record ID');
}

           //open database connection

      $connection = @mysql_connect($host, $user, $pass) or die ('Unable to connect');

       // select database
	   
          $id = $_GET['id'];
	  
      $query = "SELECT title, content, contact FROM news WHERE id = '$id'";
	  
      $result = mysql_query($query) or die ("Error in query. " . mysql_error());

        // generate and execute query

       $id = $_GET['id'];

       $query = "SELECT title, content, contact FROM news WHERE id = '$id'";

       $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());


       // if a result is returned
	   
       if (mysql_num_rows($result) > 0)
{
	  // turn it into an object
	  
	  $row = mysql_fetch_object($result);

	 // print form with values pre-filled
?>	

  <table cellspacing="5" cellpadding="5">
  
     <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

     <input type="hidden" name="id" value="<?php echo $id; ?>">

  <tr>
  
  <td valign="top"><b><font size="-1">Title</font></b></td>
  
  <td>
  
     <input size="50" maxlength="250" type="text" name="title" value="<?php echo $row->title; ?>">

  </td>
  
  </tr>
  
  <tr>
  
  <td valign="top"><b><font size="-1">Content</font></b></td>
  
  <td>
  
 <textarea name="content" cols="40" rows="10"><?php echo $row->content; ?></textarea>

  </td>
  
  </tr>
  
  <tr>
  
  <td valign="top"><font size="-1">Contact Person</font></td>
  
  <td>
  
 <input size="50" maxlength="250" type="text" name="contact" value="<?php echo $row->contact; ?>">

  </td>
  
  </tr>
  
  <tr>
  
  <td colspan=2>
  
 <input type="Submit" name="submit" value="Update">

  </td>
  
  </tr>
  
  </form>
  
  </table>
  
<?php
}
		  // no result returned
		  // print graceful error message
		  else
		  {
		      echo '<font size=-1>That press release could not be located in our database.</font>';
}
}
else
{
              //form submitted
              //start processing it
}

?>
<?php

if (!$_POST['submit'])
{
// display initial form with values pre-filled
}
else
{

// set up error list array
$errorList = array();

$title = $_POST['title'];
$content = $_POST['content'];
$contact = $_POST['contact'];
$id = $_POST['id'];

// check for record ID
if ((!isset($_POST['id']) || trim($_POST['id']) == ''))
{
die ('Missing record ID');
}

// validate text input firlds
if (trim($_POST['title']) == '')
{
$errorList[] = 'Invalid entry: Title';
}
if (trim($_POST['content']) == '')
{
$errorList[] = "Invalid entry: Content";
}

// set default value for contact person
if (trim($_POST['contact']) == '')
{
$contact = $def_contact;
}

// check for errors
// if none found...
if (sizeof($errorList) == 0)
{

// open database connection
$connection = mysql_connect($host, $user, $pass) or die ('Unable to connect');

// select database
@mysql_select_db($db) or die ('Unable to select database');

// generate and execute query
$query = "UPDATE news SET title = '$title', content = '$content', contact = '$contact', timestamp = NOW() WHERE id = '$id'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

//print result
echo '<font size=-1>Update successful.';
echo '<a href=list.php>Go back to main menu</a>/</font>';

//close database connection
mysql_close($connection);

}
else
{

// errors occured
// print as list
echo '<font size=-1>The following errors were encountered:';
echo'<br>';
echo'<ul>';
for ($x=0; $x<sizeof($errorList); $x++)
{
echo "<li>$errorList[$x]";
}
echo '</ul></font>';
}
}
?>

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.