"Fatal error: Call to undefined function " Error - php

The following error :
Fatal error: Call to undefined function add() in E:\xampp\htdocs\paperblog\Admin\AddNewPost.php on line 18
line 18 :
$msg=add($title,$subtitle,$details,$_FILES['_postImage']);
The whole codes (HTML
,PHP)are in the following lines.
I've 4 files:
The AddNewPost.php file is the main file has the HTML code.
Has PHP code :
<?php
include_once("..\DB.php");
include_once("..\Classes\post.php");
$title="";
$subtitle="";
$details="";
$msg="";
if(isset($_POST['_PostSubmit']))
{
$title=$_POST['_PostTitle'];
$subtitle=$_POST['_PostSubTtile'];
$details=$_POST['_PostDetails'];
if( !empty($title)||!empty($subtitle)||!empty($details) )
{
$msg=add($title,$subtitle,$details,$_FILES['_postImage']);
}
else
$msg=" The post is empty ";
}
include_once("Header.php");
?>
And HTML:
<form action="AddNewPost.php" method="post" id="cmntfrm" enctype= "multipart/form-data">
<P align="center" style="color:#F00"><?=$msg?></P>
<p> </p>
<table width="600" border="0" align="center">
<img src="../images/addNewPost.png"/>
<br />
<br />
<tr>
<td width="131">Post Title <h8 style="color:#F00">*</h8>:</td>
<td width="443"><input name="_PostTitle" type="text" /></td>
</tr>
<tr>
<td>Post Sub Title <h8 style="color:#F00">*</h8>:</td>
<td><input name="_PostSubTtile" type="text" /></td>
</tr>
<tr>
<td>Post Details :</td>
<td><textarea name="_PostDetails" cols="32" rows="7"> </textarea></td>
</tr>
<tr>
<td>Post Image :</td>
<td><input name="_postImage" type="file"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td></td>
<td><input name="_PostSubmit" type="submit" value="Save" id="submit" /></td>
</tr>
</table>
<center>
<img src="../Post_Imges/a.jpg" height="420" width="460" />
</center>
Thepost.php file which has functions of the post form where is in the classes folder. It Has PHP code:
<?php
include_once("../DB.php");
class post{
var $Post_ID;
var $title,$subtitle,$postdetail,$Post_Imgs;
var $pmonth ,$pyear ,$pday;
function add($title,$subtitle,$postdetail,$file){
$query=" insert into post(Title,SubTitle,PostDetails,PDay,PMonth,PYear)
values('$title,'$subtitle','$postdetail'".date("d").",".date("m").",".date("Y").")";
$this->Post_ID=$this->GetLastPostId();
$msg=test("Add",$query);
$msg.="<br/>".$this->uploadImage($file);
return $msg;
}
function GetLastPostId(){
$query="select Max(Post_ID) from Post";
$result=mysql_query($query);
$row=mysql_fetch_row($result);
return $row[0];
}
function uploadImage($file){
uploadFile("Post_Imges\$Post_ID.jpg",$file);
}
}
?>
3.The DB.php file which has some function for DB. It has :
<?php
include_once("functions.php");
mysql_connect("localhost","root","");
mysql_select_db("paperbloge");
function test($test ,$query){
mysql_query($query);
if(!empty(mysql_errno()))
return "Post ".$test." Successfully" ;
else
return "Error".mysql_errno().":".mysql_error();
}
?>
Finaly, functions.phpfile which has uploadfile function.
function uploadFile($folderPathFileName,$file){
if (!empty($file['tmp_name'])){
move_uploaded_file($file['tmp_name'],$_SERVER['DOCUMENT_ROOT']."\paperblog\ ".$folderFileName);
$msg.="<br/> Image uploaded Successfully";
}
else
$msg= "Image File too large or No Image File";
return $msg;
}
?>
Thats the whole codes that i've .
Does anyone know what is wrong here that cause this problem?
Thanks
Ya it's working , But have some errors again .
Thanks for your helping .

add() is not a function. It is a method of a class called "post". That means you have to instantiate that class and then call that method:
$post = new Post();
$msg=$post->add($title,$subtitle,$details,$_FILES['_postImage']);

add is part of the class post
Change your line to;
$objPost = new post();
$msg = $objPost->add($title,$subtitle,$details,$_FILES['_postImage']);

Related

php codeigniter don't update variable to mysql

I have a problem with my PHP project with CodeIgniter.
I have a form in which I pass variables to the Database and execute an UPDATE Query by modifying the data on the Database.
The problem is that when I insert the new data it does not give me error, it shows me the view in which it shows the data of the table but the data are not modified.
Can anyone help me understand please?
MODELS Hello_Model.php
function displayrecordsById($id)
{
$query=$this->db->query("SELECT * FROM Todolist WHERE id='".$id."'");
return $query->result();
}
function updaterecords($testo,$stato,$id)
{
$this->db->query("update Todolist SET testo='$testo',stato='$stato' WHERE id='".$id."'");
}
Views update_records.php
<html>
<head>
<title>Registration form</title>
</head>
<body>
<?php
//$i=1;
foreach($data as $row)
{
?>
<form method="post">
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr>
<td width="230">Id </td>
<td width="329"><input type="text" name="id" value="<?php echo $row->id; ?>"/></td>
</tr>
<tr>
<td>Testo </td>
<td><input type="text" name="testo" value="<?php echo $row->testo; ?>"/></td>
</tr>
<tr>
<td>Stato</td>
<td><input type='text' name='stato' value= "<?php echo $row->stato; ?> "/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="update" value="Update Records"/></td>
</tr>
</table>
</form>
<?php } ?>
</body>
</html>
</html>
Controllers Hello.php
public function updatedata()
{
$id=$this->input->get('id');
$result['data']=$this->Hello_Model->displayrecordsById($id);
$this->load->view('update_records',$result);
if($this->input->post('update'))
{
$n=$this->input->post('testo');
$e=$this->input->post('stato');
$this->Hello_Model->updaterecords($id,$n,$e);
redirect("http://simone.fabriziolerose.it/index.php/Hello/dispdata");
echo "Success!";
}
}
I took the code from this tutorial
i think problem in calling model function check:
change this:
$this->Hello_Model->updaterecords($id,$n,$e);
To
$this->Hello_Model->updaterecords($n,$e,$id);
and you change your custom query to CI query like:
For Update
$this->db->where("id",$id);
$this->db->update("Todolist",array("testo"=>$testo,"stato"=>$stato));
For Select:
$this->db->where("id",$id);
$query=$this->db->get("Todolist");
$row=$query->row();

Post data that contains paths with ../../ gives empty $_POST array

Recently I moved my codeigniter website to a new server (goddady). Before this everything was working great with no problems. But now I started to get strange problems with post data, whenever I try to insert data that contains relative paths with dots (../../) and try to submit the form, I get an empty $_POST array. The strange thing is that this happens only with certain forms, not all of them. What could cause such problem?
Here is the form that causes problem:
<?php
if(isset($posts2) && count($posts2) == 1){
$posts2 = $posts2[0];
echo form_open_multipart('professors/update_biography/', array("id" => "professors_edit"));
echo form_hidden('posts2[id]', $posts2->id);
if(isset($user) && count($user) == 1){
$user = $user[0];
echo form_hidden('user[id]', $user->id);
echo form_hidden('user[role]', "Professor");
}
?>
<table class="admin_table">
<tr>
<th>
Биографија
</th>
<td>
<textarea name='posts2[biography]'><?php echo $posts2->biography; ?></textarea>
</td>
</tr>
<tr>
<th>
Биографија EN
</th>
<td>
<textarea name='posts2[biography_en]'><?php echo $posts2->biography_en; ?></textarea>
</td>
</tr>
<tr>
<th>
Cv
</th>
<td>
<p class="old">CV</p>
<input type="file" name='cv' id="pdf"></input>
</td>
</tr>
<tr>
<td> </td>
<td>
<input type='submit' name='submit' value='Зачувај' />
</td>
</tr>
</table>
<?php
echo form_close();
?>
<div class="redButton" style="float:left; width: 150px;">
<?php
if(!isset($prof[0]->id)){ //da ne go prikazuva za profesor
echo anchor('professors/', 'Назад до професори');
}
?>
</div>
<?php
}
?>

How do i get variables from a html form using a session wrapper class in PHP?

Ok i have a session class that currently handles setting variables, getting variables, deleting them and destroying them. I have a form which take information from a user and i am not sure how i would get the post variables using a session class rather than just using $_SESSION{'some variable'] to set the variables and use them.
<?php
class sessionClass{
public function _constructor()
{
session_start();
}
public function destroy()
{
session_destroy();
}
public function add($name, $value)
{
if(empty($name)){
die('Invalid variable name');
}
$_SESSION[$name] = $value;
}
public function delete($name)
{
session_unset ($_SESSION[$name]);
}
public function get($name)
{
if(isset($_SESSION[$name]))
$_SESSION[$name] = $name;
else
return ($_SESSION[$name]);
}
}
?>
Above is my session class. Now i am trying to use OOPHP and i am trying to validate the data being entered into my template form page. However because i am passing the variables $_POST['LoginID'] and $_POST['Password'] i am getting these errors.
Notice: Undefined index: LoginID in /home/comp3170-020/public_html/assignment2/index.php on line 20 Notice: Undefined index: LoginID in /home/comp3170-020/public_html/assignment2/index.php on line 25 ect ect
//Creating class objects
$val = new validatorClass();
$ses = new sessionClass();
$dis = new XHTMLDisplayClass();
$dis->setTemplate('templates/index.tpl.php');
//Checking that data is validated
if (isset($_POST['Submit']))
{
if ((!$val->isValidLoginID($_POST['LoginID'])) || (!$val->isValidLoginIDE($_POST['LoginID'])) ||(!isValidPassword($_POST['Password'])))
{
$errs = $val->getErrorMessages();
$d-> addVar('errors', $errs);
}
else {
//$ses = new sessionClass();
$ses->add('LoginID', $_POST['LoginID']);
$ses->add('Password',$_POST['Password'] );
$ses->add('validationDone', true);
header('Location:auth.php');
exit;
}
}
Can anyone tell me how i should pass the variables to the functions of the classes.
form code :
<form name="form1" action="index.php" method="POST">
<td width="220" class="content_l">Login ID</td>
<font color = "FF0000">
<?php if (isset ($errors['isValidLoginID'])){echo $errors['isValidLoginID'];}?>
<?php if (isset ($errors['isValidLoginIDE'])){echo $errors['isValidLoginIDE'];}?>
<?php if (isset ($errors['isValidPassword'])){echo $errors['isValidPassword'];}?>
</font>
</tr>
<tr>
<td><input type="text" name="textfield" class="form250"></td>
</tr>
<tr>
<td height="25" valign="bottom" class="content_l">Password</td>
</tr>
<tr>
<td><input type="password" name="textfield" class="form250" ></td>
</tr>
<tr>
<tr>
<td height="25" valign="bottom" class="content_1">Identification Used</td>
</tr>
<tr>
<td><input type="radio" name="authtyp" value="Username" class="form250" <?php echo $usertyp?> checked>Username <input type="radio" name="authtyp" value= "Email" class="form250" <?php echo $emailtyp?>> Email</td>
</tr>
<tr>
<td height="40" valign="bottom">
<input type="submit" name="Submit" value="LOGIN" class="btn70" style="margin-top:10px; ">
</form>
</td>
</tr>
<tr>
<td height="30" valign="bottom">Forgot your password? </td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>

How to call php function from html form action?

I want to call php function in form action and i want to pass id as a argument. What I am doing is, in html form database column values will be displayed in text boxes, If I edit those values and click 'update' button values in database should be updated and 'Record updated successfully'message should be displayed in same page. I tried below code but not working. Let me know the solution. Thanks in advance.
<html>
<head>
<link rel="stylesheet" type="text/css" href="cms_style.css">
</head>
<?php
$ResumeID = $_GET['id'];
$con = mysql_connect("localhost", "root", "");
mysql_select_db("engg",$con);
$sql="SELECT * from data WHERE ResumeID=$ResumeID";
$result = mysql_query($sql);
$Row=mysql_fetch_row($result);
function updateRecord()
{
//If(!isset($_GET['id']))
//{
$NameoftheCandidate=$_POST[NameoftheCandidate];
$TelephoneNo=$_POST[TelephoneNo];
$Email=$_POST[Email];
$sql="UPDATE data SET NameoftheCandidate='$_POST[NameoftheCandidate]', TelephoneNo='$_POST[TelephoneNo]', Email='$_POST[Email]' WHERE ResumeID=$ResumeID ";
if(mysql_query($sql))
echo "<p>Record updated Successfully</p>";
else
echo "<p>Record update failed</p>";
while ($Row=mysql_fetch_array($result)) {
echo ("<td>$Row[ResumeID]</td>");
echo ("<td>$Row[NameoftheCandidate]</td>");
echo ("<td>$Row[TelephoneNo]</td>");
echo ("<td>$Row[Email]</td>");
} // end of while
} // end of update function
?>
<body>
<h2 align="center">Update the Record</h2>
<form align="center" action="updateRecord()" method="post">
<table align="center">
<input type="hidden" name="resumeid" value="<? echo "$Row[1]"?>">
<? echo "<tr> <td> Resume ID </td> <td>$Row[1]</td> </tr>" ?>
<div align="center">
<tr>
<td> Name of the Candidate</td>
<td><input type="text" name="NameoftheCandidate"
size="25" value="<? echo "$Row[0]"? >"></td>
</tr>
<tr>
<td>TelephoneNo</td>
<td><input type="text" name="TelephoneNo" size="25" value="<? echo "$Row[1]"?>"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="Email" size="25" value="<? echo "$Row[3]"?>">
</td>
</tr>
<tr>
<td></td>
<td align="center"><input type="submit" name="submitvalue" value="UPDATE" ></td>
</tr>
</div>
</table>
</form>
</body>
</html>
try this way
HTML
<form align="center" action="yourpage.php?func_name=updateRecord" method="post">
PHP
$form_action_func = $_POST['func_name'];
if (function_exists($form_action_func)) {
updateRecord();
}
write form action="" and then write your php code as below
note : use form method as get
<?php
if(isset($_GET['id']))
{
call your function here
}
?>
in function access all values using $_GET['fieldname']
simple way make your "Submit " and "Update" action performed on same page then
if(isset($_POST['update']))
{
//perform update task
update($var1,var2,$etc); // pass variables to function
header('Location: http://www.example.com/');// link to your form
}
else if(isset($_POST['submit']))
{
//perform update task
submit($var1,$var2,$etc);// pass variables to function
header('Location: http://www.example.com/'); // link to next page after submit successfully
}
else
{
// display form
}

move_uploaded_file is giving a error

this is my form
<form action="test.php" method="post" name="myform">
<table width="500" border="0">
<tr>
<td width="369" colspan="3">Admin's Area </td>
<td width="121"><?php echo $_SESSION['name'];?></td>
</tr>
<tr>
<td colspan="3">sponseres list </td>
<td>+Add new Sponser</td>
</tr>
<tr>
<td colspan="3"><?php echo $sponsere_list; ?></td>
<td> </td>
</tr>
<tr>
<td align="center" colspan="4"> <a name="sponserForm" id="sponserForm"></a> Add New Sponser Form</td>
</tr>
<tr>
<td align="left">Sponser name</td>
<td align="left"><input type="text" name="spname" id="spname" tabindex="1" /></td>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td align="left">Image</td>
<td align="left"><input type="file" name="fileToUpload" /></td>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td align="left">Add this</td>
<td align="left"><input type="submit" name="sumit" id="sumit" value="Submit" tabindex="3" /></td>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td align="center" colspan="4"> </td>
</tr>
</table>
</form>
and this is the php code to retrive it
<?php
if(isset($_POST['spname'])){
$spname=mysql_real_escape_string($_POST['spname']);
$user_query = "INSERT INTO `sponsers` (`spname`)
VALUES ('{$spname}')
";
$sql=mysql_query($user_query)or die (mysql_error());
$spic= mysql_insert_id();
$newname="$spic.jpg";
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],"../sponsers/$newname")or die (mysql_error());
}
?>
when i try to upload a image it gives me this warning message
Notice: Undefined index: fileToUpload in J:\xampp\htdocs\srimag\admin\test.php on line 3
so i tried to echo the fileToUpload value by using $_POST['fileToUpload'] it show the values without errors so can't figure out the error.
so please help me on this :-(
Thanks.
Your main problem is you are missing the appropriate enctype attribute on your form
<form ... enctype="multipart/form-data">
Make sure you read this section of the manual carefully - http://php.net/manual/en/features.file-upload.php
Your issue is mentioned on the first page
Note:
Be sure your file upload form has attribute enctype="multipart/form-data" otherwise the file upload will not work.
You need enctype="multipart/form-data" in your form to upload images, also it would be a good idea to check if user even uploads an image and specificity naming a file .jpg will not work, images will be treated as corrupt when outputting if ther not jpegs, not to mention people uploading php files.
You also need to make some other checks on validity, upload security is not something that should be overlooked, else you have one of thos awful phone home / botnet malware scripts injecting code into all your scripts:
<?php
if(isset($_POST['spname'])){
$spname=mysql_real_escape_string($_POST['spname']);
$user_query = "INSERT INTO `sponsers` (`spname`)
VALUES ('{$spname}')";
$sql=mysql_query($user_query)or die (mysql_error());
$spic= mysql_insert_id();
if(isset($_FILES["fileToUpload"]["tmp_name"]) && $_FILES["fileToUpload"]["error"] ==0){
$name = basename($_FILES["fileToUpload"]['name']);
$ext = end(explode('.', $name));
$newname = $spic.".".$ext;
$info = getimagesize($_FILES["fileToUpload"]['tmp_name']);
$allowed = array('image/png','image/jpg','image/gif');
if($info[0]>0 && $info[1] > 0 && in_array($info['mime'],$allowed)){
move_uploaded_file($_FILES["fileToUpload"]['tmp_name'], "../sponsers/$newname");
//done upload
}else{
//Not allowed, perhap notify user
}
}
}
?>
include this in form tag
enctype="multipart/form-data"
You should add this in your form
<form action="test.php" method="post" name="myform" enctype="multipart/form-data">

Categories