gpt4 book ai didi

php - 可以将 INSERT、UPDATE、DELETE、SELECT 查询放在同一个文件中吗

转载 作者:可可西里 更新时间:2023-11-01 07:59:40 28 4
gpt4 key购买 nike

我有文件夹和页面/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));
}
?>

最佳答案

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

function insert()
{

}

function delete()
{

}

function update()
{

}

function fetch()
{

}

关于php - 可以将 INSERT、UPDATE、DELETE、SELECT 查询放在同一个文件中吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11219139/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com