PHP-在MySQL查询中的另一个函数中使用HTML表单函数中的变量

我试图在另一个函数的MySQL查询中使用由一个函数的表单创建的变量。简而言之,我只想在餐厅页面菜单页面上显示特定餐厅的菜单项。

showMenu()函数,需要显示连接到餐厅的菜单项。

如果我用下面的$ name代替餐厅的实际价值,则此查询有效。它不适用于$ name值。

function showMenu() {
   global $connection;
   $query = "SELECT MenuID,ItemName,Description,Price,ItemType FROM menu,restaurants where menu.RestaurantID = restaurants.RestaurantID and restaurants.Name='".$name."'";
   $result = mysqli_query($connection,$query);

       if ($result->num_rows > 0) {
           echo "<table class='sortable'><tr><th>Name</th><th>Description</th><th>Price</th><th>ItemType</th></tr>";
           // output data of each row
           while($row = $result->fetch_assoc()) {
               $o = '<tr><td data-label="Name">' . " " . $row["ItemName"]. '</td><td data-label="Description">' . " " . $row["Description"]. '</td><td data-label="Price">' . " " . $row["Price"]. '</td><td data-label="Type">' . " " . $row["ItemType"]. '</td></tr>';
               echo $o;
           }
           echo "</table>";
       }   else {
           echo "0 results";
           }
}

添加餐厅代码

function addRestaurant() {
    if(isset($_POST['submit'])) {
        global $connection;
        $name = $_POST['name'];
        $address = $_POST['address'];
        $city = $_POST['city'];
        $state = $_POST['state'];       
        $zipcode = $_POST['zipcode'];
        $googlemapslink = $_POST['googlemapslink'];
        $restauranttype = $_POST['restauranttype'];
        $website = $_POST['website'];
        $logo = $_POST['logo'];
        $sitelink = $_POST['sitelink'];

        if ($googlemapslink == "") {
            $googlemapslink = "https://youtu.be/dQw4w9WgXcQ";
        }

        if ($website == "") {
            $website = "https://youtu.be/dQw4w9WgXcQ";
        }

        if ($logo == "") {
            $logo = "https://youtu.be/dQw4w9WgXcQ";
        }

        $name = mysqli_real_escape_string($connection,$name);
        $address = mysqli_real_escape_string($connection,$address);
        $city = mysqli_real_escape_string($connection,$city);
        $state = mysqli_real_escape_string($connection,$state);
        $zipcode = mysqli_real_escape_string($connection,$zipcode);
        $googlemapslink = mysqli_real_escape_string($connection,$googlemapslink);
        $restauranttype = mysqli_real_escape_string($connection,$restauranttype);
        $website = mysqli_real_escape_string($connection,$website);
        $logo = mysqli_real_escape_string($connection,$logo);
        $sitelink = mysqli_real_escape_string($connection,$sitelink);

        $query = "INSERT INTO `restaurants` (Name,Address,City,State,ZipCode,GoogleMapsLink,Website,RestaurantType,RestaurantLogo,SiteLink) ";
        $query .= "VALUES (";
        $query .= "'$name',";
        $query .= "'$address',";
        $query .= "'$city',";
        $query .= "'$state',";
        $query .= "'$zipcode',";
        $query .= "'$googlemapslink',";    
        $query .= "'$website',";
        $query .= "'$restauranttype',";
        $query .= "'$logo',";
        $query .= "'$sitelink'); ";

        $filesite = "restaurants/" . $sitelink;
        $file = "restaurants/menu.php";
        $contents = file_get_contents($file);

        file_put_contents($filesite,$contents);

        $result = mysqli_query($connection,$query);
            if(!$result) {
                die("Query failed." . mysqli_error($connection));
            } else {
                echo "Record updated!";
            }
    }
}

我已经查看了Stackoverflow上的几乎所有内容,并且无法将对其他人有用的东西转换为适用于我的情况的东西。在addRestaurant()下使$ name全局无效,在showMenu()下使$ name全局无效。

有什么想法吗?

lmy87488860 回答:PHP-在MySQL查询中的另一个函数中使用HTML表单函数中的变量

尝试将变量名称发送给函数。

showMenu($name) { 
   // code to do things
} 

您在查询中使用$name,但没有将其传递给函数。

本文链接:https://www.f2er.com/3162799.html

大家都在问