如何在同一个php页面上的其他php脚本中使用php变量

时间:2022-08-24 14:47:56

The below is my source code where I want to use the variable $r1 which is at the bottom php script in another php script which is on top on the same page. I need a simple solution which solves this problem. I want to use that variable in the update query which is present in the code.

下面是我的源代码,我想使用变量$ r1,它位于同一页面上的另一个PHP脚本的底部php脚本中。我需要一个简单的解决方案来解决这个问题。我想在代码中出现的更新查询中使用该变量。

<?php
$con=mysql_connect("localhost","root","") or die("could not connect to db");

mysql_select_db("test"); 


$valid_formats = array("jpg", "png", "gif", "zip", "bmp","MP4","3GP");
$max_file_size = 1024*10000; //100 kb
//$path = "uploads/"; // Upload directory
$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    // Loop $_FILES to execute all files
    foreach ($_FILES['files']['name'] as $f => $name) {     
        if ($_FILES['files']['error'][$f] == 4) {
            continue; // Skip file if any error found
        }          
        if ($_FILES['files']['error'][$f] == 0) {              
            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name is too large!.";
                continue; // Skip large files
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            }
            else{ // No error found! Move uploaded files 
                move_uploaded_file($_FILES["files"]["tmp_name"][$f],"uploads/" . $_FILES["files"]["name"][$f]); 
                     // Number of successfully uploaded files
                     $file="uploads/".$_FILES["files"]["name"][$f];

              /*  $sql = "Update media set path = '$file' where username = '$r1'";             
     if (!mysql_query($sql))
     {
        die('Error: ' . mysql_error());
     }*/
                }
            }
        }
    }


$sql="select * from media";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query))
{
    $image=$row[2];
    if($image!='')
    {
    echo "<img src='".$row['path']."' width='175' height='200' />";
    }
}



?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Multiple File Upload with PHP - Demo</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="wrap">
        <h1><a>Multiple File Upload with PHP</a></h1>
        <?php
        # error messages
        if (isset($message)) {
            foreach ($message as $msg) {
                printf("<p class='status'>%s</p></ br>\n", $msg);
            }
        }
        # success message
        if($count !=0){
            printf("<p class='status'>%d files added successfully!</p>\n", $count);
        }
        ?>
        <p>Max file size 100kb, Valid formats jpg, png, gif</p>
        <br />
        <br />
        <!-- Multiple file upload html form-->


        <form action="" method="post" enctype="multipart/form-data">

            <span style="font-size:12pt;">Channel:  </span> <select name="channels"> 
                        <option value="">Channel</option>
                        <?php 


                        $sql_query="SELECT * FROM media";
                        $result1 = mysql_query($sql_query) or die(mysql_error());
                        while($data=mysql_fetch_row($result1))
                        {                       
                        ?>
                        <option value="<?php  $r1 = $data[0]; echo $r1;?>" ><?php $r1 = $data[1]; echo $r1 ?></option>
                        <?php
                        }

                        ?>

                        </select>

            <input type="file" name="files[]" id="image" multiple="multiple">
            <input type="submit" value="Upload">
        </form>
</div>
</body>
</html>

1 个解决方案

#1


0  

If you want to have the value of the <select name="channels"> you can use this

如果你想拥有

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){

   $r1 = $_POST["channels"];
   //your code here

}

Hope this help.

希望这有帮助。

#1


0  

If you want to have the value of the <select name="channels"> you can use this

如果你想拥有

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){

   $r1 = $_POST["channels"];
   //your code here

}

Hope this help.

希望这有帮助。