Hi:
I have coded my script to add a news article and it does, but it gives me an error,
"Update successful. Go back to the main menu. Error in query: INSERT INTO news(slug, content, contact, timestamp) VALUES('test', 'test', 'test', NOW()). No database selected"
The data IS entered!
Here is my code:
add.php
<?
// add.php - add a new press release
?>
<?
// form not yet submitted
// display initial form
if (!isset($POST['submit']))
{
?>
<table cellspacing="5" cellpadding="5">
<form action="<? echo $SERVER['PHP_SELF']; ?>" method="POST">
<tr>
<td valign="top"><b><font size="-1">Heading</font></b></td>
<td><input size="50" maxlength="250" type="text" name="slug"></td>
</tr>
<tr>
<td valign="top"><b><font size="-1">Content</font></b></td>
<td><textarea name="content" cols="40" rows="10"></textarea></td>
</tr>
<tr>
<td valign="top"><font size="-1">Contact person</font></td>
<td><input size="50" maxlength="250" type="text" name="contact"></td>
</tr>
<tr>
<td colspan=2><input type="Submit" name="submit" value="Add"></td>
</tr>
</form>
</table>
<?
}
else
{
// includes
include("../conf.php");
include("../functions.php");
// set up error list array
$errorList = array();
$count = 0;
// validate text input fields
$slug = mysql_escape_string($POST['slug']);
$content = mysql_escape_string($POST['content']);
$contact = mysql_escape_string($_POST['contact']);
if (!$slug) { $errorList[$count] = "Invalid entry: Slug"; $count++; }
if (!$content) { $errorList[$count] = "Invalid entry: Content"; $count++; }
// set default value for contact person
if (!$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 = "INSERT INTO news(slug, content, contact, timestamp) VALUES('$slug', '$content', '$contact', NOW())";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// print result
echo "<font size=-1>Update successful. <a href=list.php>Go back to the main menu</a>.</font>";
// close database connection
mysql_close($connection);
}
else
{
// errors found
// print as list
echo "<font size=-1>The following errors were encountered: <br>";
echo "<ul>";
for ($x=0; $x<sizeof($errorList); $x++)
{
echo "<li>$errorList[$x]";
}
echo "</ul></font>";
}
}
?>
<?
// set up error list array
$errorList = array();
$count = 0;
// validate text input fields
$slug = mysql_escape_string($POST['slug']);
$content = mysql_escape_string($POST['content']);
$contact = mysql_escape_string($_POST['contact']);
if (!$slug) { $errorList[$count] = "Invalid entry: Slug"; $count++; }
if (!$content) { $errorList[$count] = "Invalid entry: Content"; $count++; }
?>
<?
// set default value for contact person
if (!$contact) { $contact = $def_contact; }
?>
<?
// check for errors
// if none found...
if (sizeof($errorList) == 0)
{
// generate and execute query
$query = "INSERT INTO news(slug, content, contact, timestamp) VALUES('$slug', '$content', '$contact', NOW())";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// print result
echo "<font size=-1>Update successful. <a href=list.php>Go back to the main menu</a>.</font>";
// snip
}
else
{
// errors found
// print as list
echo "<font size=-1>The following errors were encountered: <br>";
echo "<ul>";
for ($x=0; $x<sizeof($errorList); $x++)
{
echo "<li>$errorList[$x]";
}
echo "</ul></font>";
}
?>
conf.php
<?php
// conf.php - configuration parameters
// database configuration
$host = "localhost";
$user = "**";
$pass = "*";
$db = "_news";
$def_contact = "**";
?>
functions.php
<?php
// format MySQL DATETIME value into a more readable string
function formatDate($val)
{
$arr = explode("-", $val);
return date("l F d, Y", mktime(0,0,0, (int)$arr[1], (int)$arr[2], (int)$arr[0]));
}
?>
Can anyone look at this and see what I am missing?