Web Analytics Made Easy -
StatCounter Having a problem with my first PHP script - CodingForum

Announcement

Collapse
No announcement yet.

Having a problem with my first PHP script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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

    Comment


    • #3
      I still get the same error.
      Last edited by Democrazy; Sep 7, 2011, 02:29 AM.

      Comment


      • #4
        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; Sep 7, 2011, 02:55 AM.

        Comment


        • #5
          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

          Comment


          • #6
            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

            Comment


            • #7
              Ok, I got confused.

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

              Comment


              • #8
                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; Sep 7, 2011, 04:19 AM.

                Comment


                • #9
                  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

                  Comment


                  • #10
                    Your option field says "Mens & Womans" while your if else statement says "Mens & Womens"
                    it's a spelling discrepancy.

                    Comment


                    • #11
                      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.

                      Comment


                      • #12
                        Change your html code to:
                        Code:
                        <SELECT name="newsletter">
                        <OPTION>Mens</OPTION>
                        <OPTION>Mens & Womens</OPTION>
                        <OPTION>Womens</OPTION>
                        </SELECT>

                        Comment


                        • #13
                          Its already set to that.

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

                          Comment


                          • #14
                            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; Sep 7, 2011, 04:35 AM.

                            Comment


                            • #15
                              ****, your right dude!! Cheers on that one yeah!

                              Comment

                              Working...
                              X
                              😀
                              🥰
                              🤢
                              😎
                              😡
                              👍
                              👎