0

2 つの PHP ファイルから 1 つの PHP ファイルにアクセスしようとしています。

insert.phpfile を使用して特定のテーブルに値を挿入し、次にmodify.phpfile を使用してテーブルの値を変更します。

database.php挿入用と変更用の2 つの関数を書きたいと思います。そして、このファイルをinsert.php`modify.phpにインクルードしたい

database.phpページ呼び出しから挿入関数のみを実行し、ページが呼び出されたときにinsert.php変更関数のみを実行したいだけです。database.phpmodify.php

これを行う可能性はありますか?

4

2 に答える 2

1

データベース.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();
?>

変更.php

<?php
include("database.php");
modify_fn();
?>
于 2013-09-17T09:00:39.930 に答える
0

あなたが言及した2つの関数でdatabase.phpファイルを作成できます:function update()function insert().

次に、次のように、phpincludeを使用して、database.php ファイルを他の 2 つのファイルに含めます。

insert.php

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

変更.php

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

モデルがあるデータベース層を作成し、これにクラスのインスタンスを使用する必要があります。次に、database.php を他のスクリプトに含め、db オブジェクトをインスタンス化し、メソッドを呼び出します。このようなもの:

データベース.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);

変更.php

include_once(database.php);
$dbObj = new MyDatabaseThingy();
/* make sure you have the connection and so on */
$dbObj->update(your data);
于 2013-09-17T09:03:27.220 に答える