Fatal error: Call to undefined function doDB()

Links for php scripts

Moderators: macek, egami, gesf

omar
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Oct 10, 2011 3:22 pm

Fatal error: Call to undefined function doDB()

Postby omar » Mon Oct 10, 2011 3:36 pm

Hello everyone
I am new to php and tried to learn by using book now i tried this script gave me the above error. I did define the function doDB() in the connection file, second code is the connet file.
Please help.

Code: Select all



<?php
require_once("includes/connect.php");

//determine if they need to see the form or not
if (!$_POST) {
   //they need to see the form, so create form block
   $display_block = "
   <form method=\"POST\" action=\"".$_SERVER["PHP_SELF"]."\">

   <p><strong>Your E-Mail Address:</strong><br/>
   <input type=\"text\" name=\"email\" size=\"40\" maxlength=\"150\">

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

   <p><input type=\"submit\" name=\"submit\" value=\"Submit Form\"></p>
   </form>";

} else if (($_POST) && ($_POST["action"] == "sub")) {
   //trying to subscribe; validate email address
   if ($_POST["email"] == "") {
      header("Location: manage.php");
      exit;
   } else {
      //connect to database
      doDB();
      //check that email is in list
      emailChecker($_POST["email"]);

      //get number of results and do action
      if (mysqli_num_rows($check_res) < 1) {
         //free result
         mysqli_free_result($check_res);

         //add record
         $add_sql = "INSERT INTO subscribers (email) VALUES('".$_POST["email"]."')";
         $add_res = mysqli_query($mysqli, $add_sql) or die(mysqli_error($mysqli));
         $display_block = "<p>Thanks for signing up!</p>";

         //close connection to MySQL
         mysqli_close($mysqli);
      } else {
         //print failure message
         $display_block = "<p>You're already subscribed!</p>";
      }
   }
} else if (($_POST) && ($_POST["action"] == "unsub")) {
   //trying to unsubscribe; validate email address
   if ($_POST["email"] == "") {
      header("Location: manage.php");
      exit;
   } else {
      //connect to database
      doDB();

      //check that email is in list
      emailChecker($_POST["email"]);

      //get number of results and do action
      if (mysqli_num_rows($check_res) < 1) {
         //free result
         mysqli_free_result($check_res);

         //print failure message
         $display_block = "<p>Couldn't find your address!</p>
         <p>No action was taken.</p>";
      } else {
         //get value of ID from result
         while ($row = mysqli_fetch_array($check_res)) {
            $id = $row["id"];
         }

         //unsubscribe the address
         $del_sql = "DELETE FROM subscribers WHERE id = '".$id."'";
         $del_res = mysqli_query($mysqli, $del_sql) or die(mysqli_error($mysqli));
         $display_block = "<P>You're unsubscribed!</p>";
      }
      mysqli_close($mysqli);
   }
}
?>
<html>
<head>
<title>Subscribe/Unsubscribe to a Mailing List</title>
</head>
<body>
<h1>Subscribe/Unsubscribe to a Mailing List</h1>
<?php echo "$display_block"; ?>
</body>
</html>


bellow is the connct.ph file

Code: Select all


<?
//set up a couple of functions for use by scripts in ch19
function doDB() {
   global $mysqli;

   //connect to server and select database; you may need it
   $mysqli = mysqli_connect("localhost", "omar", "ali1966", "hchf");

   //if connection fails, stop script execution
   if (mysqli_connect_errno()) {
      printf("Connect failed: %s\n", mysqli_connect_error());
      exit();
   }
}

function emailChecker($email) {
   global $mysqli, $check_res;

   //check that email is not already in list
   $check_sql = "SELECT id FROM subscribers WHERE email = '".$email."'";
   $check_res = mysqli_query($mysqli, $check_sql) or die(mysqli_error($mysqli));
}

?>

Last edited by omar on Thu Oct 13, 2011 3:50 am, edited 1 time in total.

lokanath7
New php-forum User
New php-forum User
Posts: 139
Joined: Mon Nov 15, 2010 12:11 am

Re: Fatal error: Call to undefined function doDB()

Postby lokanath7 » Tue Oct 11, 2011 2:50 am

is that doDB() is defined in "connect.php" ? if yes then give the code written in "connect.php"

omar
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Oct 10, 2011 3:22 pm

Re: Fatal error: Call to undefined function doDB()

Postby omar » Wed Oct 12, 2011 3:28 pm

Hi
thank you for replay, yes it is. bellow is the connecthion svript

Code: Select all

<?
//set up a couple of functions for use by scripts
function doDB() {
   global $mysqli;

   //connect to server and select database; you may need it
   $mysqli = mysqli_connect("localhost", "omar", "ali1966", "hchf");

   //if connection fails, stop script execution
   if (mysqli_connect_errno()) {
      printf("Connect failed: %s\n", mysqli_connect_error());
      exit();
   }
}

function emailChecker($email) {
   global $mysqli, $check_res;

   //check that email is not already in list
   $check_sql = "SELECT id FROM subscribers WHERE email = '".$email."'";
   $check_res = mysqli_query($mysqli, $check_sql) or die(mysqli_error($mysqli));
}
?>













Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 2 guests

cron