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
https://forums.phpfreaks.com/topic/246402-use-if-correctly/
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
https://forums.phpfreaks.com/topic/246402-use-if-correctly/#findComment-1265330
Share on other sites

Hi,

 

 

Thanks for your reply.

 

Is there an easier way to do it? I know I can simply cut and copy that code, but I want to learn whats going on.. That code you write is confusing as hell as its introduced all of these elements I've never heard of before.

 

EDIT: I'm new to PHP.

Link to comment
https://forums.phpfreaks.com/topic/246402-use-if-correctly/#findComment-1265343
Share on other sites

Ah huh! Read it a few times over. I now see how it works.

 

Thank you very much.

 

 

Now I see how it works, I can build the full PHP will all entries.

 

You mentioned validate e-mail.

I do want this, but I fear it will be too time consuming for me to learn how to do it. Is it very hard?

Link to comment
https://forums.phpfreaks.com/topic/246402-use-if-correctly/#findComment-1265345
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
https://forums.phpfreaks.com/topic/246402-use-if-correctly/#findComment-1265351
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
https://forums.phpfreaks.com/topic/246402-use-if-correctly/#findComment-1265548
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
https://forums.phpfreaks.com/topic/246402-use-if-correctly/#findComment-1265569
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.