ashstampede
Programmer
I have a weird problem with my php page, I have three or so functions that call the database. two of the functions run and on the third I have output if an error occurred with the database. stating "No database selected"
basically what goes wrong is when the article id is sent in the url header from another page, the code doesn't successfully connect to th databse to retrieve the data.
Code:
<?php
session_start();
//redirect the user if they are not admin
if($_SESSION['user_level'] != 1 || !isset($_SESSION['user_id']) )
{
//get out of here
//redirect to the page they need to goto
$url = '[URL unfurl="true"]http://';[/URL]
$url.= $_SERVER['HTTP_HOST'];
//check for trailing slashes
if((substr($url, -1) =='/') || (substr($url,-1)=='\\'))
{
$url = substr($url,0,-1); //chop off the slashes
}//end check for slashes
$url .= 'login.php';
header("Location:". $url);
exit(); // quit the script
}//not admin or logged in
/**********************************
Generate the category list
**********************************/
function selectList()
{
require_once('../connection_string.php');
$selout = "<select name=\"up_category\">\n";
$query = "SELECT * FROM category";
$result = @mysql_query($query) OR die("ERROR: " . mysql_error());
if($result)
{
while($row = mysql_fetch_array($result,MYSQL_NUM)) //returns a record if successful
{
$selout .= "<option value=\"$row[0]\">$row[1]</option>\n";
}
}
$selout .="</select>";
mysql_close();
echo($selout);
}//end function selectList
# grab the article id from the url address bar
# fecth all the information from the database
# output it to html for display
function populate($id)
{
require_once('../connection_string.php');
$query = "SELECT * FROM article WHERE article_id =$id";
//Select the database
$result = @mysql_query($query) OR die('ERROR: ' . mysql_error());
$row = mysql_fetch_array($result);
$output = "";
if($row)
{
$output = "<input name=\"up_title\" size=\"45\" maxlength=\"150\" type=\"text\" value=\"";
$output .= $row['article_title'] ."\" />\n</p>";
$output .= "<p>\nDescription:<br /><textarea name=\"up_desc\" cols=\"45\" rows=\"3\" >";
$output .=$row['article_desc'];
$output .="</textarea>\n</p><p>\nPost:<br /><textarea name=\"up_body\" cols=\"55\" rows=\"19\" >";
$output .= $row['article_body'];
}
mysql_close();
echo($output);
}//end function
/***********************************************************
UPDATE STATEMENT, takes an article id and updates the
article where the articale id is equals
**********************************************************/
if(isset($_POST['submitted']))
{
require_once('../connection_string.php');
$errors = array();
//check if all the form entered is empty
//is title blank?
if(empty($_POST['up_artId']))
{
$errors[] = 'no article ID supplied';
}
else
{
$artId = escape_data($_POST['up_artId']);
}
if(empty($_POST['up_title']))
{
$errors[] = 'please enter a title';
}
else
{
$title = escape_data($_POST['up_title']);
}
//is description blank
if(empty($_POST['up_desc']))
{
$errors[] = 'please enter a description';
}
else
{
$desc = escape_data($_POST['up_desc']);
}
//is category empty
if(empty($_POST['up_category']))
{
$errors[] = 'please enter a category';
}
else
{
$cat = escape_data($_POST['up_category']);
}
//check if article is empty
if(empty($_POST['up_body']))
{
$errors[] = 'please enter an article body';
}
else
{
$body = escape_data($_POST['up_body']);
$body .= "\n\nEDITED ON: " .date("Y,n,j,H:i:s");
}
//is category empty
if(empty($_POST['up_action']))
{
$errors[] = 'please enter a category';
}
else
{
$action = escape_data($_POST['up_action']);
}
//if there was no errors proceed
if(empty($errors))
{
if($action=="update")
{
//insert data into artical table and current table
$query ="UPDATE article SET id_cat_art=$cat, article_title ='$title',article_desc= '$desc',article_date = NOW(),article_body = '$body' WHERE article_id =$artID";
//send to the database
$result = @mysql_query($query) OR DIE('MYSQL ERROR OCCURED' .mysql_error());
//mysql affected rows is called for statements that alter the table
//returns 0 when a delete occurs,-1 for a fail mysql query, and the
//total number of rows effected by a INSERT and only the rows UPDATED
$row = mysql_affected_rows(); //return a record, if execution occured
if($row > 0)
{
//redirect to the page they need to goto
$url = '[URL unfurl="true"]http://';[/URL]
$url.= $_SERVER['HTTP_HOST'];
//check for trailing slashes
if((substr($url, -1) =='/') || (substr($url,-1)=='\\'))
{
$url = substr($url,0,-1); //chop off the slashes
}//end check for slashes
$url .= "../index.php";
header("Location:" .$url);
mysql_close();
exit();
}//if returned rows
}//if action was update
if($action=="delete")
{
//insert data into artical table and current table
$query ="DELETE FROM article WHERE article_id ='$artID'";
//send to the database
$result = @mysql_query($query) OR DIE('MYSQL ERROR OCCURED' .mysql_error());
//mysql affected rows is called for statements that alter the table
//returns 0 when a delete occurs,-1 for a fail mysql query, and the
//total number of rows effected by a INSERT and only the rows UPDATED
$row = mysql_affected_rows(); //return a record, if execution occured
if($row > 0)
{
//redirect to the page they need to goto
$url = '[URL unfurl="true"]http://';[/URL]
$url.= $_SERVER['HTTP_HOST'];
//check for trailing slashes
if((substr($url, -1) =='/') || (substr($url,-1)=='\\'))
{
$url = substr($url,0,-1); //chop off the slashes
}//end check for slashes
$url .= "../index.php";
header("Location:" .$url);
mysql_close();
exit();
}//if returned rows
}
}//end if empty errors
else
{
$errors[] = "an error occured in the database";
//no records found
$errors[] = mysql_error() ."<br /><br />Query: " .$query;
}
//close the database connection
mysql_close();
}//form has been posted
else
{
$errors = NULL;
}
?>
</head>
<div id="content">
<div id="post">
<form action="update.php" method="post" name="updater">
<p>
Article Id:<br />
<input type="text" name="up_artId" value="<?php echo $_GET['id'];?>" />
</p>
<p>
Category:<br />
<?php
selectList();
?>
</p>
<p>
Title:<br />
<?php
$id = $_GET['id'];
populate($id);
?>
</textarea>
basically what goes wrong is when the article id is sent in the url header from another page, the code doesn't successfully connect to th databse to retrieve the data.