Welcome to WebmasterWorld Guest from 35.172.195.49

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Unexpected T_VARIABLE when trying to submit my form.

Designed my own form which causes this error.

     
2:05 pm on Apr 22, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2005
posts:96
votes: 0


I'm trying to create a script which sends a e-mail to the one who submitted the form and a e-mail to the webmaster (me). It also has to put the submitted info into my MySQL database. I'm just a beginner with PHP, so I don't know what could be wrong. When I try to submit the form, it gives me this error:

Parse error: parse error, unexpected T_VARIABLE in /home/example/public_html/test3/verwerken.php on line 38

Does anyone know how to solve this?

Thank you very much in advance,
Stefan.

[edited by: jatar_k at 5:31 pm (utc) on April 22, 2005]
[edit reason] no urls thanks [/edit]

2:12 pm on Apr 22, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 17, 2003
posts:76
votes: 0


Hi

Im not really a php expert - but is it because the variabe $time is named $tijd earlier in the page?

2:15 pm on Apr 22, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 17, 2003
posts:76
votes: 0


and - think there may be a ) missing.
6:13 pm on Apr 22, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2005
posts:96
votes: 0


$time was indeed wrong. Thanks for pointing that out. The error does still show up though.

edited by: jatar_k at 5:31 pm (utc) on April 22, 2005
no urls thanks

Why isn't this allowed? This way, I can't show others my PHP code so they may see a wrong command and help me out with what the problem would be...
6:21 pm on Apr 22, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2005
posts:96
votes: 0


<?
PRINT "<center>";
PRINT "Bedankt voor het bestellen van de GSM-Deurbel. U kunt het verschuldigde bedrag (80,-) overmaken
op gironummer 4338942 of via paypal naar bestellen@example.nl";
PRINT "</center><p><center><TABLE border=0 width=500";
PRINT "cellspacing=0 cellpadding=7>";
PRINT "<TR><TD><FONT SIZE=-1 face=verdana>";
PRINT "Uw gegevens:";
PRINT "$naam";
PRINT "$straat";
PRINT "$postcode";
PRINT "$plaats";
PRINT "$telefoonnummer";
PRINT "$email";
PRINT "Als deze gegevens niet kloppen, laat het ons dan z.s.m. weten (bestellen@example.nl).";
PRINT "</FONT></TD></TR></TABLE></center>";

mail("$email", "Bestelling GSM-Deurbel", "Geachte $name,\nBedankt voor uw bestelling. Als de betaling is
ontvangen, wordt het pakketje binnen 7 werkdagen bij u thuis bezorgt.\n\nU kunt het bedrag (80,-)
overmaken op onze girorekening 4338942 of via paypal naar bestellen@example.nl.\n\nUw gegevens (ter
bevestiging):\n$naam\n$straat\n$postcode\n$plaats\n$telefoonnummer\n$email\nAls deze gegevens niet
kloppen, laat het ons dan z.s.m. weten (bestellen@example.nl).");

mail("sdegraaf@example.com", "Bestelling GSM-Deurbel", "Nieuwe bestelling voor de GSM-Deurbel. Gegevens
van de klant:\n\n$naam\n$straat\n$postcode\n$plaats\n$telefoonnummer\n$email");

$ip = getenv ( 'REMOTE_ADDR' );
$tijd = date ( 'D M j' );

$host = "localhost";
$gebruiker = "narutouz_goten";
$wachtwoord = "XHoIS";
$db = "narutouz_ict";
$dbtable = "bestellen";

mysql_connect($host,$gebruiker,$wachtwoord) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$toevoegen = 'INSERT INTO $dbtable values('$naam','$straat','$postcode','$plaats','$telefoonnummer','$email','$ip','$tijd';
mysql_query($toevoegen) or die(mysql_error());
?>

[edited by: jatar_k at 6:37 pm (utc) on April 22, 2005]
[edit reason] generalized email addresses [/edit]

6:36 pm on Apr 22, 2005 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


$toevoegen = 'INSERT INTO $dbtable values('$naam','$straat','$postcode','$plaats','$telefoonnummer','$email','$ip','$tijd';

problem is the above has mismatched quotes and, as cazgh mentioned, a missing ).

try this way

$toevoegen = "INSERT INTO $dbtable values('$naam','$straat','$postcode','$plaats','$telefoonnummer','$email','$ip','$tijd')";

when the parser was encountering the second single quote in that string it was ending that string assignment so it had no idea what to do with the rest of the line.

6:49 pm on Apr 22, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2005
posts:96
votes: 0


Thanks for that code. Now I don't get the error, but all the variables which I put on a new line (or do I need two \n's after each other?) are all behind each other:

Uw gegevens:TestTeststreet144817BVTesttown5282978me@me.comAls deze gegevens niet kloppen, laat het ons dan z.s.m. weten (bestellen@example.nl).

I also get a new error message:

Column count doesn't match value count at row 1

Do you know how to solve this?

[edited by: jatar_k at 5:25 pm (utc) on April 28, 2005]
[edit reason] generalized url [/edit]

6:53 pm on Apr 22, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2005
posts:96
votes: 0


The script also didn't put anything in the MySQL database for some reason (the table is empty).
7:06 pm on Apr 22, 2005 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


>> Column count doesn't match value count at row 1

it means you aren't passing the proper number of values for your insert. You have 2 options

[dev.mysql.com...]

1. when you build your insert statement you can include a column list like so

$toevoegen = "INSERT INTO $dbtable (col_name1, col_name2, col_name3, ....) values('$value1', '$value2', '$value3', .....)";

2. you need to include every column in the table when passing an insert with out a column list

10:56 am on Apr 23, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2005
posts:96
votes: 0


Thank you very much for the help! I added the column names (I didn't know that was possible) and now it sends a e-mail to the submitter and the admin and it sends the information to the database now. I only have two problems left:

1: The information I print to the browser with the PRINT function is still put after each other (it doesn't put the submitted info of each field on a new line). How can I do this? I thought with \n but it doesn't work.

2: Also, I need some way to show the administrator all the info which is submitted on a page (older submissions at the bottom and newer at the top). How can I do this and how can I let the admin change the submitted info without using PHPMyAdmin?

2:45 am on Apr 27, 2005 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


To get data printing on a separate line you have to use html breaks. Either use a <br /> or wrap the entire output in <pre> tags.

I don't quite track on question number two ...? I don't use phpMyAdmin, but it seems as though you are asking how to display entries in the database in date-entered order?

6:55 am on Apr 27, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2005
posts:96
votes: 0


Thanks coopster! Works now. What I meant with the second question was that I want to display all rows from a table on a normal page (so admins can see what is in the database without using phpMyAdmin). I found a tutorial to do this and it seems to be working. However, I need a script to edit those entries (without phpMyAdmin. just on a normal html/php page). I found a tutorial for this aswell, but I get this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/narutouz/public_html/ict/aanpassen.php on line 27

My PHP code which should be able to do what I said above:

<? 
mysql_connect("localhost","user","pass");
mysql_select_db("narutouz_ict");

if(!isset($cmd))
{
$result = mysql_query("select * from bestellen order by ID");

while($r=mysql_fetch_array($result))
{
$titel=$r["Naam"];
$id=$r["ID"];

echo "<a href='aanpassen.php?cmd=edit&id=$id'>$titel - Aanpassen</a>";
echo "<br>";
}
}
?>
<?
if($_GET["cmd"]=="edit" $_POST["cmd"]=="edit")
{
if (!isset($_POST["submit"]))
{
$id = $_GET["ID"];
$sql = "SELECT * FROM bestellen WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>

Can you tell me what the problem is? Thanks in advance.

[edited by: coopster at 2:04 am (utc) on April 28, 2005]

[edited by: jatar_k at 5:24 pm (utc) on April 28, 2005]
[edit reason] snipped irrelevant code and user/pass [/edit]

2:08 am on Apr 28, 2005 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


I'm guessing you have something wrong with your query. Or else you don't have a valid connection. I'll often dump the query in situations like this to start troubleshooting. It probably has something to do with the variable data in $_GET["ID"];
$id = $_GET["ID"]; 
$sql = "SELECT * FROM bestellen WHERE id=$id";
exit("\$sql is: $sql");
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
2:37 pm on Apr 28, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2005
posts:96
votes: 0


I've tested it with your code and it displays this:

$sql is: SELECT * FROM bestellen WHERE ID=

But shouldn't it display the id-number behind ID=? This is my table:

CREATE TABLE `bestellen` (
`ID` int(11) NOT NULL auto_increment,
`Naam` varchar(50) default NULL,
`Straat` varchar(50) default NULL,
`Postcode` varchar(50) default NULL,
`Plaats` varchar(50) default NULL,
`Telefoonnummer` varchar(50) default NULL,
`Email` varchar(50) default NULL,
`Betaald` varchar(50) default NULL,
`ip_ad` varchar(25) default NULL,
`log_date` datetime default NULL,
PRIMARY KEY (`ID`)
) TYPE=MyISAM AUTO_INCREMENT=8 ;

I still can't figure out what would be wrong. I also tried using $sql = "SELECT * FROM bestellen WHERE ID=$id"; instead of id=$id"; which also didn't work.

4:00 pm on Apr 28, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2005
posts:96
votes: 0


I now don't get a error anymore but the fields to edit a row in the database are empty now.

I changed
$sql = "SELECT * FROM bestellen WHERE ID=$id";

To
$sql = "SELECT * FROM bestellen WHERE ID='$id'";

Is this allowed? Or doesn't it show anything because it doesn't recognise the command? Thanks.

PS: Is this correct by the way (maybe I've made a mistake somewhere so that it doesn't show the info from the database in the fields): <input type="TEXT" name="betaald" value="<?php echo $myrow["Betaald"]?>" size=30>

[edited by: dbzfyam at 4:05 pm (utc) on April 28, 2005]

4:01 pm on Apr 28, 2005 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


looking at the code I am wondering if this is correct

if (!isset($_POST["submit"]))

meaning you only want to select the id when they haven't submitted the form?

I am not sure but it seems that it should be

if(isset($_POST["submit"]))

if that test is backwards then it might be executing that code when the id isn't there yet instead of when it is. The GET id obviously isn't there so I am guessing the logic is wring somewhere.

5:16 pm on Apr 28, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2005
posts:96
votes: 0


Just tried that, but when I click on the name to edit the row, I get a blank page. What I tried to do was that if the Submit button has't been clicked (which doesn't appear on the first 2 pages, so it won't), then it should get all rows from my database. After that, it should display all fields (which I added underneath that part) and fetch all fields and automatically insert them into those fields. Then, I created a Submit button and after that one is clicked ( if ($_POST["$submit"] ), it should place everything from the form into the database. However, it doesn't display the fields from the databse in the forms' fields ( I use <?php echo $rij["Betaald"]?> for that, which should get that row and insert it ). I've searched on Google and Yahoo, but I can't find any other way either to edit rows through a PHP script.
6:47 pm on Apr 28, 2005 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


ok let's see if we can break this down then, as I see it you have 3 cases

1. no cmd sent
2. cmd sent but no submit
3. cmd sent and submit sent

I see cmd can be passed either by get or post so we will account for that.
if there are any other cases then my code won't be right ;)

so , we can try setting up a little testing to see what is blowing out. We'll test the 2 vars we care about and set them up for later use first, I like yes and no because it reads well, though you could use 1 and 0 but whatever.

$cmd = 'no'; 
$submit = 'no';
if (isset($_GET['cmd'])) {
$cmd = $_GET['cmd'];
} else if (isset($_POST['cmd'])) {
$cmd = $_POST['cmd'];
}
if (isset($_POST['submit'])) $submit = 'yes';

alright so now we have our values set up and we aren't using straight post or get values either which I don't like doing. Now we need to set up our three combinations of these values. We try to find the uniqueness in each so we don't have to test everything every time and I also try to put the most common first but I will just go with your order

if ($cmd == 'no') { 
echo '<p>cmd is no';
} else if ($submit == 'no' && $cmd == 'edit') {
echo '<p>cmd is set but submit is not there';
} else {
echo '<p>cmd is set and submit exists';
}

there are a couple of other ways to do this as well, testing for submit and then dropping into a switch on cmd but I figure I will just set up the 3 known cases for now. The final else to trap anything that doesn't pass any previous test. Obviously this is a simple test that just echos what was found but I usually start with something like this to make sure that my logic works before I add in a ton of other code and then smash my head against the keyboard for hours because of a simple logic problem. :)

If we add in the connect code above that block with some added 'or die's, that should be removed once the script is completed, and drop in the selects that you have in your original. This is my normal next step when creating a script to make sure
a) that my logic still works
b) that my selects are being created properly

we should probably get the id now as well
mysql_connect("localhost","goten","HXoSI") or die ('connect: ' . mysql_error());
mysql_select_db("narutouz_ict") or die ('selectdb: ' . mysql_error());

$cmd = 'no'; 
$submit = 'no';
$id = -1;
if (isset($_GET['cmd'])) {
$cmd = $_GET['cmd'];
} else if (isset($_POST['cmd'])) {
$cmd = $_POST['cmd'];
}
if (isset($_POST['submit'])) $submit = 'yes';
if (isset($_GET['ID']) $id = $_GET['ID'];

if ($cmd == 'no') { 
$sql = "select * from bestellen order by ID";
echo '<p>cmd is no';
echo '<br>',$sql;
} else if ($submit == 'no' && $cmd == 'edit') {
$sql = "SELECT * FROM bestellen WHERE id=" . $id;
echo '<p>cmd is set but submit is not there';
echo '<br>',$sql;
} else {
echo '<p>cmd is set and submit exists';
}

from there, if all of that works, then it is only a matter of putting your fetch_arrays back in

8:31 pm on Apr 29, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2005
posts:96
votes: 0


I tried you code to see if it works and it does see the correct command and/or submit. But it doesn't display a list of all rows in the table anymore and I couldn't get the fields to work either (so that they automatically fill out the fields with the current values). It isn't possible I believe to upload files, so you can see what I currently have (the total script)? The codes are a bit too big to post here. I don't really have a lot of knowledge regarding PHP, so I don't really know how to get it to work (I thought it would be simple to create a script like this, but either I'm doing something really wrong or it isn't as simple as I thought it was).
8:45 pm on Apr 29, 2005 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


don't worry, once you overcome the small piece that you are missing this will all click and next time it will be much more straight forward.

>> I tried you code to see if it works and it does see the correct command and/or submit

perfect, so we got the logic to work. The next step is to remove all of the echo junk I threw in there and put back all of your sql queries

some other parts could probably be optimized but that should maybe be another lesson ;)

I will just plug it back together and we will see if it works, maybe it will, maybe it won't, I added those 'or die's to your queries as well in case something else goes wrong

<? 
// db connect
mysql_connect("localhost","user","pass");
mysql_select_db("narutouz_ict");
// check and initialize
$cmd = 'no';
$submit = 'no';
$id = -1;
if (isset($_GET['cmd'])) {
$cmd = $_GET['cmd'];
} else if (isset($_POST['cmd'])) {
$cmd = $_POST['cmd'];
}
if (isset($_POST['submit'])) $submit = 'yes';
if (isset($_GET['ID']) $id = $_GET['ID'];
// done checking everything now let's do some querying
if ($cmd == 'no') {
$sql = "select * from bestellen order by ID";
$result = mysql_query($sql) or die ('cmd=no died: ' . mysql_error());
while($r=mysql_fetch_array($result)) {
$titel=$r["Naam"];
$id=$r["ID"];
echo "<a href='aanpassen.php?cmd=edit&id=$id'>$titel - Aanpassen</a>";
echo "<br>";
}
//echo '<p>cmd is no';
//echo '<br>',$sql;
} else if ($submit == 'no' && $cmd == 'edit') {
$sql = "SELECT * FROM bestellen WHERE id=" . $id;
$result = mysql_query($sql) or die ('cmd=edit died: ' . mysql_error());
$myrow = mysql_fetch_array($result);
//echo '<p>cmd is set but submit is not there';
//echo '<br>',$sql;
}
?>

I also removed the else on there because I didn't know of any code to go in there, try that

9:32 am on Apr 30, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2005
posts:96
votes: 0


I replaced

<?
mysql_connect("localhost","username","password");
mysql_select_db("database");

if(!isset($cmd))
{
$result = mysql_query("select * from bestellen order by ID");

while($r=mysql_fetch_array($result))
{
$titel=$r["Naam"];
$id=$r["ID"];

echo "<a href='aanpassen.php?cmd=edit&id=$id'>$titel - Aanpassen</a>";
echo "<br>";
}
}
?>
<?
if($_GET["cmd"]=="edit" ¦¦ $_POST["cmd"]=="edit")
{
if (!isset($_POST["submit"]))
{
$id = $_GET["ID"];
$sql = "SELECT * FROM bestellen WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>

with your code. It does display the rows from the table again, but it should only display the fields when the links are clicked (to edit them). And when I try to click on a link, it does go to the right URL, but the fields are empty (am I using the right code: <input type="TEXT" name="betaald" value="<?php echo $myrow["Betaald"]?>" size=30> to display the contents of the row?).