gpt4 book ai didi

php - 函数删除测试

转载 作者:行者123 更新时间:2023-11-28 23:38:44 24 4
gpt4 key购买 nike

全局变量级别范围,我正在尝试实现一个删除功能,并在删除查询中用一个全局变量替换表名,如下面的代码,但是当我在用户单击“删除”之前进行测试时,它已定义,但是当点击按钮后,变量是未定义的。

$GLOBALS['$table'] 由用户在不同的函数中填写,当我回显 $GLOBALS['$table'] 首先,它向我展示了值(value),这很好。

<?php

function DeleteOption()
{
include ('../CIEcon.php');

echo "<form action= 'Delete.php' method = 'post'>";
echo $GLOBALS['$table'];
if (isset($_POST['delete'])) {
if (empty($_POST['Id']) || $_POST['Id'] == 0) {
echo "<h4> please choose something to delete </h4>";
echo $GLOBALS['$table'] . "hmmmm";
}
else {
$impid = implode("' , '", $_POST['Id']);
$sqlDelete = "DELETE " . $GLOBALS['$table'] . " WHERE Id IN ('" . $impid . "')";
$DeleteQuery = mysqli_query($dbCIE, $sqlDelete) or die("Error : " . mysqli_error($dbCIE));
}
} // end of delete...
else {
echo "";
}

echo "</form >";
}; // End of Delete Function..

这就是用户定义 $Globle['table'] 的方式

<?php
include('../CIEcon.php');

$GLOBALS['$table']="";




function DisplayOption(){
include('../CIEcon.php');

echo '
<form action= "Delete.php" method = "post">
<table width ="40%" cellpadding ="4" border="1" align="center" >

<tr >
<th style ="color: white; background-color: #f26822 ; " > Select a Catagory To Delete From </th>

</tr>';

echo "<tr>



<td>".

"<select name = lists >
<option name= nothing value= 0 selected >Choose a Catagory</option>
<option name= nothing value= 1 > Advertising </option>
<option name= nothing value= 2> Fiscal </option>
<option name= nothing value= 3> Food </option>
<option name= nothing value= 4> Shopping </option>
<option name= nothing value= 5> Rentals </option>
<option name= nothing value= 6> Setting up </option>
<option name= nothing value= 7> Performances </option>
<option name= nothing value= 8> Registration/Ushering </option>
<option name= nothing value= 9> Master of Ceremonies </option>
<option name= nothing value= 10> Cleaning up </option>
<option name= nothing value= 11> Others </option>

</select>"



." </td>


</tr>";




echo '

</table>
<br/>

<div align="center">
<input type="submit" name="submit" value="submit" />
<input type="reset" value="Clear" />
<hr> <hr>
</div>


</form>

';

///
};



function SelectOption(){
include('../CIEcon.php');

///
if(isset($_POST['submit'])){

if(isset($_POST['submit'])) // second submit
{
$errorMessage = "";

if(($_POST['lists'])== 0) // trying to get error if user don't choose.
{
$errorMessage .= "<li>You Forgot to Choose !</li>";

}

$lists = $_POST['lists']; // <-save info in variable based on user input

if(!empty($errorMessage))
{
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
die();
}

} // end of second submit




switch($lists)
{
case '1':
$GLOBALS['$table'] ="Advertising";
break;
case '2':
$GLOBALS['$table'] ="Fiscal";
break;
case '3':
$GLOBALS['$table']="Food";
break;
case '4':
$GLOBALS['$table'] ="Shopping";
break;
case '5':
$GLOBALS['$table'] ="Rentals";
break;
case '6':
$GLOBALS['$table'] ="SettingUp";
break;
case '7':
$GLOBALS['$table'] ="Performances";
break;
case '8':
$GLOBALS['$table'] ="Registration";
break;
case '9':
$GLOBALS['$table'] ="MasterOfCeremonies";
break;
case '10':
$GLOBALS['$table'] ="Cleaning";
break;
case '11':
$GLOBALS['$table']="Others";
break;

default;
echo 'Unsupported category';
break;
}



if ($GLOBALS['$table'] != ""){

$sql = "SELECT * FROM ". $GLOBALS['$table']. " ";
$result = mysqli_query($dbCIE, $sql) or die(mysqli_error($dbCIE));

//trying this...
$_SESSION["tbl"] = $GLOBALS['$table'];

/// NOW DISPLAY ALL INFO FROM CHOSEN DATABASE...///

echo "
<form action= 'Delete.php' method = 'post'>

<table cellpadding ='4' border='1' width='80%' align='center'>
<tr>

<th>Check </th>
<th>Job's Name</th>
<th>Description</th>
<th> No Students needed</th>
<th>Due Date</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<br>";
echo "<tr>";
echo "<td> <input type='checkbox' name='Id[]' value='". $row['Id'] ."' /> </td>";
// echo "<td> <input type='checkbox' name='Table[]' value='". $GLOBALS['$table'] ."' /> </td>";

echo "<td>" . $row['JobName'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td>" . $row['NoStudent'] . "</td>";
echo "<td>" . $row['DueDate'] . "</td>";
echo "</tr>";


}




echo "</table>";
/// END THE SEARCH HERE...........///

echo " <br>
<div align='center'>
<input type='reset' value='clear' >

<input type='submit' name='delete' value='delete'>

</div>

</form>";




} // End if !table="";


} else { }



///
};// end of function SelectOption

最佳答案

我看到您已经在尝试使用 session 变量的概念。方法如下:

在您的 PHP 文件的最顶部,就在这一行的下方:

$GLOBALS['$table']="";

添加这个:

session_start();
if (isset($_SESSION['table'])) {
$GLOBALS['$table'] = $_SESSION['table'];
}
// rest of your PHP code...

然后在 SelectOption 函数中,紧接着:

switch($lists)
{
...
}

添加以下行:

$_SESSION['table'] = $GLOBALS['$table'];

通过这种方式,您可以在请求中保持所选表的有效性。

不过,我会敦促您重写代码,更好地将输出 HTML 的部分与初始化变量和执行 SQL 的部分分开。将这两者分开得越好,维护代码就越容易。

关于php - 函数删除测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34980173/

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