0

I am trying to access one PHP file from two PHP files.

insert.php file is used to insert values into a certain table and then modify.php file is used to modify the values of the table.

I want to write two functions in database.php -- one for inserting and another for modifying. And I want to include this file into insert.php and `modify.php

I just want to execute only insert function from database.php and insert.php page call and execute only the modify function from database.php when modify.php page is called.

Is there any possibility to do this?

6
  • Why not? include file with function declarations and call the functions you need. Commented Sep 17, 2013 at 8:53
  • show the code of database.php Commented Sep 17, 2013 at 8:53
  • if($insert) { include 'insert.php'; } ..... Commented Sep 17, 2013 at 8:53
  • i meant to say that, database.php has 2 functions like insert_values() and modify_values() i have a html page used to insert values and modify values. when i am trying to insert values in a table the insert.php file will call and/or if i am trying to modify the values of table then modify.php will call. i include database.php file in both insert.php and modify.php now i have to execute only insert_values() function from database.php when insert.php file call. as well as for modify.php file also... please explain the solution sir. thanks in advance. Commented Sep 17, 2013 at 9:05
  • check my answer below. stackoverflow.com/a/18845503/1357033 Commented Sep 17, 2013 at 9:05

2 Answers 2

1

database.php

<?php
function insert_fn()
{
//write insert code here
echo "inserted"; //just for demo.
}
function modify_fn()
{
//write modifycode here
echo "modified."; //just for demo.
}
?>

insert.php

<?php
include("database.php");
insert_fn();
?>

modify.php

<?php
include("database.php");
modify_fn();
?>
Sign up to request clarification or add additional context in comments.

5 Comments

Ok sir. after i execute the file. if it will give right output then i will. thank you.
i have 2 form files names form1.php and form2.php . both file has Submit button. when i click on Submit button .... both files will call only one file named execute.php . i write code like form1.php : <form action="execute.php" .......></form> form2.php : <form action="execute.php" .......></form> execute.php have insert() delete() methods. when one form1.php submitted then insert() method would be execute and as well as when form2.php submitted then delete90 method would be call. according to your above answer how can i write code in <form action="??????????"> please give me the solution.
you can set name for each submit button. and in execute.php, check the name of submit button and do the rest. got it?
yes sir. but the question is . i use name ="submit" and value ="submit" in both forms. then let me know how can i get different methods when different forms are submitted??
please change name or value and check that in execute.php or place any hidden field in form and check
0

You can create your database.php file with the two functions you mentioned: function update() and function insert().

You then use the php include to include the database.php file into your other two files, like this:

insert.php

include_once('database.php');
insert(your data comes here);

modify.php

include_once('database.php');
update(your data comes here);

You should make a Database Layer where you have your Model and use an instance of the Class for this. You then include your database.php into your other scripts and instantiate your db object and call your method. Something like this:

database.php

class MyDatabaseThingy 
{
    /* rest of your code */
    public function update(data) {your code here}
    public function insert(data) {your code here}
    /* rest of your code */
}

insert.php

include_once(database.php);
$dbObj = new MyDatabaseThingy();
/* make sure you have the connection and so on */
$dbObj->insert(your data);

modify.php

include_once(database.php);
$dbObj = new MyDatabaseThingy();
/* make sure you have the connection and so on */
$dbObj->update(your data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.