Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Having a problem with my first PHP script

    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:
    Code:
    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:
    Code:
    <?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:
    Code:
    <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>

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,312
    Thanks
    4
    Thanked 206 Times in 203 Posts
    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 Code:
    <?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);
    ?>
    Dave .... HostMonster for all of your hosting needs

  • #3
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    I still get the same error.
    Last edited by Democrazy; 09-07-2011 at 08:29 AM.

  • #4
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Some problems are now fixed.

    I now get only one error.

    The code sits at this:
    Code:
    <?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:
    Code:
    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?
    Last edited by Democrazy; 09-07-2011 at 08:55 AM.

  • #5
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,312
    Thanks
    4
    Thanked 206 Times in 203 Posts
    Quote Originally Posted by Democrazy View Post
    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.
    Dave .... HostMonster for all of your hosting needs

  • #6
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,312
    Thanks
    4
    Thanked 206 Times in 203 Posts
    Quote Originally Posted by Democrazy View Post
    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.

    PHP Code:
    mysql_query ($link,$query); 
    Dave .... HostMonster for all of your hosting needs

  • #7
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Ok, I got confused.

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

  • #8
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    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
    Code:
    mysql_query ($query);
    This is what the code stands at now:
    Code:
    <?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);
    ?>
    Last edited by Democrazy; 09-07-2011 at 10:19 AM.

  • #9
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,312
    Thanks
    4
    Thanked 206 Times in 203 Posts
    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 Code:
    <?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);
            }
    ?>
    Dave .... HostMonster for all of your hosting needs

  • #10
    New Coder
    Join Date
    Aug 2011
    Posts
    51
    Thanks
    6
    Thanked 12 Times in 12 Posts
    Your option field says "Mens & Womans" while your if else statement says "Mens & Womens"
    it's a spelling discrepancy.

  • #11
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    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.

  • #12
    New Coder
    Join Date
    Aug 2011
    Posts
    51
    Thanks
    6
    Thanked 12 Times in 12 Posts
    Change your html code to:
    Code:
    <SELECT name="newsletter">
    <OPTION>Mens</OPTION>
    <OPTION>Mens & Womens</OPTION>
    <OPTION>Womens</OPTION>
    </SELECT>

  • #13
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Its already set to that.

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

  • #14
    New Coder
    Join Date
    Aug 2011
    Posts
    51
    Thanks
    6
    Thanked 12 Times in 12 Posts
    No it's not. What it said on your first post is:

    PHP Code:
    <OPTION>Mens Womans</OPTION
    What it needs to be is:
    PHP Code:
    <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."
    Last edited by ASTP001; 09-07-2011 at 10:35 AM.

  • Users who have thanked ASTP001 for this post:

    Democrazy (09-07-2011)

  • #15
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    ****, your right dude!! Cheers on that one yeah!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •