Jump to content

Use "if" correctly


Freedom-n-Democrazy

Recommended Posts

Hi all,

 

 

I am making a newsletter sign up system and needing a little help with the PHP side of things. The HTML code I am about to quote is a stripped back version of what I am trying to do (keeping it simple for the thread). :)

 

HTML code:

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

 

<DIV>

 

  <SPAN>

 

  Action:

 

    <SELECT name="action">

    <OPTION>Register</OPTION>

    <OPTION>Unregister</OPTION>

 

    </SELECT>

 

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

 

  <BR>

  Newsletter:

 

    <SELECT name="newsletter">

 

    <OPTION>Mens</OPTION>

    <OPTION>Womens</OPTION>

 

    </SELECT>

 

  <BR>

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

 

  </SPAN>

 

</DIV>

 

</FORM>

 

PHP code:

<?php

$link = mysql_connect('localhost', 'testuser', 'testpw');

if (!$link) {

  die('Could not connect: ' . mysql_error());

}

echo 'Connected successfully';

mysql_select_db('testdb', $link);

 

if $_POST['action'] == ('register') {

  if $_POST['newsletter'] == ('mens') {

  $sql = "INSERT INTO newsletters(mens) VALUES('{$_POST['e-mail']}')"

  }

  if $_POST['newsletter'] == ('womens') {

  $sql = "INSERT INTO newsletters(womens) VALUES('{$_POST['e-mail']}')"

  };

}

 

if (!mysql_query($sql,$link)) {

die('Error: ' . mysql_error());

}

echo 'Done!';

mysql_close($link);

?>

 

I know the HTML part is fine, but its the PHP code where I am making the mistakes. I think I am treating it too much like Python.

What am I doing wrong?

Link to comment
Share on other sites

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// something was posted

if(isset($_POST['action']) && $_POST['action'] == 'register')
{
	if(isset($_POST['newsletter']) && ($_POST['newsletter'] == 'mens' || $_POST['newsletter'] == 'womens'))
	{
		// validate email?

		$email = mysql_real_escape_string($_POST['email']);

		if($_POST['newsletter'] == 'mens')
		{
			$sql = "INSERT INTO newsletters(mens) VALUES('$email')"
		}
		else
		{
			$sql = "INSERT INTO newsletters(womens) VALUES('$email')"
		}

		// do the query here
	}
}
} 

Link to comment
Share on other sites

I'm going to have three newsletters, does this mean I would write:

 

if(isset($_POST['newsletter']) && ($_POST['newsletter'] == 'mens' || $_POST['newsletter'] == 'womens')) || $_POST['newsletter'] == 'mensandwomens')) {

$email = mysql_real_escape_string($_POST['email']);

if($_POST['newsletter'] == 'mens') {
$sql = "INSERT INTO newsletters(mens) VALUES('$email')"
}

if {
$sql = "INSERT INTO newsletters(womens) VALUES('$email')"
}

if {
$sql = "INSERT INTO newsletters(mensandwomens) VALUES('$email')"
}

 

Also, must you use "else" if its the last instruction, or is "if" fine?

Link to comment
Share on other sites

Good afternoon guys,

 

I used this code, and get the error "Error: Query was empty".

I've gone over and over it, but can't figure out whats going wrong. Can you guys see any problem in it??

 

<?php
$link = mysql_connect('localhost', 'testusr', 'testpw');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('testdb', $link);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['action']) && $_POST['action'] == 'register') {
	if (isset($_POST['newsletter']) && ($_POST['newsletter'] == 'mens' || $_POST['newsletter'] == 'mensandwomens' || $_POST['newsletter'] == 'womens')) {
		$email = mysql_real_escape_string($_POST['e-mail']);
		if ($_POST['newsletter'] == 'mens') {
			$sql = "INSERT INTO newsletters(mens) VALUES('$email')";
		}
		elseif ($_POST['newsletter'] == 'mensandwomens') {
			$sql = "INSERT INTO newsletters(mensandwomens) VALUES('$email')";
		}
		elseif ($_POST['newsletter'] == 'womens') {
			$sql = "INSERT INTO newsletters(womens) VALUES('$email')";
		}
	}
}
}
if (!mysql_query($sql,$link)) {
die('Error: ' . mysql_error());
}
echo 'Done!';
mysql_close($link);
?>

Link to comment
Share on other sites

The query needs to be with the $sql block, not where it is now because any page view that isn't a post will trigger the error you're getting, because the $sql variable is not being set - this is only set if the form was posted and the action is register, and newsletter is mens|womens|mensandwomens.

 

Use Firebug's net tab, to see what data is being posted to your page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.