...

View Full Version : PHP Trouble



Skaidon
12-20-2003, 03:20 PM
Ok, I have this code, to edit news:


<?php
$PageTitle="Skaidon Designs - Admin Panel.";
?>
<?php include ('header.php'); ?>

<?

// form not yet submitted
// display initial form with values pre-filled
if (!$submit)
{
// open database connection
$connection = mysql_connect('localhost','****','*****') or die ("Unable to connect!");

// select database
mysql_select_db(testnews) or die ("Unable to select database!");

// generate and execute query
$query = "SELECT title, newstext 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
?>
<form action="<? echo $PHP_SELF; ?>" method="POST">
<table width="96%" align="center" cellpadding="0" cellspacing="1" class="contenttable">
<tr>
<th class="contentth" align="left" height="21" valign="middle">
Title
</th>
</tr>
<tr>
<td class="contenttd" align="left" valign="top">
<input size="50" maxlength="250" type="text" name="title" value="<? echo $row->title; ?>">
</td>
</tr>
</tr>
<tr>
<th class="contentth" align="left" valign="middle">
Description
</th>
</tr>
<tr>
<td class="contenttd" align="left" valign="top">
<input name="newstext" cols="40" rows="10" value="<? echo $row->newstext; ?>" />
</td>
</tr>
<tr>
<th class="contentth" align="left" valign="middle">
<input type="Submit" name="submit" value="Update">
</th>
</tr>
</form>
</table>
<?
}
// no result returned
// print graceful error message
else
{
echo "<span class='info'>That news article could not be located in our
database.</span>";
}
}
else
{
// form submitted
// start processing it
// set up error list array
$errorList = array();
$count = 0;

// validate text input fields
if (!$title) { $errorList[$count] = "Invalid entry: Title"; $count++; }

if (!$newstext) { $errorList[$count] = "Invalid entry: News Text"; $count++; }

// check for errors
// if none found...
if (sizeof($errorList) == 0)
{
// open database connection
$connection = mysql_connect(localhost, ****, *****) or die ("Unable to connect!");

// select database
mysql_select_db(testnews) or die ("Unable to select database!");

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

// print result
echo "<span class='info'>Update successful. <a href=list.php>Go back to the main menu</a>.</span>";

// close database connection
mysql_close($connection);
}
else
{
// errors occurred
// print as list
echo "<span class='error'>The following errors were encountered: <br>";
echo "<ul>";
for ($x=0; $x<sizeof($errorList); $x++)
{
echo "<li>$errorList[$x]";
}
echo "</ul></span>";
}
}
?>

<?php include ('footer.php'); ?>

And I can't get this to work, instead of showing the news article for editing, it just shows the error text "That news article could not be located in our database."

Any help would be greatly appreciated, Im very new to PHP, so this may be solved with something very simple. :)

fimi
12-20-2003, 04:23 PM
its because you didn't submit and you have no value for ' $id ' it goes to the else statement after the HTML part.

i see no reason of that suff under if (!$submit).

i would suggest using Smarty Template Engine, its very easy and worth it...you will have clean php code and not just that you can also skin the script. Sure it takes little bit of time to learn it but really its worth it.

hope this helps,
fimi

raf
12-20-2003, 04:25 PM
I don't see $id being set anywhere
I also don't see you specifying the collection ($_POST, $_SERVER etc.) so i assume you have regiter_globals = on

I recommens turning them to off
http://www.php.net/register_globals

And then try changing
<? echo $PHP_SELF; ?>"

into

<?php echo $_SERVER['PHP_SELF']; ?>"

(don't rely on shorttags)


To make sure $id has a value when the query is ran, change your code to
$query = "SELECT title, newstext FROM news WHERE id = '$id'";
echo $query;
die();
$result = mysql_query($query) or die ("Error in query: $query. " .

and then load the page in your browser and look at the printed query
<edit>Posts crossed</edit>

fimi
12-20-2003, 04:31 PM
Originally posted by raf

To make sure $id has a value when the query is ran, change your code to
$query = "SELECT title, newstext FROM news WHERE id = '$id'";
echo $query;
die();
$result = mysql_query($query) or die ("Error in query: $query. " .

and then load the page in your browser and look at the printed query


Very useful thing to do... i use it very often when i see that there is something wrong getting the data from the db.

tip: can laso use exit; -> is same as die();

Skaidon
12-20-2003, 04:44 PM
Thanks for your help thus far.

I have ammended it, as you suggested, and you are right, the id isn't being passed over.

I have one page, that shows the news and has a link to edit, it should pass over the id number (editnews.php?id=1) is this not the correct way to do this?

Again I apologise for my "newbism", if you can point me to good articles on this, I would be greatful.

fimi
12-20-2003, 04:53 PM
yes you can do .....php?id=1

here's a good tutorial simillar to what you are doing:
click here (http://www.phpfreaks.com/tutorials/25/0.php)

and also search on google and stuff like that.


WOW Hold on....

if you are passing the id value by a link
Why do you need the (!$submit) part in your code?
you need to work that out.

here are some tutorials
http://www.hotscripts.com/PHP/Tips_and_Tutorials/News_Publishing/index.html

Skaidon
12-20-2003, 05:09 PM
OK, I have just realised that now (I was adapting some of my old code, years old :))
ight the code I have now is:


<?php
$PageTitle="Skaidon Designs - Admin Panel.";
?>
<?php include ('header.php'); ?>
<?
// open database connection
$connection = mysql_connect('localhost','****','*****') or die ("Unable to connect!");

// select database
mysql_select_db(testnews) or die ("Unable to select database!");

// generate and execute query
$query = "SELECT title, newstext FROM news WHERE id = '$id'";
echo $query;
die();
$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
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table width="96%" align="center" cellpadding="0" cellspacing="1" class="contenttable">
<tr>
<th class="contentth" align="left" height="21" valign="middle">
Title
</th>
</tr>
<tr>
<td class="contenttd" align="left" valign="top">
<input size="50" maxlength="250" type="text" name="title" value="<? echo $row->title; ?>">
</td>
</tr>
</tr>
<tr>
<th class="contentth" align="left" valign="middle">
Description
</th>
</tr>
<tr>
<td class="contenttd" align="left" valign="top">
<input name="newstext" cols="40" rows="10" value="<? echo $row->newstext; ?>" />
</td>
</tr>
<tr>
<th class="contentth" align="left" valign="middle">
<input type="Submit" name="submit" value="Update">
</th>
</tr>
</form>
</table>
<?
// form submitted
// start processing it
// set up error list array
$errorList = array();
$count = 0;

// validate text input fields
if (!$title) { $errorList[$count] = "Invalid entry: Title"; $count++; }

if (!$newstext) { $errorList[$count] = "Invalid entry: News Text"; $count++; }

// check for errors
// if none found...
if (sizeof($errorList) == 0)
{
// open database connection
$connection = mysql_connect(localhost, ****, *****) or die ("Unable to connect!");

// select database
mysql_select_db(testnews) or die ("Unable to select database!");

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

// print result
echo "<span class='info'>Update successful. <a href=list.php>Go back to the main menu</a>.</span>";

// close database connection
mysql_close($connection);
}
else
{
// errors occurred
// print as list
echo "<span class='error'>The following errors were encountered: <br>";
echo "<ul>";
for ($x=0; $x<sizeof($errorList); $x++)
{
echo "<li>$errorList[$x]";
}
echo "</ul></span>";
}

?>
<?php include ('footer.php'); ?>

And I get this error:

Parse error: parse error, unexpected $ in C:\FoxServ\www\folio-\admin\editnews.php on line 103
Can't seem to figure it out.

Thanks for the help.

fimi
12-20-2003, 05:19 PM
if the query works now you should delete the two lines you added

echo $query;

die();
.....
as for the error you get now, i see no problem on that code and the error points to an html part...unless you have not posted the whole script.

Skaidon
12-20-2003, 05:31 PM
Thanks for your help, Im re-doing some of the code, hopefully I won't have any porblems, but if I do, Ill post :)

Thanks Again Everyone.

Skaidon
12-20-2003, 05:51 PM
Right, hit a new snag now, lol, Im useless.

Anyways, here is my newish code:


<?php
$PageTitle="Skaidon Designs - Admin Panel.";
?>
<?php include ('header.php'); ?>
<?
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($datab) or die ("Unable to select database!");

// generate and execute query
$query = "SELECT title, newstext 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
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table width="96%" align="center" cellpadding="0" cellspacing="1" class="contenttable">
<tr>
<th class="contentth" align="left" height="21" valign="middle">
Title
</th>
</tr>
<tr>
<td class="contenttd" align="left" valign="top">
<input size="50" maxlength="250" type="text" name="title" value="<? echo $row->title; ?>">
</td>
</tr>
</tr>
<tr>
<th class="contentth" align="left" valign="middle">
Description
</th>
</tr>
<tr>
<td class="contenttd" align="left" valign="top">
<textarea name="newstext" cols="40" rows="10"><? echo $row->newstext; ?></textarea>
</td>
</tr>
<tr>
<th class="contentth" align="left" valign="middle">
<input type="Submit" name="submit" value="Update">
</th>
</tr>
</form>
</table>
<?
// form submitted
// start processing it
// set up error list array
if ($submit)
{
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($datab) or die ("Unable to select database!");

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

// print result
echo "<span class='info'>Update successful. <a href=index.php>Go back to the main menu</a>.</span>";

// close database connection
mysql_close($connection);
}
?>
<?php include ('footer.php'); ?>

Right, id is sent now, so that the fields are filled with the text, but, when edited and submitted, the id is no longer sent to the next part of the script, how do I go about doing this?

Thanks

Nightfire
12-20-2003, 05:59 PM
that's as you're not passing the ID in your form. Change


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

To


<form action="<?php echo $_SERVER['PHP_SELF'].'/'.$_SERVER['QUERY_STRING']; ?>" method="post">

Skaidon
12-20-2003, 06:04 PM
Now, this has the same error, but, it changes the link:

editnews.php?id=1

-- click update --

editnews.php/id=1

This now gives the same error.

Any other ideas?

Thanks

fimi
12-20-2003, 06:07 PM
Originally posted by Nightfire
To


<form action="<?php echo $_SERVER['PHP_SELF'].'/'.$_SERVER['QUERY_STRING']; ?>" method="post">
[/B]


change that to:


<form action="<?php echo $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; ?>" method="post">

he didn't hold shift when pressed the ? key on keyboard. :thumbsup:

Skaidon
12-20-2003, 06:11 PM
lol

well, now I get a weird error, but I think we are almost there now :)

Error in query: UPDATE news SET title = 'Testing the Works of monkeys', newstext = 'Well, lets hope this works, otherwise I might just cry, yes, cry. Ho Hum' NOW() WHERE id = '1'. You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOW() WHERE id = '1'' at line 1
Thats the error I get now.

Any Ideas?

Thanks.

fimi
12-20-2003, 06:14 PM
update query is wrong...
tell me the filelds you are trying to update.

Skaidon
12-20-2003, 06:16 PM
The fields are:

title and newstext

Thanks

Nightfire
12-20-2003, 06:22 PM
It's that NOW() you have in the query, it's not been set to anywhere, ta for pointing out the error I did too fimi, been a long day lol

Skaidon
12-20-2003, 06:26 PM
Ahh, what the hell was that doing there? lol, I don't know been coding all afternoon, stupid eyes.

Well, thanks, thats great, all working now :)

fimi
12-20-2003, 06:27 PM
query should look like this:


$query = "UPDATE news SET title = '$title', newstext = '$newstext' WHERE id = '$id'";

and if i were you i would add the addslashes to the $title and $newstext just in case you use ' or " when you add new news.

$newstext = addslashes($newstext); $title = addslashes($title);

then when you print you do stripslashes to take the slashes that have been added


$newstext = stripslashes($newstext);
$title = stripslashes($title);

Skaidon
12-20-2003, 06:44 PM
Thanks, will add the stripslashes to it :)

Thanks again everyone.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum