...

View Full Version : Having a problem with my first PHP script



Democrazy
09-07-2011, 07:29 AM
Previously, the PHP codebase I was using was that written by others, and I used their code to tinker with so I could get an understanding how PHP works.
Now that I am comfortable using PHP, I have written my own short PHP script from scratch with the aim of it seeing my newsletter system functioning at minimal, for the first time.
The newsletters system is made with HTML/PHP/MySQL.

I am having a problem with the PHP side of things where I am getting an error:

PHP Parse error: syntax error, unexpected T_IF on line 50
... which is:

if $_POST['action'] == 'Register' {

I am hoping someone that knows allot about PHP could take a look at my code and see whats wrong with it?

PHP code within confirm.html:

<?php
$link = mysql_connect('localhost', 'testusr', 'testpw');
mysql_select_db('testdb', $link);
$email = $_POST['e-mail'];
$query =
if $_POST['action'] == 'Register' {
if $_POST['newsletter'] == 'Mens' {
"INSERT INTO newsletters(mens) VALUES('$email')";
}
elseif $_POST['newsletter'] == 'Mens & Womens' {
"INSERT INTO newsletters(mensandwomens) VALUES('$email')";
}
elseif $_POST['newsletter'] == 'Womens' {
"INSERT INTO newsletters(womens) VALUES('$email')";
}
;}
mysql_query ($link, $query);
mysql_close($link);
?>

HTML FORM code within index.html:

<FORM action="confirm.html" method="post">

<DIV>

<SPAN class="input">

Action:

<SELECT name="action">

<OPTION>Register</OPTION>

<OPTION>Unregister</OPTION>

</SELECT>&nbsp&nbsp&nbsp

E-mail: <INPUT name="e-mail" type="text"></INPUT>&nbsp&nbsp&nbsp

Newsletter:

<SELECT name="newsletter">

<OPTION>Mens</OPTION>

<OPTION>Mens & Womans</OPTION>

<OPTION>Womens</OPTION>

</SELECT>&nbsp&nbsp&nbsp

<INPUT class="submit" type="submit" value="Submit">

</SPAN>

</DIV>

</FORM>

djm0219
09-07-2011, 08:03 AM
You can't try to assign a value to a variable that is the result of an if condition. Beyond that your if statements aren't properly formed and you had a stray semi-colon on the line before your mysql_query call.


<?php
$link = mysql_connect('localhost', 'testusr', 'testpw');
mysql_select_db('testdb', $link);
$email = $_POST['e-mail'];
if ($_POST['action'] == 'Register') {
if ($_POST['newsletter'] == 'Mens') {
$query = "INSERT INTO newsletters(mens) VALUES('$email')";
} elseif ($_POST['newsletter'] == 'Mens & Womens') {
$query = "INSERT INTO newsletters(mensandwomens) VALUES('$email')";
} elseif ($_POST['newsletter'] == 'Womens') {
$query = "INSERT INTO newsletters(womens) VALUES('$email')";
}
}
mysql_query ($link, $query);
mysql_close($link);
?>

Democrazy
09-07-2011, 08:21 AM
I still get the same error.

Democrazy
09-07-2011, 08:50 AM
Some problems are now fixed. :)

I now get only one error.

The code sits at this:

<?php
$link = mysql_connect('localhost', 'testusr', 'testpw');
mysql_select_db('testdb', $link);
$email = $_POST['e-mail'];
if ($_POST['action'] == 'Register') {
if ($_POST['newsletter'] == 'Mens') {
"INSERT INTO newsletters(mens) VALUES('$email')";
}
elseif ($_POST['newsletter'] == 'Mens & Womens') {
"INSERT INTO newsletters(mensandwomens) VALUES('$email')";
}
elseif ($_POST['newsletter'] == 'Womens') {
"INSERT INTO newsletters(womens) VALUES('$email')";
}
}
mysql_query ($link);
mysql_close($link);
?>

The error:
PHP Warning: mysql_query() expects parameter 1 to be string on line 57:

mysql_query ($link);

One problem I can see here is that PHP does not know its suppose to execute that code as a MySQL query. As can be seen in my initial post, I tried to set the "if" command to $query, and then inserted $query into "mysql_query ()", but PHP was not liking me using $query with the "if" statement. Why is that?

djm0219
09-07-2011, 08:56 AM
I still get the same error.

What I posted was your code corrected and checked to make sure it was valid PHP. There is no way you would have gotten the same error from what you posted if you used what I posted.

djm0219
09-07-2011, 08:58 AM
I now get only one error.

One problem I can see here is that PHP does not know its suppose to execute that code as a MySQL query. As can be seen in my initial post, I tried to set the "if" command to $query, and then inserted $query into "mysql_query ()", but PHP was not liking me using $query with the "if" statement. Why is that?

You ignored what I posted. You have to set the variable $query to something within you if conditions. You also, incorrectly, removed the second parameter to the mysql_query call rendering it invalid.


mysql_query ($link,$query);

Democrazy
09-07-2011, 09:47 AM
Ok, I got confused.

Could you please quote it out so I can see exactly what you mean?

Democrazy
09-07-2011, 10:13 AM
Arghhhhhhh!!!

I fixed one error and now getting another error. I can register an e-mail address for the Mens newsletter, but cannot register an e-mail address for the Mens & Womens or Womens newsletters.

I get the error:

PHP Notice: Undefined variable: query on line 57

mysql_query ($query);

This is what the code stands at now:

<?php
$link = mysql_connect('localhost', 'testusr', 'testpw');
mysql_select_db('testdb', $link);
$email = $_POST['e-mail'];
if ($_POST['action'] == 'Register') {
if ($_POST['newsletter'] == 'Mens') {
$query = "INSERT INTO newsletters(mens) VALUES('$email')";
}
elseif ($_POST['newsletter'] == 'Mens & Womens') {
$query = "INSERT INTO newsletters(mensandwomens) VALUES('$email')";
}
elseif ($_POST['newsletter'] == 'Womens') {
$query = "INSERT INTO newsletters(womens) VALUES('$email')";
}
}
mysql_query ($query);
mysql_close($link);
?>

djm0219
09-07-2011, 10:19 AM
Move your query and close inside the if test for whether or not Register has been pressed since you only want to insert something when someone has pressed the Register button on your form. You also still had the wrong syntax for the call to mysql_query ... it needs both the link and the query as you had in your original post (and please use php tags for code instead of code tags).



<?php
$link = mysql_connect('localhost', 'testusr', 'testpw');
mysql_select_db('testdb', $link);
$email = $_POST['e-mail'];
if ($_POST['action'] == 'Register') {
if ($_POST['newsletter'] == 'Mens') {
$query = "INSERT INTO newsletters(mens) VALUES('$email')";
}
elseif ($_POST['newsletter'] == 'Mens & Womens') {
$query = "INSERT INTO newsletters(mensandwomens) VALUES('$email')";
}
elseif ($_POST['newsletter'] == 'Womens') {
$query = "INSERT INTO newsletters(womens) VALUES('$email')";
}
mysql_query ($link, $query);
mysql_close($link);
}
?>

ASTP001
09-07-2011, 10:21 AM
Your option field says "Mens & Womans" while your if else statement says "Mens & Womens"
it's a spelling discrepancy.

Democrazy
09-07-2011, 10:25 AM
I still don't understand what you mean. I'm more of a visual learner over theory. I would need to see code example. I don't get you. But thanks anyway. :)

ASTP001
09-07-2011, 10:27 AM
Change your html code to:

<SELECT name="newsletter">
<OPTION>Mens</OPTION>
<OPTION>Mens & Womens</OPTION>
<OPTION>Womens</OPTION>
</SELECT>

Democrazy
09-07-2011, 10:29 AM
Its already set to that.

The HTML is fine. I just included it in my initial post for those who like to visualize.

ASTP001
09-07-2011, 10:31 AM
No it's not. What it said on your first post is:


<OPTION>Mens & Womans</OPTION>

What it needs to be is:

<OPTION>Mens & Womens</OPTION>

Edit: also if you want to be correct with English...(although you may need to change your php to be consistent with this... when you say "mens," it's possessive so it needs an apostrophe, same with "womans." Correct way would be to say "Men's" and "Women's."

Democrazy
09-07-2011, 10:33 AM
****, your right dude!! Cheers on that one yeah!

Democrazy
09-07-2011, 10:34 AM
OMFG God bro! You've fully solved it! Your the best!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum