1

I'm trying to access my function from class using if condition where my function that contains SQL Statement what I want to do is access it from another php file using if condition if it's existing it will return true. Codes below.

main.html

< script type = "text/javascript" src = "https://code.jquery.com/jquery-1.12.3.min.js" > < /script>	<script>	$(document).on("click", "#addBooksId", function()	{ $.get("addBooks.html", function(data) { $( "#myform" ).empty(); $('#myform').append(data); / / Receive with value alert('This is add books section'); }); }); < /script>
body { padding: 0; margin: 0; overflow-y: scroll; font-family: Arial; font-size: 15px; } #container { background-color: #707070; } #menu { width: 1250px; margin: 0 auto; text-align: left; } #menu ul { list-style-type: none; /*Remove bullets*/ padding: 0; margin: 0; position: relative; } #menu ul li { display: inline-block; } #menu ul li:hover { text-decoration: none; color: black; } #menu ul li a, visited { color: #CCC; /*Color of text*/ display: block; /*Takes up the full width available*/ padding: 15px; text-decoration: none; } #menu ul li a:hover { color: #CCC; text-decoration: none; } #menu ul li:hover ul { display: block; } #menu ul ul li { display: block; } #menu ul ul { display: none; position: absolute; background-color: #707070; min-width: 140px; /*Width when hover*/ } #menu ul ul li a:hover /*Color of text when hover*/ { color: #099; }
<div id="container"> <div id="menu"> <ul> <li> <a href="#">Manage Books</a> <ul> <div id="addBooksId"> <li><a href="#">Add Books</a> </li> </div> </ul> </li> </ul> </div> </div> <div id="myform"> <p></p> </div>

This is my home page where the user can choose an option. After clicking the option in drop down menu. It will redirect me to the addBooks.html where the user will fill up the following textfield.

addBooks.html

<style type="text/css"> .addBooks { clear: both; width: 800px; margin: 0 auto; margin-top: 10%; } .addBooks label { float: left; width: 150px; text-align: right; } .addBooks input { text-align: left; margin-left: 100px; } .addBooks select { text-align: left; margin-left: 100px; } </style>
<div id="container"> <div class="addBooks"> <h1>Add Books</h1> <hr> <form method="post" action="../process/bookProcess.php"> <fieldset> <div class="form-field"> <label for="name">Book Name:</label> <input type="text" id="name" name="bookName" required> </div> <br /> <div class="form-field"> <label for="edition">Edition:</label> <input type="text" id="edition" name="bookEdition" required> </div> <br /> <div class="form-field"> <label for="pages">Pages:</label> <input type="text" id="pages" name="bookPages" required> </div> <br /> <div class="form-field"> <label for="language">Language:</label> <input type="text" id="language" name="bookLanguage" required> </div> <div class="form-field"> <input type="submit" id="submitBooks" value="Submit"> </div> </fieldset> </form> </div> <!--- end of addBooks ---> </div> <!--- end of container --->

connection.php

<?php //PHP CONSTANTS DEFINE ('host', 'localhost'); DEFINE ('username', 'root'); DEFINE ('password', ''); DEFINE ('database', 'librarysystem'); function getConnection() { $myDB = mysqli_connect(host,username,password,database); if (!$myDB) { trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error()); } else { return $myDB; } } ?> 

I created a connection so I will just call it from every file that I want to use. Here in my booksCRUD.php I created a protected string so I can access it anywhere. I already use this method in my login. So this is the second time I'll use this in my INSERT statement.

bookCRUD.php

<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); class booksCRUD{ private $accessMyDB; public function __construct(){ $this->accessMyDB = getConnection(); } public function createBook($dbBookName,$dbEdition,$dbPages,$dbLanguage){ $statement = $this->accessMyDB->prepare("INSERT INTO bookmodule (bookName, bookEdition, bookPages, bookLanguage) VALUES (?, ?, ?, ?)"); $statement->bind_param("ssss", $dbBookName,$dbEdition,$dbPages,$dbLanguage); if($statement->execute()){ $statement->close(); return true; }else{ echo "Failed: " . $this->accessMyDB->error; return false; } } } ?> 

bookProcess.php

<?php session_start(); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); require_once('../connection/connection.php'); require_once('../crud/bookCRUD.php'); if(isset($_POST['submitBooks'])){ $dbBookName = $_POST['bookName']; $dbEdition = $_POST['bookEdition']; $dbPages = $_POST['bookPages']; $dbLanguage = $_POST['bookLanguage']; if(!empty($dbBookName) && !empty($dbEdition) && !empty($dbPages) && !empty($dbLanguage)){ $obj = new booksCRUD(); if(method_exists($obj,'createBook')){ if($obj->createBook($dbBookName,$dbEdition,$dbPages,$dbLanguage)){ echo "Successful!"; }else{ echo "error"; } }else{ // method doesn't exist } }else{ echo "all fields are required"; } } ?> 

I created a object in my if condition but I cannot access my function correct me if I'm doing it wrong.

1
  • make a object and call the function. Commented May 16, 2016 at 5:41

2 Answers 2

1

Use method_exists() function to check if a method exists or not, and then call the method, like this:

if(method_exists(new booksCRUD,'createBook')){ // method exists }else{ // method doesn't exist } 

Update:

There are several errors in your code, such as:

  • See this statement,

    $dbLanguange = $_POST['bookLanguage']; ^ it should be $dbLanguage 
  • See the condition of your if clause,

    if(!empty($dbBookName) && !empty($dbEdition) && !empty($dbPages) && empty($dbLanguage)) ^ it should be !empty($dbLanguage) 
  • Since you're not planning to access $accessMyDB property outside of the class booksCRUD, it's better that you make it private.

  • See this line,

    echo "Error" . $mysqli->error(); 

Your connection handler $mysqli is not available in this scope.

So the solution would be, keep your connection.php as it is and change your bookCRUD.php and bookProcess.php in the following way:

bookCRUD.php:

<?php class booksCRUD{ private $accessMyDB; public function __construct(){ $this->accessMyDB = getConnection(); } public function createBook($dbBookName,$dbEdition,$dbPages,$dbLanguage){ $statement = $this->accessMyDB->prepare("INSERT INTO bookmodule (bookName, bookEdition, bookPages, bookLanguage) VALUES (?, ?, ?, ?)"); $statement->bind_param("ssss", $dbBookName,$dbEdition,$dbPages,$dbLanguage); if($statement->execute()){ $statement->close(); return true; }else{ echo "Failed: " . $this->accessMyDB->error; return false; } } } ?> 

bookProcess.php:

<?php session_start(); require_once('../connection/connection.php'); require_once('../crud/booksCRUD.php'); if(isset($_POST['submitBooks'])){ $dbBookName = $_POST['bookName']; $dbEdition = $_POST['bookEdition']; $dbPages = $_POST['bookPages']; $dbLanguage = $_POST['bookLanguage']; if(!empty($dbBookName) && !empty($dbEdition) && !empty($dbPages) && !empty($dbLanguage)){ $obj = new booksCRUD(); if(method_exists($obj,'createBook')){ if($obj->createBook($dbBookName,$dbEdition,$dbPages,$dbLanguage)){ echo "Successful!"; }else{ echo "error"; } }else{ // method doesn't exist } }else{ echo "all fields are required"; } } ?> 
Sign up to request clarification or add additional context in comments.

12 Comments

Hi! I follow your code but it didn't work for me. I updated my post please see. :)
@JohnFrancis Have you included connection.php in bookCRUD.php page? I don't see any require_once('connection.php'); there.
I didn't put require_once('connection.php'); in my script. I just access it directly in my constructor. From what I read I don't need to create a instance of an object so I just directly access it. Correct me if I'm wrong please.
@JohnFrancis 1) You didn't change bookCRUD.php and bookProcess.php page as per my Update section, did you? 2) Look at my comment above. 3) You're missing name attribute in your input submit element, it should be like this: <input type="submit" name="submitBooks" id="submitBooks" value="Submit"> 4) Your JavaScript and CSS is not relevant here.
This is the second time you answered my question. So glad your always ready to help :)
|
0

in your bookProcess.php

 if(!empty($dbBookName) && !empty($dbEdition) && !empty($dbPages) && empty($dbLanguage)) // If not empty { $obj=new booksCRUD;//create object $res=$obj->createBook($dbBookName,$dbEdition,$dbPages,$dbLanguage);//call FUNCTION HERE if($res==true){ echo "Successful!"; } else{ echo "Error"; } } else { echo "Error POST Values are empty"; } 

1 Comment

I tried this but it didn't work for me. Please see my updated post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.