Forum Moderators: coopster

Message Too Old, No Replies

Help with re-submiting an edited post. Please

Getting a T_LOGICAL_OR error :(

         

php4life

6:08 pm on Aug 3, 2009 (gmt 0)

10+ Year Member



Hi, I'm relativly new to php, i'm only young, but i've taught myself of tutorial etc on the net. I'm making a website at the moment, and i have a place to upload a post (pretty standard), but i wanted to make it so i could edit a post, so i built in user unique links on the posts, so when logged in a user could edit any post that belonged to them. This links to a new edit.php page (i also send over the posts 'id' which is set up as a primary key and auto incremental field in my db) I did this so i could echo out all of the relavent information from that post back into the form, so it can be edited. I got that to work fine. But my problem now is that i tried to resend the edited post back to the DB using the UPDATE mysql function. For that i basically just took my script for posting the form originally, and adapted it so it would resend the edited post. However, now i'm getting an error at my while statement, which previously worked perfectly. I dont know why, but apparently it is something to do with that i am attempting to perform the while loop regardless of whether ID is set or not but i am only running the query if the id is set. If my id isn't set the query isn't ran, but i'm running the while loop anyway. I kind of understand what that means, but i'm a bit of a loss as to how to fix it. WOndering if anyone could help a youngster out please? this is my code for you to see (the probable many errors,I'm still learning though, so i dnt mind)

<?php

$connect = mysql_connect("***********************") or die ("Couldn't connect");
mysql_select_db("********************") or die ("Couldn't Find DB");

$id = $_GET['id'];

if ($id)

$edits = mysql_query("SELECT * FROM ads WHERE id='$id'");

while($row = mysql_fetch_assoc($edits))


{
$title = $row['title'];
$category = $row['category'];
$ads = $row['ads'];
$location = $row['location'];
$body = $row['body'];
$contact = $row['contact'];
$email = $row['email'];
$telephone = $row['telephone'];
$date = $row['date'];
$author = $row['author'];





}


?>

<?php

If ($_POST['post'])

{

$title = $_POST['title'];
$category = $_POST['category'];
$ads = $_POST['ads'];
$location = $_POST['location'];
$body = $_POST['body'];
$contact = $_POST['contact'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$end = date("Y-m-d", mktime(0,0,0,$month,$day,2009));
$date = $_POST['date'];
$day = $_POST['day'];
$month = $_POST['month'];
$author = $_POST['author'];


if ($title&&$category&&$ads&&$location&&$body&&$contact&&$email&&$end)
{
mysql_connect("**********************") or die (mysql_error());
mysql_select_db("**************") or die (mysql_error());

$date = date("Y-m-d");
$end = date("Y-m-d", mktime(0,0,0,$month,$day,2009));
$day = $_POST['day'];
$month = $_POST['month'];
$author = $_SESSION['username'];
$id = $_GET['id'];







$insertedit = mysql_query("UPDATE INTO ads SET WHERE `id`='$id' ('', '$title','$category','$ads','$location','$body','$contact','$email','$telephone','$end','$date','$author')") or die(mysql_error());

die("WOW! Wasn't that Easy!");
}
else
echo "Please complete all fields<p>";


}

?>

My form is underneath here and within it i am echoing out the original post back into the various fields (such as title,body etc) so they can be edited. As i said this was working fine, until i tried to post the edited version back to my DB. But now i get a T_LOGICAL_OR error at line 66 which is my while($row = mysql_fetch_assoc($edits))statement.

Thank you

smatts9

8:06 pm on Aug 3, 2009 (gmt 0)

10+ Year Member



After scanning it quickly, shouldn't there be an "{" following your "if($id)" and then somewhere a "}" to conclude the if?

php4life

8:21 pm on Aug 3, 2009 (gmt 0)

10+ Year Member



"After scanning it quickly, shouldn't there be an "{" following your "if($id)" and then somewhere a "}" to conclude the if?"

In short, unfortunately i dont know, i did try what you said to do and i still get the T_LOGIC error at the while loop line :(

smatts9

8:28 pm on Aug 3, 2009 (gmt 0)

10+ Year Member



Try getting rid of the "if ($id)" from the original and see if it works.

php4life

8:54 pm on Aug 3, 2009 (gmt 0)

10+ Year Member



I did have a try at that before, but it didnt work, i did just try it again, but it still made my while loop error a T_LOGICAL. :(

Its like the last thing I need to do really, apart from some asthetics tidying on my site, so its so frustrating

idfer

10:04 pm on Aug 3, 2009 (gmt 0)

10+ Year Member



Well the code as you've posted compiles fine. T_LOGICAL_OR is the parser's name for "or" as in

mysql_blah_blah(...) or die (...);

It's common to get a "syntax error, unexpected T_LOGICAL_OR" if you have a semi-colon before the "or die" part, e.g.:

mysql_blah_blah(...); or die (...);

There might also be some strange hex character that's invisible in your text editor. So check your code 1-2 lines up and down from the line number in the error message.

If you still can't find anything, post the full text of the error message, it'll make it easier to analyze the problem.

Hope this helps. [a moderator will soon be along to welcome you to this board :-)]

eelixduppy

12:00 am on Aug 4, 2009 (gmt 0)



I can't actually tell you exactly what is wrong with the code up above that would produce the error you are getting, but I'm sure it's something small I'm overlooking. In any case there are some logical errors in there and I have come up with a quick revision of your script that you should be able to learn from. Here it is...

<?php

/* make sure there is an ID set */
if(!isset($_GET['id']))
{
echo 'You need specify an ID to view this page.';
exit;
} else {
$id = (int)$_GET['id'];
}

/* only need to connect once, here */
$connect = mysql_connect("***********************") or die ("Couldn't connect");
mysql_select_db("********************") or die ("Couldn't Find DB");

/*
* If we need to show the edit form
* example URL: http:www.example.com/edit.php?op=edit&id=123
*/
if(isset($_GET['op']) && $_GET['op'] == 'edit')
{
$edits = mysql_query("SELECT * FROM ads WHERE id='$id'");

/* This takes the array index and makes it a variable ie ($row['example'] --> $example) */
extract(mysql_fetch_assoc($edits), EXTR_OVERWRITE);
}
else if(isset($_POST['submit_edit']))
{
/* the fields you want to check */
$check_fields = array(
'title',
'category',
'ads',
'location',
'body',
'contact',
'email',
'telephone',
'date',
'day',
'month',
'author'
);


$missing_fields = array();

foreach($check_fields AS $field)
{
if(isset($_POST[$field]))
{
if(empty($_POST[$field]))
{
$missing_fields[] = $field;
}

} else {
$missing_fields[] = $field;
}
}

/* if there are missing fields, print them */
if(count($missing_fields) > 0)
{
echo 'Missing the following fields: ' . implode(',', $missing_fields);
}
/* otherwise update the database table */
else
{
$title = $_POST['title'];
$category = $_POST['category'];
$ads = $_POST['ads'];
$location = $_POST['location'];
$body = $_POST['body'];
$contact = $_POST['contact'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$date = $_POST['date'];
$day = $_POST['day'];
$month = $_POST['month'];
$author = $_POST['author'];

$end = date("Y-m-d", mktime(0, 0, 0, $month, $day, 2009));

$author = $_SESSION['username'];

$query = "
UPDATE ads
SET
`title` = '$title',
`category` = '$category',
`ads` = '$ads',
`location` = '$location',
`body` = '$body',
`contact` = '$contact',
`email` = '$email',
`telephone` = '$telephone',
`end` = '$end',
`date` = '$date',
`author` = '$author'
WHERE `id` = '$id'
";

/* make update */
$insertedit = mysql_query($query) or die(mysql_error());

/* see if it worked */
if($insertedit)
{
echo 'Update was successful.';
} else {
echo 'Update failed.';
}
}
?>

I hope that gets you further into your learning process. Make sure to look up any functions you don't understand at [php.net...] and let us know if you are still having issues with this new script. I have no actually tested it so try it out and see where it brings you.

So Welcome to WebmasterWorld and good luck! :)

php4life

4:41 pm on Aug 4, 2009 (gmt 0)

10+ Year Member



Thanks very much for going to the trouble, to look at this for me, I shall try and implement it now. :)

php4life

4:54 pm on Aug 4, 2009 (gmt 0)

10+ Year Member



Just briefly, i'm gettin this error on line 28 which is <b>Warning: Invalid argument supplied for foreach() </b>

which relates to this : ($check_fields AS $field)

I'm still looking at fixing this, but your experience may allow you to help me much faster.

:)