display block and connection troubles

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

display block and connection troubles

Post by Obadiah »

for some reason or another when i run this script all the gui displays correctly....i dont get any DB connection errors...in fact i dont get any errors at all...but...the email isnt storing in the database and none of my error or display_block() are working within the php script...can anyone help me figure out why?

Code: Select all

<?php
//set up a couple of functions
function doDB()
{
	global $conn;
	//connect to server and select database; you may need it
	$conn = mysql_connect("88888", "8888", "888888888888")
		or die(mysql_error());
	mysql_select_db("testdb",$conn) or die(mysql_error());
}

function emailChecker($email)
{
	global $conn, $check_result;
	//check that email is not already in list
	$check = "select id from suscribers where email = '$email'";
	$check_result = mysql_query($check,$conn) or die(mysql_error());
}

//determine if they need to see the form or not
if($_POST[op] != "ds")
{
//they do so create the form block
$display_block= "
<form method=POST action=\"$_SERVER[PHP_SELF]\">
<p><strong>Your Email Address:</strong><br>
<input type=text name\"email\" size=40 maxlength=150></p>

<p><strong>Action:</strong><br>
<input type=radio name=\"action\" value=\"sub\" checked> suscribe
<input type=radio name=\"action\" value=\"unsub\"> unsuscribe

<input type=hidden name=\"op\" value=\"ds\">

<p><input type=submit name=\"submit\" value=\"Submit Form\"></p>
</form>";
}	
else if(($_POST[op] == "ds") && ($_POST[action] == "sub"))
	{
		if($_POST[email] == "")
		{
			header("Location: manage.php");
			exit;
		}
		doDB();
		//check that email is in list
		emailChecker($_POST[email]);
		
		//get number of results and do action
		if(mysql_num_rows($check_result) < 1)
		{
			//add record
			$sql = "insert into suscribers values('', '$_POST[email]')";
			$result = mysql_query($sql,$conn) or die(mysql_error());
			$display_block= "<p>Thanks for signing up!</p>";
		}
			else
			{
				//Print failure message
				$display_block= "<p>You're already subcribed</p>";
			}
	}
	else if(($_POST[op] == "ds") && ($_POST[action] == "sub"))
	{
		//trying to unsubscribe; validate email address
		if($_POST[email]=="")
		{
			header("Location: manage.php");
			exit;
		}
		//connect to database
		doDB();
		
		//check that email is in list
		emailChecker($_POST[email]);
		
		//get number of results and do action
		if(mysql_num_rows($check_result) < 1)
		{
			//print failure message
			$display_block = "<p>Couldn't find your address!</p>
			<p>No action was taken</p>";
			
		}
		else
		{
			//unsubscribe the address
			$id = mysql_result($check_result, 0, "id");
			$sql = "DELETE FROM subcribers WHERE id = '$id' ";			
			$result = mysql_query($sql,$conn) or die(mysql_error());
			$display_block = "<p>You're unsubscribed</p>";
		}
	}
?>
<html>
<head>
<title></title>
</head>
<body>
<h1>Subscribe/Unsubscribe</h1>
<?php echo "$display_block";?>
</body>
</html>
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

1. Don't use "global" if you don't need it!
2. If you have only one connection to the DB, you don't have to add the connection resource ($conn).
Example:

Code: Select all

mysql_query($check,$conn);
Become:

Code: Select all

mysql_query($check);
3.
Bad:

Code: Select all

function emailChecker($email)
{
        global $conn, $check_result;
        //check that email is not already in list
        $check = "select id from suscribers where email = '$email'";
        $check_result = mysql_query($check,$conn) or die(mysql_error());
}
Good:

Code: Select all

function emailChecker($email)
{ 
        //check that email is not already in list
        $check = "select id from suscribers where email = '$email'";
        $check_result = mysql_query($check) or die(mysql_error());

        return $check_result;
}
and then:

Code: Select all

$check_result = emailChecker($_POST[email]);
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

thanx for the seemingly simplistic reply...a bit too sarcastic for my taste anyway when i make those changes i get the following

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Program Files\xampp\htdocs\PHP\manage.php on line 16

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\bla\bla\bla on line 16
Access denied for user 'ODBC'@'localhost' (using password: NO)

i think i was closer before....try running the program yourself...you will see that it works and that the only problems i get are ine it dosent give any errors from storing in the DB but it doesnt store at all....then not all the display blocks show up....copy and run it first if anyone has the time and if you figure out what i was doing wrong...please reply back to me...thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So your script is telling you that the login name 'ODBC' does not have access to the server 'localhost' so you have no connection.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

thanx for the seemingly simplistic reply...a bit too sarcastic for my taste anyway when i make those changes i get the following
I'm sorry... I didn't mean to insult or hurt someone.



Post your code again (the one you run when you got the error "ODBC@localhost"...) and I'll try to find the problem.
But I think, like Everah said, that it's only a MySQL user problem.


And again, I am really sorry :wink:
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I think that on an internet forum people can be interpreted in a completely different way than they intended... obadiah, I think he was just trying to help... I suggest when people try to help you it's a good idea (whether you think they are poking fun at you or not) to just be grateful. I'm not trying to come down on you or anything... I just notice a lot of people get irritated with folks for trying to help them on this forum...
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

@everah=no...and thats what has me confused is that it actually shows the gui and allows you to enter the interface....it just wont give back the display blocks()

@ok= i greatly apprieciate that you did attempt to assist me and/or took the time to even look at my question and review my code...i agree with spacegoat in the way of saying alot of things written can be misinterpreted....sorry bout the misinterpretation mann...thanks again :)...i did try out your suggestion btw...when you said that the
global wasn't needed...am i not using the connection function throughout the whole of the program via db()?
if your gonna use a method or whatnot throughout a script arent you supposed to define it global...kinda like
a OPTION EXPLICIT declaration in VB?

@spacegoat= much agreed...i dont know this language well and much apologies again if i took it the assist given in the wrong way...the application i posted is a book tutorial...its not quite working the way it told me...lol
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Here is what I would do... first, ditch the connection inside of a function thing for now. Rewrite the first part of the script like this...

Code: Select all

<?php
/**
 * this function checks to see if there is an email in the database, true if so, false otherwise
 *
 * @param string $email
 * @return boolean
 */
function emailChecker($email)
{
    //check that email is not already in list
    if (!$result = mysql_query("select id from suscribers where email = '$email'"))
    {
        die('there was an error checking the email: ' . mysql_error());
    }

    if (mysql_num_rows($result) > 0)
    {
        return true; // this means the email already exists
    }

    return false;
}

if (!$connect = mysql_connect('localhost', 'dbuser', 'userpassword'))
{
    die('Connect connect to the db server: ' . mysql_error());
}

if (!mysql_select_db('dbname', $connect))
{
    die('could not not access the database: ' . mysql_error());
}
?>
Then I would suggest you go through your code a look for those little things that can keep a script from working the way you expect, such as

Code: Select all

$sql = "insert into suscribers values('', '$_POST[email]')";
$_POST[email]==""
<?php echo "$display_block";?>
After you run across some of these things, post back so we can help some more.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

*Clears throat*

Code: Select all

function emailChecker($email) 
{ 
    $email = mysql_real_escape_string($email);
    //check that email is not already in list 
    if (!$result = mysql_query("select id from suscribers where email = '$email'"))
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Good idea. I should have thought of that.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

Everah wrote: Then I would suggest you go through your code a look for those little things that can keep a script from working the way you expect, such as

Code: Select all

$sql = "insert into suscribers values('', '$_POST[email]')";
$_POST[email]==""
<?php echo "$display_block";?>
After you run across some of these things, post back so we can help some more.
i made all of the other changes although im not quite sure what this line does...looks
kinda like a cool sort of shortcut though

Code: Select all

$email = mysql_real_escape_string($email);
@everah= should i remove the above lines you asked me to look at?...im looking at them now should i replace

Code: Select all

$sql = "insert into suscribers values('', '$_POST[email]')";
with something like

Code: Select all

$mysql_query(insert into suscribers values('', '$_POST[email]')");
or will my "wing-in it" in that situation botch things up....another thing....
$_POST[email]=="" isnt that to show the contents of the email column, and if i take out <?php echo "$display_block";?>would'nt that prevent me from veiwing the
GUI for now....i commented those 3 for now btw...i just wanted to know
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Change what you know how to change (unless you like learning by trial and error - which I don't mind so much)...

Code: Select all

<?php
// This ...
$sql = "insert into suscribers values('', '$_POST[email]')";

//Should look like this...
$sql = "insert into suscribers values ('', '" . mysql_real_escape_string($_POST[email]) . "')";

// this...
$_POST[email]==""

// should look like this...
$_POST['email']==""

// and this...
?>
<?php echo "$display_block";?>
<?php
// should look like this...
?>
<?php echo $display_block; ?>
Of course, these are little things. Some might hurt you, some might not. But the object is to code clean, so give them a whirl and see where it goes...
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

i made all the changes you requested but it seems im back at square one....nothing is saving into the database....and still no message....here is my new code

Code: Select all

<?php 
/** 
 * this function checks to see if there is an email in the database, true if so, false otherwise 
 * 
 * @param string $email 
 * @return boolean 
 */ 

function emailChecker($email) 
{ 
    $email = mysql_real_escape_string($email); 
    //check that email is not already in list 
    if (!$result = mysql_query("select id from suscribers where email = '$email'")) 
    if (mysql_num_rows($result) > 0) 
    { 
        return true; // this means the email already exists 
    } 

    return false; 
} 

if (!$connect = mysql_connect('thishost', 'boot', 'Alt+F4')) 
{ 
    die('Connect connect to the db server: ' . mysql_error()); 
} 

//determine if they need to see the form or not 
if($_POST[op] != "ds") 
{ 
//they do so create the form block 
$display_block= " 
<form method=POST action=\"$_SERVER[PHP_SELF]\"> 
<p><strong>Your Email Address:</strong><br> 
<input type=text name\"email\" size=40 maxlength=150></p> 

<p><strong>Action:</strong><br> 
<input type=radio name=\"action\" value=\"sub\" checked> suscribe 
<input type=radio name=\"action\" value=\"unsub\"> unsuscribe 

<input type=hidden name=\"op\" value=\"ds\"> 

<p><input type=submit name=\"submit\" value=\"Submit Form\"></p> 
</form>"; 
}        
else if(($_POST[op] == "ds") && ($_POST[action] == "sub")) 
        { 
                if($_POST[email] == "") 
                { 
                        header("Location: manage.php"); 
                        exit; 
                } 
                doDB(); 
                //check that email is in list 
                emailChecker($_POST[email]); 
                
                //get number of results and do action 
                if(mysql_num_rows($check_result) < 1) 
                { 
                        //add record 
                        $sql = "insert into suscribers values ('', '" . mysql_real_escape_string($_POST[email]) . "')";
                        $result = mysql_query($sql,$conn) or die(mysql_error()); 
                        $display_block= "<p>Thanks for signing up!</p>"; 
                } 
                        else 
                        { 
                                //Print failure message 
                                $display_block= "<p>You're already subcribed</p>"; 
                        } 
        } 
        else if(($_POST[op] == "ds") && ($_POST[action] == "sub")) 
        { 
                //trying to unsubscribe; validate email address 
                if($_POST['email']=="") 
                { 
                        header("Location: manage.php"); 
                        exit; 
                } 
                //connect to database 
                doDB(); 
                
                //check that email is in list 
                emailChecker($_POST[email]); 
                
                //get number of results and do action 
                if(mysql_num_rows($check_result) < 1) 
                { 
                        //print failure message 
                        $display_block = "<p>Couldn't find your address!</p> 
                        <p>No action was taken</p>"; 
                        
                } 
                else 
                { 
                        //unsubscribe the address 
                        $id = mysql_result($check_result, 0, "id"); 
                        $sql = "DELETE FROM subcribers WHERE id = '$id' ";                  
                        $result = mysql_query($sql,$conn) or die(mysql_error()); 
                        $display_block = "<p>You're unsubscribed</p>"; 
                } 
        } 
?> 
<html> 
<head> 
<title></title> 
</head> 
<body> 
<h1>Subscribe/Unsubscribe</h1> 
<?php echo $display_block; ?>
</body> 
</html>
the weird thing ive just noticed is...its not giving me an error mesage for my doDB() i use throughout the script...imnot sure why not though
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Throw this as the beginning of your script:

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
?>
Then rerun it and see what comes up.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

Notice: Use of undefined constant op - assumed 'op' in C:\here\in\this\folder\manage.php on line 33

Notice: Undefined index: op in C:\here\in\this\folder\manage.php on line 33

that was a neat trick :)
[edited]

hmmm...its not liking this line so much anymore

Code: Select all

if($_POST[op] != "ds")
its not defined and so i understand the error, but the tutorial that im following doesnt include a way of defining it how would i go about doing that?
Last edited by Obadiah on Thu Sep 21, 2006 3:52 pm, edited 1 time in total.
Post Reply