3

我有文件夹和页面 /admin/add/index.php、/admin/edit/index.php 和 /admin/view/index.php 都需要页面 /admin/php.php ,其中包含我所有的 php 代码每个,我都无法打开流错误。我应该将它们分开<? ?>还是做我做不到的事情或最好的方法?

我的 admin/php.php 文件:

<?php
FOR ADD CONTACT PAGE

require ("../../dbandpassword.php");
$ID = $_POST['ID'];
$firstname = mysqli_real_escape_string($con,$_POST['firstname']);
$lastname = mysqli_real_escape_string($con,$_POST['lastname']);

$sql = "INSERT INTO table (firstname, lastname) VALUES ('$firstname', '$lastname')"; 

mysqli_query($con,$sql) or die ("Error: ".mysqli_error($con));

header("location: http://www.mydomain.com/admin/edit/?ID=".mysqli_insert_id($con));

exit;
}
FOR EDIT CONTACT PAGE

require ("../../dbandpassword.php");
$ID = $_POST['ID'];
$firstname = mysqli_real_escape_string($con,$_POST['firstname']);
$lastname = mysqli_real_escape_string($con,$_POST['lastname']);

$sql = "UPDATE table SET `firstname` = '$firstname', `lasttname` = '$lastname' WHERE `table`.`ID`  = '$ID' LIMIT 1";

mysqli_query($con,$sql) or die ("Error: ".mysqli_error($con));

FOR VIEW CONTACT PAGE

require ("../../dbandpassword.php");

$ID = $_GET['ID'];

$sql = "SELECT * FROM table WHERE ID='$ID' limit 1" or die ('Error: ' .mysqli_error());

$query = mysqli_query($con,$sql) or die ('Error: ' .mysqli_error($con));
$row = mysqli_fetch_array($query);

$space = (!empty($row['firstname']) && !empty($row['lastname'])) ? ' ' : '';        
$name = $row['firstname'].$space.$row['lastname'];       

mysqli_query($con,$sql) or die ("Error: ".mysqli_error($con));

FOR VIEW ALL CONTACTS PAGE

require ("../dbandpassword.php");

$ID=$_GET['ID'];

$query = "SELECT * FROM table WHERE firstname IN ('Bob','Joe') AND lastname = 'Smith' ORDER BY date DESC";

$result=mysqli_query($con,$query);
while ($row = mysqli_fetch_array($result)) {

$space = (!empty($row['firstname']) && !empty($row['lastname'])) ? ' ' : '';        
$name = $row['firstname'].$space.$row['lastname'];   

mysqli_query($con,$query) or die ("Error: ".mysqli_error($con));
}
?>
4

1 回答 1

5

最好创建一个 php 文件,然后为每个操作创建单独的函数,例如

function insert()
{

}

function delete()
{

}

function update()
{

}

function fetch()
{

}
于 2012-06-27T03:55:07.640 回答