Page 1 of 2

Update Problem

Posted: Fri Jun 20, 2003 8:12 am
by Keanman
Hey guys and gals,

Ok, I'm having trouble updating my database. Right now I'm getting the error:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/html/updateForm.php on line 21

I had a problem getting a database selected with $id so I added some code and now I get the error. I'm not 100% sure how to fix this problem but I know it has something to do with the line I added . Here's the run down on what this page should do:

1 - updateForm.php - This page displays the contents of the database and puts and "Edit" link and the beggining of each record. When the user clicks on the link it brings them to update.php

2 - update.php - This page consists of text input boxes and one drop down box. So depending on which link the user clicks, it should display the contents of that record in the input boxes so the user can see what they are changing. Then they can click on update and update the database.

Here is my code:

UPDATEFORM.PHP

Code: Select all

<? 
include("header.inc"); 
include("details.inc"); 
?> 

<H2>hoose a Record to Update</H2> 
Enter a Record 
Search Database 
Create a New User<P><HR></CENTER><P> 

<? 

include("title.inc"); 

$query="SELECT * FROM assets ORDER BY location ASC"; 
$result=mysql_query($query); 
mysql_close(); 

while ($rs = mysql_fetch_array($result)) { 

print ("<TR ALIGN=CENTER><TD>Edit</TD><TD>" . $rs['itemName'] . "</TD><TD>" . $rs['serialNum'] . "</TD><TD>" . $rs['assetNum'] . "</TD><TD>" . $rs['location'] . "</TD><TD>" . $rs['description'] . "</TD><TD>" . $rs['entryDate'] . "</TD><TD>" . $rs['warranty'] . "</TD></TR>"); 

} 

include("footer.inc"); 
?>
UPDATE.PHP

Code: Select all

<? 
include("header.inc"); 
?> 

<H2>Update Database</H2><P><HR> 
<P> 
Enter a Record 
Search Database 
Create a New User<P><HR></CENTER><P> 

<? 
include("details.inc"); 

$asset = $_POST['asset']; 
$name = $_POST['name']; 
$location = $_POST['location']; 
$description = $_POST['description']; 
$warranty = $_POST['warranty']; 
$serial = $_POST['serial']; 
$id=$_GET['id']; 

$query="SELECT * FROM assets WHERE id='$id'"; 
$result=mysql_query($query); 
mysql_close(); 

print ($query); 
mysql_query($query) or die (mysql_error()); 

if(isset($_POST['btnUpdate'])) { 
$query = "UPDATE assets SET 
serialNum = '$serial', 
assetNum = '$asset', 
itemName = '$name', 
location = '$location', 
description = '$description', 
warranty = '$warranty' 
WHERE id LIKE '$id'"; 
mysql_query($query) or die (mysql_error()); 
mysql_close(); 
} 

?> 

<form method="post" action="<?php echo $PHP_SELF; ?>"> 

<TABLE BORDER=1 WIDTH=40% ALIGN="CENTER"> 
<TR ALIGN=CENTER> 
<TD>Item Name</TD><TD> 
></TD> 
</TR> 
<TR ALIGN=CENTER> 
<TD>Serial Number</TD><TD>></TD> 
</TR> 
<TR ALIGN=CENTER> 
<TD>Asset Number</TD><TD>></TD> 
</TR> 
<TR ALIGN=CENTER> 
<TD>Location</TD><TD><Select name="location" value="<? echo $rs['location'] ?>"> 
<OPTION> 
<OPTION>Baie Verte 
<OPTION>Botwood 
<OPTION>Bishop Falls 
<OPTION>Buchans 
<OPTION>Carmanville 
<OPTION>Centreville 
<OPTION>Change Islands 
<OPTION>Fogo Island 
<OPTION>Gambo 
<OPTION>Gander 
<OPTION>Gaultois 
<OPTION>Glenwood 
<OPTION>Glovertown 
<OPTION>Grand Falls 
<OPTION>Greenspond 
<OPTION>Harbour Breton 
<OPTION>Hare Bay 
<OPTION>Harry's Harbour 
<OPTION>Hermitage 
<OPTION>King's Point 
<OPTION>La Scie 
<OPTION>Lewisporte 
<OPTION>Lumsden 
<OPTION>Musgrave Harbour 
<OPTION>Norris Arm 
<OPTION>Point Leamington 
<OPTION>Robert's Arm 
<OPTION>Seal Cove 
<OPTION>Springdale 
<OPTION>St. Alban's 
<OPTION>Summerford 
<OPTION>Twillingate 
<OPTION>Wesleyville 
</SELECT></TD> 
</TR> 
<TR ALIGN=CENTER> 
<TD>Description</TD><TD>></TD> 
</TR> 
<TR ALIGN=CENTER> 
<TD>Warranty</TD><TD>></TD> 
</TR> 
</TABLE> 

<CENTER> 
 
 
</FORM></CENTER> 

<? 
include("footer.inc"); 
?>
Any suggestions would be greatly appreciated and thanks in advance:

[Admin Edit: added tags][/color]

Posted: Fri Jun 20, 2003 8:21 am
by cactus
Firstly, can you edit your post and use the BB tags to make the code "pretty", it will help us see the syntax. [Edit, cheers Admin]

Secondly, you have closed your mySQL connection after running your first query:

Code: Select all

$query="SELECT * FROM assets WHERE id='$id'"; 
$result=mysql_query($query); 
mysql_close();
You then are trying to run further processes on your database but with out a connection:

Code: Select all

print ($query); 
mysql_query($query) or die (mysql_error()); 

if(isset($_POST['btnUpdate'])) { 
$query = "UPDATE assets SET 
serialNum = '$serial', 
assetNum = '$asset', 
itemName = '$name', 
location = '$location', 
description = '$description', 
warranty = '$warranty' 
WHERE id LIKE '$id'"; 
mysql_query($query) or die (mysql_error()); 
mysql_close(); 
}
Rectify this and let the group know how you get on :)

Regards,

Posted: Fri Jun 20, 2003 8:24 am
by releasedj
I think it's because you do a mysql_close() before you fetch the data.

Code: Select all

mysql_close();

while ($rs = mysql_fetch_array($result)) {

Posted: Fri Jun 20, 2003 8:33 am
by Keanman
Ok, one question. How do I make the code stand out like that. Somebody mentioned it before as [syntax=php]and[/syntax] but it didn't work for me when i tried it.

Second, I made the suggested changes above. But I'm not sure if they worked because I'm still getting the White space error.

Posted: Fri Jun 20, 2003 8:37 am
by Keanman
Also, I know this is a mewb question. But how exactly do I connect? I know the whole process but how much of it do I need to use again? Like can I get away with just adding:

$query="SELECT * FROM assets WHERE id='$id'";
$result=mysql_query($query);

Or do I need to add all of it:

include("details.inc");
$query="SELECT * FROM assets WHERE id='$id'";
$result=mysql_query($query);

Or should I just take out the mysql_close();?

Posted: Fri Jun 20, 2003 8:38 am
by twigletmac
Keanman wrote:Ok, one question. How do I make the code stand out like that. Somebody mentioned it before as

Code: Select all

and
but it didn't work for me when i tried it.
You need to make sure that you've got BBCode enabled for the post (best to edit your profile so that it's enabled by default).

Mac

Posted: Fri Jun 20, 2003 8:46 am
by Keanman
ok, now i'm getting an unexpected T-string error on line 20.

Posted: Fri Jun 20, 2003 8:54 am
by cactus
In which file are you getting that error ?

As for your mysql_close() question, you should do this when you have finished with your connection, this is after you have run all the mysql_* methods you need to run in your application, usually at the very end of your script.

Refs:
http://www.php.net/manual/en/ref.mysql.php
http://www.php.net/manual/en/function.mysql-connect.php

Regards,

Posted: Fri Jun 20, 2003 8:58 am
by Keanman
I'm getting the error in line 20 of updateForm.php

So how many closes should I have in one page? Just the one? If not exactly what code do I use to connect again?

Posted: Fri Jun 20, 2003 9:04 am
by releasedj
You don't even need to close the connection. PHP will do it automatically when the page has finished.

Posted: Fri Jun 20, 2003 9:07 am
by cactus
As the PHP Manual says:
PHP Man wrote:Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.
Ref: http://www.php.net/manual/en/function.mysql-close.php

So you could leave it out, it's only applicable if your worried about system resources.

Ref: http://www.php.net/manual/en/language.t ... f-destruct

Can we see how you are connecting to mySQL, from the code above it does'nt seem like you are.
PHP Man wrote:Example 1. MySQL connect example

Code: Select all

<?php
    $link = mysql_connect("localhost", "mysql_user", "mysql_password")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_close($link);
?>
Regards,

Posted: Fri Jun 20, 2003 9:11 am
by Keanman
Ok, thanks for the info on close.

As for the connection its in the details.inc file. Here it is:

<?
$username="username";
$password="password";
$database="inventory";
$localhost="localhost";

mysql_connect($localhost);
@mysql_select_db($database) or die( "Unable to select database");
?>


As of right now we are not using a password or username. It still works but we had a few troubles with it and we're working them out now. I've already done an enter/delete/search page with this connection and they work.

Posted: Fri Jun 20, 2003 9:28 am
by cactus
You are using include() to pull in a lot of files in the "updateForm.php", do any of these have any PHP in them ??

Can you post the entire error message to this thread for us to have have a look at.

What is in these "included" files ?

Also, you may need to actually pass "null" values to the connection method, even if your not using a u/n and p/w:

Code: Select all

mysql_connect($localhost, '', '');
Regards,

Posted: Fri Jun 20, 2003 9:33 am
by Keanman
The includes are just simple pieces of code. They all work in all other pages.

DETAILS.INC
<?
$username="inventory";
$password="pilrb";
$database="inventory";
$localhost="localhost";

mysql_connect($localhost);
@mysql_select_db($database) or die( "Unable to select database");
?>

TITLE.INC
<TABLE BORDER=1 WIDTH=100%>
<TR ALIGN=CENTER>
<TD></TD>
<TD><B>Item Name</B></TD>
<TD><B>Serial Number</B></TD>
<TD><B>Asset Number</B></TD>
<TD><B>Location</B></TD>
<TD><B>Description</B></TD>
<TD><B>Date of Entry</B></TD>
<TD><B>Warranty</B></TD>
</TR>

and HEADER.INC and FOOTER.INC are basic opening and closing HTML tags.

And here is the error:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/html/updateForm.php on line 20

Posted: Fri Jun 20, 2003 9:36 am
by releasedj
Try removing all includes except for the details.inc.

Then see if there error still occurs. If so, can yo post the updateform.php file again with any modifications that you've made?