dsljpwi494719 2019-04-26 16:04
浏览 73

从数据库中提取id后包含PHP文件名

So when I add a product, an id is assigned and a file is created with the product id, eg 1000.php. I don't know how to call it in the product.php file to be included:

I know what I did in the code pasted below is wrong but I don't know how to make it call the 1000.php file by extracting the id from sql first and then include it in the same file. Any advice?


<?php 
function between($start, $end, $string) {
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}
$pid = $_GET['id'];
$pid = trim($pid);
include('php/config.php');
$sql = "SELECT * FROM products WHERE pid='".$pid."'";
    $result = $config->query($sql);
    $num_row = mysqli_num_rows($result);
       if($num_row == 1 ) {
         $row = mysqli_fetch_array($result);
         extract($row);
        $display = 1; } else { $display = 0; }
include('".$pid.".php');

  • 写回答

1条回答 默认 最新

  • douhui9192 2019-04-26 16:26
    关注

    If I'm understanding well what you are asking help for, before this line: include('".$pid.".php');

    you should create the file and write in it all the data you need in it; something like this could work:

    <?php 
    function between($start, $end, $string) {
        $string = ' ' . $string;
        $ini = strpos($string, $start);
        if ($ini == 0) return '';
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;
        return substr($string, $ini, $len);
    }
    $pid = $_GET['id'];
    $pid = trim($pid);
    include('php/config.php');
    $sql = "SELECT * FROM products WHERE pid='".$pid."'";
        $result = $config->query($sql);
        $num_row = mysqli_num_rows($result);
           if($num_row == 1 ) {
             $row = mysqli_fetch_array($result);
             extract($row);
            $display = 1; } else { $display = 0; }
    try {
     $handle = fopen($dir.$filename, 'x');//look at https://www.php.net/manual/en/function.fopen.php on why to use 'x'
     fwrite($fp, $allTheDataYouWantToWrite);
     fclose($handle);
    }catch(\Exception $e){
    //do something if exception happens
    }
    

    After that you have the file created in that directory and its ready for using it Take in account that include is used to read php code and maybe is not that what you want...

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100