为了账号安全,请及时绑定邮箱和手机立即绑定

如何将图像值从语句中的变量传递到另一个 PHP 文件

如何将图像值从语句中的变量传递到另一个 PHP 文件

PHP
绝地无双 2022-11-04 16:45:55
嘿,所以我试图从我的 PHP 文件中传递值,我将数据从数据库中拉到另一个 PHP 文件到一个标签,我试图在我进行查询的同一个文件上显示我的图像,它确实有效,但我需要它在另一个文件中。谢谢    if($image!="")    {        $res = mysqli_query($link,"SELECT * FROM lug_num WHERE lug_id= $image");        $array = array();        ?><br><br><div id = img_lug><?php        while($row=mysqli_fetch_array($res))        {                        global $img;                        $img = $row["image"];            //echo $img;                        ?><img src="<?php echo $img;  ?>"  width='200' height='200' /><?php                                    }            ?></div><?php这是我获取并可以显示图像的文件,我需要另一个文件中的 $img 变量更新所以我尝试了会话它没有工作说我有太多的会话我尝试了功能相同的交易不想工作有什么方法可以用脚本完成吗?谢谢
查看完整描述

2 回答

?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

ajax.php


<?php

    $link=mysqli_connect("localhost","root","123456");

    mysqli_select_db($link,"demo");

     error_reporting(0);

    $make =$_GET["make"];

    $model = $_GET["model"];

    $year = $_GET["year"];

    $oem = $_GET["oem_number"];

    $lug = $_GET["lug_number"];

    $image = $_GET["lug_number"];



if($make!="")

{


    $res = mysqli_query($link,"SELECT * FROM models WHERE make_id= $make");

    echo "<select id='modeldd' onchange='change_model()'>";

    echo "<option selected = '' disabled=''>";echo "Select Model";echo "</option>";

    while($row=mysqli_fetch_array($res))

    {

        echo "<option value = '$row[model_id]'>"; echo $row["model"]; echo "</option>";


    }

    echo "</select>";

}

if($model!="")

{

    $res = mysqli_query($link,"SELECT * FROM year WHERE model_id= $model");

    echo "<select id='yeardd' onchange='change_year()'>";

    echo "<option selected = '' disabled=''>";echo "Select Year";echo "</option>";

    while($row=mysqli_fetch_array($res))

    {


        echo "<option value = '$row[year_id]'>"; echo $row["year"]; echo "</option>";


    }

    echo "</select>";

}

if($year!="")

{

    $res = mysqli_query($link,"SELECT * FROM oem WHERE year_id= $year");

    echo "<select id='oemdd' onchange='change_oem()'>";

    echo "<option selected = '' disabled=''>";echo "Select OEM number";echo "</option>";

    while($row=mysqli_fetch_array($res))

    {


        echo "<option value = '$row[oem_id]'>"; echo $row["oem_number"]; echo "</option>";


    }

    echo "</select>";

}

if($oem!="")

{

    $res = mysqli_query($link,"SELECT * FROM lug_num WHERE oem_id= $oem");

    echo "<select id='lugdd' onchange='change_lug()'>";

    echo "<option selected = '' disabled=''>";echo "Select lug number";echo "</option>";

    while($row=mysqli_fetch_array($res))

    {


        echo "<option value = '$row[lug_id]'>"; echo $row["lug_number"]; echo "</option>";


    }

    echo "</select>";


}

if($lug!="")

{

    $res = mysqli_query($link,"SELECT * FROM fix_type WHERE lug_id= $lug");

    echo "<select>";

    echo "<option selected = '' disabled=''>";echo "Select fix type";echo "</option>";

    while($row=mysqli_fetch_array($res))

    {

        $test = $row["fix_name"];

        echo "<option value = '$row[fix_id]'>"; echo $row["fix_name"]; echo "</option>";


    }

    echo "</select>";

}

$img = ""


if($image!="")

{

    $res = mysqli_query($link,"SELECT * FROM lug_num WHERE lug_id= $image");

    $array = array();

    ?><br><br><div id = img_lug><?php

    while($row=mysqli_fetch_array($res))

    {


        $img = $row["image"];

        //echo $img;


        ?><img src="<?php echo $img;  ?>"  width='200' height='200' /><?php



    }

        ?></div>

<?php } ?>


索引.php


<?php

    $link=mysqli_connect("localhost","root","123456");

    mysqli_select_db($link,"demo");


?>


<!DOCTYPE html>

<html>

<head>

    <title></title>

</head>

<body>

    <form name="form1" action="" method="post">

        <table>

            <tr>

                <td>Select Make</td>

                <td><select id="makedd" onchange="change_make()">

                    <option>Select</option>

                    <?php

                        $res = mysqli_query($link,"SELECT * FROM `manufacturer`");

                        while($row=mysqli_fetch_array($res))

                        {

                            ?>


                            <option value="<?php echo $row["make_id"];?>"><?php echo $row["make"] ?></option>

                            <?php

                        }

                    ?>

                </select></td>

            </tr>

            <tr>

                <td>Select Model</td>

                <td>

                    <div id="model">

                        <select>

                            <<option selected="" disabled="">Select Manufacturer</option>

                        </select>

                    </div>

                </td>


            </tr>

            <tr>

                <td>Select Year</td>

                <td>

                    <div id="year">

                        <select>

                            <option>Select Year</option>


                        </select>

                    </div>

                </td>


            </tr>

            </tr>

            <tr>

                <td>Select OEM Number</td>

                <td>

                    <div id="oem">

                        <select>

                            <option>Select OEM Number</option>


                        </select>

                    </div>

                </td>


            </tr>                       

            <tr>

                <td>Select lug Number</td>

                <td>

                    <div id="lug">

                        <select>

                            <option>Select Lug Number</option>


                        </select>

                    </div>

                </td>


            </tr>

            <tr>

                <td>Select Fix type</td>

                <td>

                    <div id="fix" >

                        <select>

                            <option>Select Fix type</option>


                        </select>

                    </div>

                </td>


            </tr>

            <tr>

                <td>Image from lug</td>

                <td><div id="image">


                </div>

                </td>


            </tr>



        </table>

        <?php

        include "ajax.php";


        ?><img src="<?php include "ajax.php"; echo $img;  ?>"  width='200' height='200'  /><?php

        ?>


    </form>

<script type="text/javascript">

function change_make(){

    var xmlhttp=new XMLHttpRequest();

    xmlhttp.open("GET","ajax.php?make="+document.getElementById("makedd").value,false);

    xmlhttp.send(null);


    document.getElementById("model").innerHTML=xmlhttp.responseText;


}


function change_model() {

    var xmlhttp=new XMLHttpRequest();

    xmlhttp.open("GET","ajax.php?model="+document.getElementById("modeldd").value,false);

    xmlhttp.send(null);


    document.getElementById("year").innerHTML=xmlhttp.responseText;

}

function change_year() {

    var xmlhttp=new XMLHttpRequest();

    xmlhttp.open("GET","ajax.php?year="+document.getElementById("yeardd").value,false);

    xmlhttp.send(null);


    document.getElementById("oem").innerHTML=xmlhttp.responseText;

}

function change_oem() {

    var xmlhttp=new XMLHttpRequest();

    xmlhttp.open("GET","ajax.php?oem_number="+document.getElementById("oemdd").value,false);

    xmlhttp.send(null);


    document.getElementById("lug").innerHTML=xmlhttp.responseText;

}

function change_lug() {

    var xmlhttp=new XMLHttpRequest();

    xmlhttp.open("GET","ajax.php?lug_number="+document.getElementById("lugdd").value,false);

    xmlhttp.send(null);


    document.getElementById("fix").innerHTML=xmlhttp.responseText;

}



</script>

</body>

</html>



查看完整回答
反对 回复 2022-11-04
?
慕娘9325324

TA贡献1783条经验 获得超4个赞

你尝试的方式是错误的。看,你的 $image 变量可以是全局的,但你的循环不是全局的。它会给你循环的最后一个值。


1.将 $img 变量更改为本地变量。

2.然后创建一个新的全局变量。并给它这样的值。


global $global_images;

$global_images.= "@".$row["image"]; // @ is just a separator.

3.现在在另一个页面(你想要的地方),你必须再次创建基本循环,像这样


include "2.php";


$get_images=explode('@', $global_image);  //We can choose one by one the images with @ and explode function

//The loop

foreach ($get_images as $show){?>

<img src="<?php echo $show;  ?>"  width='200' height='200'>

<?php } ?>

据我了解,这正是您想要的。现在您可以根据需要对其进行自定义。


查看完整回答
反对 回复 2022-11-04
  • 2 回答
  • 0 关注
  • 132 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号