Jump to content

Archived

This topic is now archived and is closed to further replies.

sofaman

isset error !!!!

Recommended Posts

Hello, I'm a newbie to PHP.

I've created a database and can view and add information stored in it via PHP.
I'm trying to modify the entries. I have the following code (mainly borrowed out the book I'm learning from)

It's coming up with a parse error on line 49, if (!isset($_GET['id'])) {
I've check every where I can think of (code and on here) and even change a few things but it always comes up with this error :(

I'm using php5.0.2 and php desginer pro 2007

[code]
// Build UPDATE query
$update = "UPDATE details SET
user='$fn', model='$ln',
engine='$en', bhp='$bn'
WHERE Id=$id";

// execute query and check for success
if (!mysqli_query($link, $update)) {
$msg = "Error updating data";
} else {
$msg = "Record Successfully updated:";

//write table row confirming data
$table_row = <<<EOR
<TR>
  <TD>$fn</TD>
  <TD>$ln</TD>
  <TD>$en</TD>
  <TD>$bn</TD>
</TR>
EOR;
  }

//if not posted, check that an id has been passed via the url
} else {
if (!isset($_GET['id'])) {
  $msg = "No user selected";
} else {
$id = $_GET['id'];

// Build and execute the query
$select = "SELECT user, model,
  engine, bhp FROM details WHERE Id=$id";
$result = mysqli_query($link, $select);


[/code]

Please help, full code can put posted or hosted as a text file if needed

thank you

Sofa

Share this post


Link to post
Share on other sites
You are missing an IF statement somewhere. You have the first IF statement that executes the querey and then there are two ELSE statements - the 2nd one being the line just before line 49.

Share this post


Link to post
Share on other sites
Thanks for the advise, I've had a look at my IF statements and they seem fine :(

I've uploaded the full code to a text file so not to waste space on here.

[url=http://www.vxbits.co.uk/mod.txt]Click Here[/url] for full code

Share this post


Link to post
Share on other sites
[code]
// Build UPDATE query
$update = "UPDATE details SET user='$fn', model='$ln', engine='$en', bhp='$bn' WHERE Id=$id";

// execute query and check for success
if (!mysqli_query($link, $update)) {
  $msg = "Error updating data";
} else {
  $msg = "Record Successfully updated:";

  //write table row confirming data
  $table_row = <<<EOR
      <TR>
        <TD>$fn</TD>
        <TD>$ln</TD>
        <TD>$en</TD>
        <TD>$bn</TD>
      </TR>
      EOR;
}

//if not posted, check that an id has been passed via the url
} else {
if (!isset($_GET['id'])) {
  $msg = "No user selected";
} else {
$id = $_GET['id'];

// Build and execute the query
$select = "SELECT user, model,
  engine, bhp FROM details WHERE Id=$id";
$result = mysqli_query($link, $select);

[/code]
check again. i started trying to indent your code a little better so i could see what's going on, and stopped when i found an } else { for no reason. you seem to have if's, else's and brackets all over the place. your last block doesnt have a closing } either. go through the code carefully, making sure that you've got these in the right place, then try again. should be like:

[code]
if (condition) {
  ...code...
} else {
  ... code
}

if (condition) {
  ...code...
} else if (condition) {
  ...code...
} else {
  ...code...
}
[/code]



cheers

Share this post


Link to post
Share on other sites
Ok, the problem is not the IF/ELSES. The problem is in the places where you do this:

[code]<?php
$table_row = <<<EOR
<TR>
  <TD>$fn</TD>
  <TD>$ln</TD>
  <TD>$en</TD>
  <TD>$bn</TD>
</TR>
EOR;
?>[/code]

I've never seen that function (?) before. But, when I comment out the first section, I got an error right after the second section that used the same EOF statement. I commented all of those lines in the page and it worked.

just do the following and you should be fine:

[code]<?php
$table_row = "<TR>
  <TD>$fn</TD>
  <TD>$ln</TD>
  <TD>$en</TD>
  <TD>$bn</TD>
</TR>";
?>[/code]

Share this post


Link to post
Share on other sites
OK, after a little research I see that your EOF use is valid - EXCEPT from what I found, the closing tag MUST be the first thing on the line. In other words, you cannot have ANY white-space (spaces, tabs, etc) before the closing EOF statement.

"[i]The closing identifier must begin in the first column of the line. Also, the identifier used must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore. [/i]"

http://us2.php.net/types.string

Share this post


Link to post
Share on other sites
[quote author=mjdamato link=topic=112598.msg457443#msg457443 date=1161790970]
Ok, the problem is not the IF/ELSES. The problem is in the places where you do this:
[/quote]
i hate to piss on your chips, but take a look at the code again. the use of IF/ELSE/brackets are EXACTLY the problem.
cheers
Mark

Share this post


Link to post
Share on other sites
Thanks for helping :D

I've re-aligned the code so it's more how [b]I[/b] was learned. It's in the link above.

While you are helping, I'm going to re-write the code from scratch without the book. That should be fun but at least I will know whats going on with the code !!!

I'd still love to know whats wrong with this code though !!

Share this post


Link to post
Share on other sites
no worries

ok, so you've re-aligned the code, but the problem is still there. without also knowing/guessing what you want to do, i cant put the ifs/elses/brackets in the right places for you.

consider this part of your code:
[code]
//if not posted, check that an id has been passed via the url
} else {
[/code]
the closing bracket, }, doesnt close an opening bracket, so is redundant.
and consider this part of your code:

[code]
  $msg = "No user selected";
} else {
[/code]
the opening { bracket does not get closed.

so as for:
[quote]
I'd still love to know whats wrong with this code though !!
[/quote]
the problem is your use of if/else/brackets. look back at my first reply - the second chunk of code i posted shows an example of how you should use if/else, etc. every opening bracket needs a closing bracket, with no exceptions.

cheers
Mark

Share this post


Link to post
Share on other sites
In a bid to solve the if/else statements I've trimmed down the code to a bit, making sure I didn't delete any IF/Else or {}. I know the code is a little messy and I hate useing parts of other peoples codeing unless I know whats going on :(

[code]
<?php
// connect to database
include 'db.php';

// has form been submitted?

if ($_POST)
{
// execute query and check for success
if (!mysqli_query($link, $update))
{
$msg = "Error updating data";
}
else
{
$msg = "Record Successfully updated:";

}

//if not posted, check that an id has been passed via the url
}
else
{
if (!isset($_GET['id']))
{
$msg = "No user selected";
}
else
{
$id = $_GET['id'];

// check that the record exits
if (mysqli_num_rows($result)<1)
{
$msg = "No user with that ID found!";
}
else
{
// set vars for the form code
$form_start="FORM METHOD=\"post\"ACTION=

// assign the results to an array

while ($row = mysqli_fetch_array($result))
{
$fn = $row['user'];
}

} // end if record exists' if

} // end if ID given in url' if

} // end if form posted' if


// close connection
mysqli_close($link);
[/code]
To help, the basics about the code.
On the HTML page previous to this one.
A user logs in and is assigned an ID in the html code ww......co.uk/mod_user.php?id=1 (I know it's not  best way of doing it but it's a start for a novice and I'll do a more secure version when I learn how)

This page takes the user id, creates a form from the database information.
The user then alters the details in the form and submits it back into the database.

I hope that helps a little as I'm pulling my hair out :(
I bet when we/I find the error it's something really daft !!

Share this post


Link to post
Share on other sites
without reading your latest fully, i have a problem with this line:

[code]
$form_start="FORM METHOD=\"post\"ACTION=
[/code]

it doesnt have a closing quote, and doesnt really resemble HTML (no opening <, no space between the method and the action, no closing > and </form> anywhere to be seen, etc. maybe i'm not understanding fully here, but things seem to be a little random and void of any real structure. kudos for sorting out all your ifs and elses, as it seems to be normal now, but things like the afformentioned will cause you more problems still.

Share this post


Link to post
Share on other sites
[quote author=redbullmarky link=topic=112598.msg457465#msg457465 date=1161794082]
[quote author=mjdamato link=topic=112598.msg457443#msg457443 date=1161790970]
Ok, the problem is not the IF/ELSES. The problem is in the places where you do this:
[/quote]
i hate to piss on your chips, but take a look at the code again. the use of IF/ELSE/brackets are EXACTLY the problem.[/quote]

Um, no. I thought the same thing UNTIL I looked at the FULL SOURCE CODE (which the author has since changed). Some of the indenting was off, but the IF/ELSE statements were properly created. The apparent problem in the code posted above was not really there as there was an if statement that was not included that came before. All the IF/ELSE's you see above are the ones in the original code.

I actually took the code the author had linked to (before he modified it) and the only problem was the use of the HEREDOC usage as I explained. When I fixed that the page had no parsing errors.

[b]sofaman[/], Did you try fixing the problems with the EOF usage as I explained?

Share this post


Link to post
Share on other sites
[code][quote author=redbullmarky link=topic=112598.msg457542#msg457542 date=1161800528]
[code]
$form_start="FORM METHOD=\"post\"ACTION=
[/code]

it doesnt have a closing quote, and doesnt really resemble HTML (no opening <, no space between the method and the action, no closing > and </form> anywhere to be seen,
[/quote]

Thank you for spotting that mistake with the opening < :( the closing </form> was cut out to show the if/else code was corrrect  ???

Ohh after all the code adjustments it's no longer the isset statement but line 77 [code]<TD COLSPAN="2">INPUT TYPE="submit"[/code]
Here's the full code after a little more house keeping

[code]
<HTML>
<HEAD>
<STYLE>
BODY
{font-family:arial;}
.error {font-weight:bold; color:#ff0000;}
</STYLE>
</HEAD>
<BODY>
<h2>Edit details:</h2>



<?php
// connect to database
include 'db.php';

// has form been submitted?

if ($_POST)
{
foreach
($_POST as $K => $V)
{$v = trim($V) ;$$K = $v;}

// Build UPDATE query
$update = "UPDATE details SET user='$fn', model='$ln', engine='$en',
bhp='$bn' WHERE Id=$id";

// execute query and check for success
if (!mysqli_query($link, $update))
{
$msg = "Error updating data";
}
else
{
$msg = "Record Successfully updated:";

//write table row confirming data
$table_row =
"<TR>
<TD>$fn</TD>
<TD>$ln</TD>
<TD>$en</TD>
<TD>$bn</TD>
</TR>";
}

//if not posted, check that an id has been passed via the url
}
else
{
if (!isset($_GET['id']))
{
$msg = "No user selected";
}
else
{
$id = $_GET['id'];

// Build and execute the query
$select = "SELECT user, model,
engine, bhp FROM details WHERE id=$id";
$result = mysqli_query($link, $select);

// check that the record exits
if (mysqli_num_rows($result)<1)
{
$msg = "No user with that ID found!";
}
else
{
// set vars for the form code
$form_start = "<FORM METHOD=\"post\" ACTION= \"" . $_SERVER['PHP_SELF'] . "\">";
$form_end =
"<TR>
<TD COLSPAN="2">INPUT TYPE="submit"
    VALUE="submit changes" /></TD>
<TD COLSPAN="2"><INPUT TYPE="reset"
    VALUE="Cancel" /></TD>
</TR>";
</form>


// assign the results to an array
while ($row = mysqli_fetch_array($result))
{
$fn = $row['user'];
$ln = $row['model'];
$en = $row['engine'];
$bn = $row['bhp'];

write table row with form fields
$table_row =
"<TR>
<TD><INPUT TYPE="text" NAME="fn"
VALUE=$fn SIZE="127"/></TD>
<TD><INPUT TYPE="text" NAME="ln"
    VALUE=$ln SIZE="127"/></TD>
<TD><INPUT TYPE="text" NAME="en"
    VALUE=$en SIZE="127"/></TD>
<TD><INPUT TYPE="text" NAME="bn"
VALUE=$bn SIZE="4"/></TD>
</TR>";
}
// end if record exists' if
}
// end if ID given in url' if
}
// end if form posted' if
}


// close connection
mysqli_close($link);

// print error/success message
echo(IsSet($msg)) ? "<div
class=\"error\">$msg</div> : "";
?>

<!-- set up the table-->
<TABLE BORDER="1" CELLPADDING="5">
<!-- Show start-of-frm code if form needed -->
<? echo (IsSet($form_start)) ? $form_start : "";
?>
<INPUT TYPE="HIDDEN" NAME="id" VALUE="<? echo $id ?>" />
  <tr>
    <th>User Name</th>
    <th>Model</th>
    <th>Engine</th>
    <th>BHP</th>
  </tr>
<!-- show appropriate table row code (none set if there errors) -->
<? echo (IsSet($table_row)) ? $table_row : "";
?>

<!-- show end-of-form code if we are displaying the form -->
<? echo (IsSet($form_end)) ? $form_end : "" ; ?>
</TABLE>

<br/> <a href="mod.php">Back to users list</a>
</BODY>
</HTML>
?>


[/code]

And a big thank you for your time :D
[/code]

Share this post


Link to post
Share on other sites
[quote author=mjdamato link=topic=112598.msg457567#msg457567 date=1161802974]

[b]sofaman[/], Did you try fixing the problems with the EOF usage as I explained?
[/quote]

I did try it, you was looking at an older code that was uploaded. Thanks for looking :D

Share this post


Link to post
Share on other sites
sofaman,

You just need to slow down and look at the formatting.

That line is part of several lines of text that you are assigning to a variable - within double quotes. You need to escape the double quotes that exist within that string. Plus you have a part of the string (</form>) that is just sitting there outside of quotes completely. Try this:

[code]<HTML>
<HEAD>
<STYLE>
BODY
{font-family:arial;}
.error {font-weight:bold; color:#ff0000;}
</STYLE>
</HEAD>
<BODY>
<h2>Edit details:</h2>



<?php
// connect to database
include 'db.php';

// has form been submitted?

if ($_POST)
{
foreach
($_POST as $K => $V)
{$v = trim($V) ;$$K = $v;}

// Build UPDATE query
$update = "UPDATE details SET user='$fn', model='$ln', engine='$en',
bhp='$bn' WHERE Id=$id";

// execute query and check for success
if (!mysqli_query($link, $update))
{
$msg = "Error updating data";
}
else
{
$msg = "Record Successfully updated:";

//write table row confirming data
$table_row =
"<TR>
<TD>$fn</TD>
<TD>$ln</TD>
<TD>$en</TD>
<TD>$bn</TD>
</TR>";
}

//if not posted, check that an id has been passed via the url
}
else
{
if (!isset($_GET['id']))
{
$msg = "No user selected";
}
else
{
$id = $_GET['id'];

// Build and execute the query
$select = "SELECT user, model,
engine, bhp FROM details WHERE id=$id";
$result = mysqli_query($link, $select);

// check that the record exits
if (mysqli_num_rows($result)<1)
{
$msg = "No user with that ID found!";
}
else
{
// set vars for the form code
$form_start = "<FORM METHOD=\"post\" ACTION= \"" . $_SERVER['PHP_SELF'] . "\">";
$form_end =
"<TR>
<TD COLSPAN=\"2\">INPUT TYPE=\"submit\"
    VALUE=\"submit changes\" /></TD>
<TD COLSPAN=\"2\"><INPUT TYPE=\"reset\"
    VALUE=\"Cancel\" /></TD>
</TR>
</form>";


// assign the results to an array
while ($row = mysqli_fetch_array($result))
{
$fn = $row['user'];
$ln = $row['model'];
$en = $row['engine'];
$bn = $row['bhp'];

write table row with form fields
$table_row =
"<TR>
<TD><INPUT TYPE=\"text\" NAME=\"fn\"
VALUE=$fn SIZE=\"127\" /></TD>
<TD><INPUT TYPE=\"text\" NAME=\"ln\"
    VALUE=$ln SIZE=\"127\" /></TD>
<TD><INPUT TYPE=\"text\" NAME=\"en\"
    VALUE=$en SIZE=\"127\" /></TD>
<TD><INPUT TYPE=\"text\" NAME=\"bn\"
VALUE=$bn SIZE=\"4\" /></TD>
</TR>";
}
// end if record exists' if
}
// end if ID given in url' if
}
// end if form posted' if
}


// close connection
mysqli_close($link);

// print error/success message
echo(IsSet($msg)) ? "<div
class=\"error\">$msg</div> : "";
?>

<!-- set up the table-->
<TABLE BORDER="1" CELLPADDING="5">
<!-- Show start-of-frm code if form needed -->
<? echo (IsSet($form_start)) ? $form_start : "";
?>
<INPUT TYPE="HIDDEN" NAME="id" VALUE="<? echo $id ?>" />
  <tr>
    <th>User Name</th>
    <th>Model</th>
    <th>Engine</th>
    <th>BHP</th>
  </tr>
<!-- show appropriate table row code (none set if there errors) -->
<? echo (IsSet($table_row)) ? $table_row : "";
?>

<!-- show end-of-form code if we are displaying the form -->
<? echo (IsSet($form_end)) ? $form_end : "" ; ?>
</TABLE>

<br/> <a href="mod.php">Back to users list</a>
</BODY>
</HTML>
?>
[/code]

Share this post


Link to post
Share on other sites
[code]<HTML>
<HEAD>
<STYLE>
BODY
{font-family:arial;}
.error {font-weight:bold; color:#ff0000;}
</STYLE>
</HEAD>
<BODY>
<h2>Edit details:</h2>



<?php
// connect to database
include 'db.php';

// has form been submitted?

if ($_POST)
{
foreach
($_POST as $K => $V)
{$v = trim($V) ;$$K = $v;}

// Build UPDATE query
$update = "UPDATE details SET user='$fn', model='$ln', engine='$en',
bhp='$bn' WHERE Id=$id";

// execute query and check for success
if (!mysqli_query($link, $update))
{
$msg = "Error updating data";
}
else
{
$msg = "Record Successfully updated:";

//write table row confirming data
$table_row =
"<TR>
<TD>$fn</TD>
<TD>$ln</TD>
<TD>$en</TD>
<TD>$bn</TD>
</TR>";
}

//if not posted, check that an id has been passed via the url
}
else
{
if (!isset($_GET['id']))
{
$msg = "No user selected";
}
else
{
$id = $_GET['id'];

// Build and execute the query
$select = "SELECT user, model,
engine, bhp FROM details WHERE id=$id";
$result = mysqli_query($link, $select);

// check that the record exits
if (mysqli_num_rows($result)<1)
{
$msg = "No user with that ID found!";
}
else
{
// set vars for the form code
$form_start = "<FORM METHOD=\"post\" ACTION= \"" . $_SERVER['PHP_SELF'] . "\">";
$form_end =
"<TR>
<TD COLSPAN=\"2\">INPUT TYPE=\"submit\"
  VALUE=\"submit changes\" /></TD>
<TD COLSPAN=\"2\"><INPUT TYPE=\"reset\"
  VALUE=\"Cancel\" /></TD>
</TR></form>";



// assign the results to an array
while ($row = mysqli_fetch_array($result))
{
$fn = $row['user'];
$ln = $row['model'];
$en = $row['engine'];
$bn = $row['bhp'];

//write table row with form fields
$table_row .=
"<TR>
<TD><INPUT TYPE=\"text\" NAME=\"fn\"
VALUE=$fn SIZE=\"127\"/></TD>
<TD><INPUT TYPE=\"text\" NAME=\"ln\"
  VALUE=$ln SIZE=\"127\"/></TD>
<TD><INPUT TYPE=\"text\" NAME=\"en\"
  VALUE=$en SIZE=\"127\"/></TD>
<TD><INPUT TYPE=\"text\" NAME=\"bn\"
VALUE=$bn SIZE=\"4\"/></TD>
</TR>";
}
// end if record exists' if
}
// end if ID given in url' if
}
// end if form posted' if
}


// close connection
mysqli_close($link);

// print error/success message
echo  (IsSet($msg)) ? "<divclass=\"error\">$msg</div>" : "";
?>

<!-- set up the table-->
<TABLE BORDER="1" CELLPADDING="5">
<!-- Show start-of-frm code if form needed -->
<? echo (IsSet($form_start)) ? $form_start : "";
?>
<INPUT TYPE="HIDDEN" NAME="id" VALUE="<? echo $id ?>" />
  <tr>
    <th>User Name</th>
    <th>Model</th>
    <th>Engine</th>
    <th>BHP</th>
  </tr>
<!-- show appropriate table row code (none set if there errors) -->
<? echo (IsSet($table_row)) ? $table_row : "";
?>

<!-- show end-of-form code if we are displaying the form -->
<? echo (IsSet($form_end)) ? $form_end : "" ; ?>
</TABLE>

<br/> <a href="mod.php">Back to users list</a>
</BODY>
</HTML>
?>[/code]

Share this post


Link to post
Share on other sites

×

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.