我正在尝试从类中使用if条件访问我的函数,其中我的函数包含SQL语句我想要做的是使用if条件从另一个php文件访问它,如果它存在它将返回true。以下代码。
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>
这是我的主页,用户可以在其中选择一个选项。单击下拉菜单中的选项后。它会将我重定向到addBooks.html
,用户将填写以下文本字段。
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;
}
}
?>
我创建了一个连接,所以我将从我想要使用的每个文件中调用它。在我的booksCRUD.php
中,我创建了一个受保护的字符串,以便我可以在任何地方访问它。我已在登录中使用此方法。所以这是我第二次在INSERT语句中使用它。
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";
}
}
?>
我在if条件下创建了一个对象,但如果我做错了,我就无法访问我的函数。
答案 0 :(得分:1)
使用method_exists()
函数检查方法是否存在,然后调用方法,如下所示:
if(method_exists(new booksCRUD,'createBook')){
// method exists
}else{
// method doesn't exist
}
您的代码中存在多个错误,例如:
请参阅此声明,
$dbLanguange = $_POST['bookLanguage'];
^ it should be $dbLanguage
查看if
子句的条件
if(!empty($dbBookName) && !empty($dbEdition) && !empty($dbPages) && empty($dbLanguage))
^ it should be !empty($dbLanguage)
由于您不打算在课程$accessMyDB
之外访问booksCRUD
媒体资源,因此最好将其设为private
。
请参阅此行,
echo "Error" . $mysqli->error();
您的连接处理程序$mysqli
在此范围内不可用。
解决方案将保持您的 connection.php ,并更改您的 bookCRUD.php 和 bookProcess。 php 以下列方式:
<强> 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";
}
}
?>
答案 1 :(得分:0)
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";
}