PDA

View Full Version : PHP Tutorial, getting error!!


tux
04-16-2007, 09:35 PM
Hi again,

Onto part 9 but I'm getting this error when trying to run the page...

Parse error: syntax error, unexpected T_ECHO in C:\wamp\www\Test\PT.php on line 20

In line 11 of the code I cant make out which character you have used between the $submit & $data variables. Also, in the if statement where it says 'or die' only the 'die' is blue, the 'or' is black, which is different to the tutorial.

Line 20 is the elseif line..

echo "One of your fields is empty!";

What am I missing.

Thanks

Paul

tux
04-16-2007, 11:39 PM
I have managed to get rid of the error so the page now runs okay but it wont insert into the DB.

When you fill in the form and submit it, it just refreshes the page and fails to update the DB.

The 'elseif' works okay if you dont fill all fields. Here is the code.....


<?php
require_once("Connections/connection.php"); // database connection
///////////////////////////////////////////
$data2 = $_POST['data2'];
$data3 = $_POST['data3'];
$data4 = $_POST['data4'];
$submit = $_POST['submit'];
///////////////////////////////////////////
if ($submit && $data2 && $data3 && $data4){
///////////////////////////////////////////
$query = sprintf("INSERT into table1 (field2, field3, field4) values ('$data2', '$data3', '$data4')");
mysql_query($query)or die(mysql_error());
///////////////////////////////////////////
}elseif($submit){
echo "One of your fields is empty!";
}
///////////////////////////////////////////
$query = sprintf("SELECT * FROM table1");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
///////////////////////////////////////////
//do {
//echo $row['field3']."<br>";
//}while ($row = mysql_fetch_array($result))

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table width="200" border="1" cellspacing="0" cellpadding="0">
<tr>
<td><div align="center">data2</div></td>
<td><div align="center">data3</div></td>
<td><div align="center">data4</div></td>
</tr>
<tr>
<td><div align="center"><input name="data2" type="text" id="data2" size="15"></td>
<td><div align="center"><input name="data3" type="text" id="data3" size="15"></td>
<td><div align="center"><input name="data4" type="text" id="data4" size="15"></td>
</tr>
<tr>
<td colspan="3"><div align="center">
<input type="submit" name="Submit" value="Submit">
<input name="submit" type="hidden" id="submit" value="1">
</div></td>
</tr>
</table>
</form>

<table width="200" border="1" cellspacing="0" cellpadding="0">
<tr>
<td><div align="center">ID1</div></td>
<td><div align="center">data2</div></td>
<td><div align="center">data3</div></td>
<td><div align="center">data4</div></td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row['field1_id'];?>
<div align="center"></div></td>
<td><?php echo $row['field2'];?>
<div align="center"></div></td>
<td><?php echo $row['field3'];?>
<div align="center"></div></td>
<td><?php echo $row['field4'];?>
<div align="center"></div></td>
</tr>
<?php }while ($row = mysql_fetch_array($result)) ?>
</table>
</body>
</html>

What am I missing??

tux
04-17-2007, 12:07 AM
Okay, I have the script working now. All I can think was wrong was I had EMS running with the DB open. Once I closed it down the script worked ok.

Do you think this was the cause!!

Onto part 10.

davidj
04-17-2007, 06:42 AM
shouldnt matter

sounds dodgy

tux
04-17-2007, 10:48 PM
Just got to the end of the last tutorial. It all works okay except it INSERTS instead of UPDATING. It INSERTS okay, it DELETES okay, the update button works and populates the fields but when I submit, it INSERTS the updated record in a new row. Heres the code.....



<?php
require_once("Connections/connection.php"); // database connection
///////////////////////////////////////////
$data2 = $_POST['data2'];
$data3 = $_POST['data3'];
$data4 = $_POST['data4'];
$submit = $_POST['submit'];
$rowUpdate = $_POST['update_id'];
$del = $_GET['del'];
$upd = $_GET['upd'];
///////////////////////////////////////////
///////////////////////////////////////////
$query = sprintf("SELECT * FROM table1 where field1_id='$upd'");
$result = @mysql_query($query);
$rowUpdate = mysql_fetch_array($result);
///////////////////////////////////////////
if ($submit && $data2 && $data3 && $data4 && ! $update_id){
///////////////////////////////////////////
$query = sprintf("INSERT into table1 (field2, field3, field4) values ('$data2', '$data3', '$data4')");
mysql_query($query)or die(mysql_error());
///////////////////////////////////////////
}elseif($submit && $update_id){
///////////////////////////////////////////
$query = sprintf("UPDATE table1 set field2='$data2', field3='$data3', field4='$data4' where field1_id = '$update_id'");
$result = @mysql_query($query);
///////////////////////////////////////////
}
//delete record
if ($del){
///////////////////////////////////////////
$query = sprintf("DELETE FROM table1 where field1_id='$del'");
mysql_query($query)or die(mysql_error());
///////////////////////////////////////////
}
///////////////////////////////////////////
$query = sprintf("SELECT * FROM table1");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
///////////////////////////////////////////
//do {
//echo $row['field3']."<br>";
//}while ($row = mysql_fetch_array($result))

?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table width="395" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="127"><div align="center">data2</div></td>
<td width="125"><div align="center">data3</div></td>
<td width="135"><div align="center">data4</div></td>
</tr>
<tr>
<td><div align="center">
<div align="center">
<input name="data2" type="text" id="data2" size="15" value="<?php echo $rowUpdate['field2'];?>">
</div></td>
<td><div align="center">
<div align="center">
<input name="data3" type="text" id="data3" size="15" value="<?php echo $rowUpdate['field3'];?>">
</div></td>
<td><div align="center">
<div align="center">
<input name="data4" type="text" id="data4" size="15" value="<?php echo $rowUpdate['field4'];?>">
</div></td>
</tr>
<tr>
<td colspan="3"><div align="center">
<input type="reset" name="Submit4" value="Reset">
<input type="submit" name="Submit" value="Submit">
<input name="submit" type="hidden" id="submit" value="1">
<input name="update_id" type="hidden" id="update_id" value="<?php echo $rowUpdate['field1_id'];?>">
</div></td>
</tr>
</table>
</form>

<table width="396" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="63"><div align="center"></div></td>
<td width="48"><div align="center">ID1</div></td>
<td width="64"><div align="center">data2</div></td>
<td width="61"><div align="center">data3</div></td>
<td width="89"><div align="center">data4</div></td>
<td width="57">&nbsp;</td>
</tr>
<?php do { ?>
<tr>
<td>
<div align="center">
<input type="button" name="Submit3" value="Update" onclick="document.location.href='PT2.php?upd=<?php echo $row['field1_id']?>'">
</div></td><td><?php echo $row['field1_id'];?>
<div align="center"></div>
<div align="center"></div></td>
<td><?php echo $row['field2'];?>
<div align="center"></div>
<div align="center"></div>
<div align="center"></div></td>
<td><?php echo $row['field3'];?>
<div align="center"></div>
<div align="center"></div>
<div align="center"></div></td>
<td><?php echo $row['field4'];?>
<div align="center"></div>
<div align="center"></div>
<div align="center"></div></td>
<td><div align="center">
<input type="button" name="Submit2" value="Delete" onClick="document.location.href='PT2.php?del=<?php echo $row['field1_id']?>'"/>
</div></td>
</tr>
<?php }while ($row = mysql_fetch_array($result)) ?>
</table>
</body>
</html>



Where is my error??? Thanks.

davidj
04-18-2007, 06:58 AM
you have defined a variable

example...

$rowUpdate = $_POST['update_id'];


change this to...

$update_id= $_POST['update_id'];


in your IF statement you have

if ($submit && $data2 && $data3 && $data4 && ! $update_id){
as you can see you are using a number of vars which you are checking is true except the last var $update_id. This is fine but you never set that var at the top of your script

tux
04-18-2007, 12:02 PM
Hi David,

I managed to find the error myself. I had been looking all through the code but not checking the variables at the top, silly me.

I just came to post to say I had found it, all please dwith myself to find your post telling what I had done wrong. Didnt think you had posted as I usually get emails when a new post arrives!!

Never mind, It all works fine now so I will go try to earn myself a DW Club badge. :wink:

tux
04-19-2007, 06:01 PM
Okay David, I give up.

I've tried all sorts of things to get the form to retain the correct info.

Rather than show me, can you just point me in the right direction coz I think it will sink in better.

If I then still cant suss it out you can show me later.

Cheers.

Paul

davidj
04-19-2007, 06:46 PM
explain the error and post the code

tux
04-19-2007, 08:11 PM
Sorry David, I thought you were up with me. I have finished all the beginner tuts and at the end of part 11 the update input form retains the original info and not the updated info. You say to try to amend the script but cant seem to suss it.

davidj
04-19-2007, 08:48 PM
i need to see your script so i can see whats wrong

cant troubleshoot code without the code

tux
04-19-2007, 09:16 PM
David,

Its the final code you wrote in tutorial part 11 here...

http://www.dreamweaverclub.com/vtm/php-mysql-apache.php

The code is.....


<?php
require_once("Connections/connection.php"); // database connection
///////////////////////////////////////////
$data2 = $_POST['data2'];
$data3 = $_POST['data3'];
$data4 = $_POST['data4'];
$submit = $_POST['submit'];
$update_id = $_POST['update_id'];
$del = $_GET['del'];
$upd = $_GET['upd'];
///////////////////////////////////////////

///////////////////////////////////////////
$query = sprintf("SELECT * FROM table1 where field1_id='$upd'");
$result = @mysql_query($query);
$rowUpdate = @mysql_fetch_array($result);
///////////////////////////////////////////
if ($submit && $data2 && $data3 && $data4 && ! $update_id){
///////////////////////////////////////////
$query = sprintf("INSERT into table1 (field2, field3, field4) values ('$data2', '$data3', '$data4')");
mysql_query($query)or die(mysql_error());
///////////////////////////////////////////
}elseif($submit && $update_id){
///////////////////////////////////////////
$query = sprintf("UPDATE table1 set field2='$data2', field3='$data3', field4='$data4' where field1_id = '$update_id'");
$result = @mysql_query($query);
///////////////////////////////////////////
}
//delete record
if ($del){
///////////////////////////////////////////
$query = sprintf("DELETE FROM table1 where field1_id='$del'");
mysql_query($query)or die(mysql_error());
///////////////////////////////////////////
}
///////////////////////////////////////////
$query = sprintf("SELECT * FROM table1");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
///////////////////////////////////////////

//do {
//echo $row['field3']."<br>";
//}while ($row = mysql_fetch_array($result))

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table width="395" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="127"><div align="center">data2</div></td>
<td width="125"><div align="center">data3</div></td>
<td width="135"><div align="center">data4</div></td>
</tr>
<tr>
<td><div align="center">
<div align="center">
<input name="data2" type="text" id="data2" size="15" value="<?php echo $rowUpdate['field2'];?>">
</div></td>
<td><div align="center">
<div align="center">
<input name="data3" type="text" id="data3" size="15" value="<?php echo $rowUpdate['field3'];?>">
</div></td>
<td><div align="center">
<div align="center">
<input name="data4" type="text" id="data4" size="15" value="<?php echo $rowUpdate['field4'];?>">
</div></td>
</tr>
<tr>
<td colspan="3"><div align="center">
<input type="reset" name="Submit4" value="Reset">
<input type="submit" name="Submit" value="Submit">
<input name="submit" type="hidden" id="submit" value="1">
<input name="update_id" type="hidden" id="update_id" value="<?php echo $rowUpdate['field1_id'];?>">
</div></td>
</tr>
</table>
</form>

<table width="396" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="63"><div align="center"></div></td>
<td width="48"><div align="center">ID1</div></td>
<td width="64"><div align="center">data2</div></td>
<td width="61"><div align="center">data3</div></td>
<td width="89"><div align="center">data4</div></td>
<td width="57">&nbsp;</td>
</tr>
<?php do { ?>
<tr>
<td>
<div align="center">
<input type="button" name="Submit3" value="Update" onclick="document.location.href='PT2.php?upd=<?php echo $row['field1_id']?>'">
</div></td><td><?php echo $row['field1_id'];?>
<div align="center"></div>
<div align="center"></div></td>
<td><?php echo $row['field2'];?>
<div align="center"></div>
<div align="center"></div>
<div align="center"></div></td>
<td><?php echo $row['field3'];?>
<div align="center"></div>
<div align="center"></div>
<div align="center"></div></td>
<td><?php echo $row['field4'];?>
<div align="center"></div>
<div align="center"></div>
<div align="center"></div></td>
<td><div align="center">
<input type="button" name="Submit2" value="Delete" onClick="document.location.href='PT2.php?del=<?php echo $row['field1_id']?>'"/>
</div></td>
</tr>
<?php }while ($row = mysql_fetch_array($result)) ?>
</table>
</body>
</html>



At the end of the video you say that there is an oversight on your part and you will give a DWC badge if we can put it right.

Had a go but cant suss it. Can you point me in the right direction so I can have a rethink.

Cheers

Paul

davidj
04-20-2007, 06:49 AM
the oversight was not critical and your script will work if you wrote it acording to the tuts

tux
04-20-2007, 06:57 AM
Yes, the script works fine. How would you fix the bug so that the new update info is retained in the input fields and not change back to the old info from the DB?

Its just a loose end from the tutorial and it would nice that the user would see what they input after it has happened when I use this knowledge on my sites in the future.

They are great tutorials David. Simple to follow and done in such a way that its easy to pick up the bones of PHP. A big thanks.

Paul

davidj
04-20-2007, 06:57 AM
ah im sorry

i have just watched the vid and know what you mean

you are the first that has come back wanting to resolve this issue

excellent

you get a small medal just for being the first

i like students like you

davidj
04-20-2007, 07:07 AM
ok

lets look at how this works

remember that the script engine processes from top to bottom performing tasks as it goes

HINT:

if the update SELECT statement which displays the data in the form fields is above the actual UPDATE statement what do you think would happen and what do you have to do to correct this...

example...

[php engine] -> reads SELECT statement and assigns vars
[php engine] -> performs UPDATE statement

the SELECT happend first so how would it know that a change took place
////////

ok

put this right...

tux
04-20-2007, 06:10 PM
Okay, I cant believe I spent ages searching for some clever way of adding script when all I had to do was copy the update SELECT statement and paste it under the UPDATE statement.

This is now working with the updated info remaining in the form fields, but is this the correct way and how you would do it??

Here is the revised code for you to check....


<?php
require_once("Connections/connection.php"); // database connection
///////////////////////////////////////////
$data2 = $_POST['data2'];
$data3 = $_POST['data3'];
$data4 = $_POST['data4'];
$submit = $_POST['submit'];
$update_id = $_POST['update_id'];
$del = $_GET['del'];
$upd = $_GET['upd'];
///////////////////////////////////////////

///////////////////////////////////////////
$query = sprintf("SELECT * FROM table1 where field1_id='$upd'");
$result = @mysql_query($query);
$rowUpdate = @mysql_fetch_array($result);
///////////////////////////////////////////
if ($submit && $data2 && $data3 && $data4 && ! $update_id){
///////////////////////////////////////////
$query = sprintf("INSERT into table1 (field2, field3, field4) values ('$data2', '$data3', '$data4')");
mysql_query($query)or die(mysql_error());
///////////////////////////////////////////
}elseif($submit && $update_id){
///////////////////////////////////////////
$query = sprintf("UPDATE table1 set field2='$data2', field3='$data3', field4='$data4' where field1_id = '$update_id'");
$result = @mysql_query($query);
///////////////////////////////////////////
}
///////////////////////////////////////////
$query = sprintf("SELECT * FROM table1 where field1_id='$upd'");
$result = @mysql_query($query);
$rowUpdate = @mysql_fetch_array($result);
///////////////////////////////////////////
//delete record
if ($del){
///////////////////////////////////////////
$query = sprintf("DELETE FROM table1 where field1_id='$del'");
mysql_query($query)or die(mysql_error());
///////////////////////////////////////////
}
///////////////////////////////////////////
$query = sprintf("SELECT * FROM table1");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
///////////////////////////////////////////

//do {
//echo $row['field3']."<br>";
//}while ($row = mysql_fetch_array($result))

?>



Thanks for all your help David.

What do suggest is my next step should be learning PHP?

Regards, Paul

davidj
04-20-2007, 06:28 PM
Okay, I cant believe I spent ages searching for some clever way of adding script when all I had to do was copy the update SELECT statement and paste it under the UPDATE statement.


Hahahahha.....

This is now working with the updated info remaining in the form fields, but is this the correct way and how you would do it??


The same way!

What do suggest is my next step should be learning PHP?


just keep plodding on..

once you get the usual database stuff down try playing with file handling like reading and writing text files

also try your hand at some PHP OOP (Classes) //<< advanced stuff here mind so take it slowly

your doing it right so just enjoy it

davidj
04-20-2007, 06:43 PM
also string manipulation

thats worth a couple of days worth of practice

tux
04-20-2007, 06:45 PM
file handling like reading and writing text files



Enlighten me!!!


also try your hand at some PHP OOP (Classes) //<< advanced stuff here mind so take it slowly


I use css styles currently for formatting text etc., is that what you mean.

also string manipulation

Enlighten me more!!!!!!

In my next project the user will need to upload images. Can you recommend a good PHP upload solution.

davidj
04-20-2007, 07:08 PM
this is the best tool and the most used tool you will ever use when developing

http://www.php.net/get/php_manual_en.chm/from/a/mirror

click the flag

tux
04-20-2007, 08:16 PM
WOW!! That lot has gone flying way over my head.

Thanks for the manual. I have downloaded it and copied it to my Desktop.

I think I will crack on and start building my brothers website and create its function with what you have taught me. When I come up with probs, as I'm sure I will, I will come calling.

Did you know of a good PHP Image Upload Solution as I am definately going to need one.

Thanks again David.

Paul

davidj
04-20-2007, 08:18 PM
i will write one for you when your ready

we will go over it step by step

only a few lines really

tux
04-20-2007, 08:23 PM
only a few lines really


REALLY!!! I thought it would be quite involved.

davidj
04-20-2007, 08:24 PM
php has built in file handlers

tux
04-20-2007, 08:40 PM
I'll look forward to it. Thanks David, your tuts and posts have stoked a fire in me for this. A great credit to you, DWC and its members should be very greatful for your input.

Talk soon.

Paul

davidj
04-20-2007, 09:01 PM
If scripting becomes a chore then thats the time to say its not your chosen path in life... You need to have an interest in it first of all then after some time you learn and start developing some nice applications

i have been involved with some huge apps and they have come with some nightmares but overall i enjoyed the process

Promise me that you will always hand code everything you do! Never use auto scripting apps as you will know that they write shite code

preach the word

tux
04-20-2007, 09:29 PM
I started building websites for fun about 10years ago, only static stuff, but have not done much for maybe the last 5 years or so.

About a year ago I joined a camera club who recently asked me to upgrade their website and make it dynamic. I had no knowledge of dynamic sites and was and was restricted to ASP with it. But still, that gave me the bug back.

Now I have my brothers website to do and later my own and then onwards. You have shown me that scripting is not such a difficult thing if you apply yourself to it.

So, I will apply myself to it and learn and see where it leads me.

Regards, Paul.