global mysqli variable?

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Trying some simple testing to form the backbone of a subscription system. Why am I getting this error when I make a subscription?
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mail Subscription</title>
</head>
<body>

<?php
function DBCon() {
    global $con;
    $con = mysqli_connect("localhost", "user", "nopass","mtwinkie_testdb");
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
}

function Validate($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL))
        die("<h1 align=\"center\">Invalid Email.</h1>");
    else if (strlen(email) > 150)
        die("<h1 align=\"center\">Email too long.</h1>");
    $query = "SELECT id FROM Newsletter WHERE email = '" . email ."';";
    $res = mysqli_query($con,$query) or
        die(mysqli_error($con));
    if (mysqli_num_rows($res) < 1)    
        die("<h1 align=\"center\">Your already subscribed.</h1>");
    return true;
}

if (!isset($_REQUEST["do"])) {
?>

<h1>Mail Subscription</h1>
<form action="#" method="post">
<b>Your Email Address:</b><br />
<input type="text" name="email" /><br /><br />
<b>Action:</b><br />
<input type="radio" name="do" value="sub" /> Subscribe
<input type="radio" name="do" value="unsub" /> Unsubscribe
<p><input type="submit" value="Submit Form" /></p>

<?php
} else if ($_REQUEST["do"]=="sub") {
    DBCon();
    if (Validate($_REQUEST["email"])) {
        $query = "INSERT INTO Newsletter (ID, Email) VALUES (\"" . $email . "\");";
        $res = mysqli_query($con,$query) or
            die(mysqli_error($con));
        mysql_close($con);
        die("<h1 align=\"center\">Thank you for signing up!</h1>");
    }
} else if ($_REQUEST["do"]=="unsub") {
    die("<h1 align=\"center\">Not available yet =(</h1>");
} else die("<h1 align=\"center\">Logical Script Error.</h1>");
?>

</body>
</html>
As far as I can tell the global declaration should solve this problem?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
2 problems:
Code:
function Validate($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL))
        die("<h1 align=\"center\">Invalid Email.</h1>");
    else if (strlen([COLOR="Red"]email[/COLOR]) > 150)
        die("<h1 align=\"center\">Email too long.</h1>");
    $query = "SELECT id FROM Newsletter WHERE email = '" . [COLOR="red"]email[/COLOR] ."';";
    $res = mysqli_query($con,$query) or
        die(mysqli_error($con));
    if (mysqli_num_rows($res) < 1)    
        die("<h1 align=\"center\">[COLOR="red"]Your[/COLOR] already subscribed.</h1>");
    return true;
}
You left the '$' off of 'email' in 2 lines here. Also, grammar maven says that that should be "You're" ("you are"), not "Your" (possessive).

PHP:
        $query = "INSERT INTO Newsletter (ID, Email) VALUES (\"" . $email . "\");";

This is the source of the error. You say you're going to provide 2 values, ID and Email, but only provide 1. If the ID column is auto_increment, leave ID out of the insert statement.
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Thanks misson, I already the first two errors after I posted, but that's not what the problem was. I got a warning that I did not have a valid mysqli variable supplied in the email validation function. I later fixed it my moving the mysqli portion of the function where the DBCon() was called, but why can't the script posted above read the $con variable in the Validate() function is $con is a global variable?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Validate() doesn't declare $con as global. I recommend passing $con as a parameter rather than using a global; it's cleaner.
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
You're probably right, but I declare it as a global variable in the DBCon() function. Shouldn't that make it accessible in the Validate() function?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
You're probably right, but I declare it as a global variable in the DBCon() function. Shouldn't that make it accessible in the Validate() function?

Nope. Each function has its own scope. The "global" declaration imports a global variable into a function's scope (actually, it creates a local variable that holds a reference to the global variable). It doesn't make a variable a superglobal.

In any case, pass the connection around as a parameter and you don't need to worry about globals.
 
Last edited:
Top