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!
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?
<?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>
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:
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());
}
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;
}
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
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.
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...
@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
<?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
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'"))
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
$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
<?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...
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
<?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