首先,非常感谢这个伟大的网站。
我在大学的最后一年,我在我的学校的网站上工作。 我有一段时间没有这个问题,我无法解决这个问题,如果你有人帮我,我会很感激,
首先,当管理员登录并转到Delete.php时,点按(他/她)将有一个下拉列表来选择要转到的页面,然后单击“提交”按钮以显示该页面中的所有数据,并且有复选框在列出的所有作业旁边选择,然后选择要删除的作业,然后单击删除按钮删除指定的作业。
我的代码一直有效,直到用户选择并显示来自多个表的所有数据,但它不会被删除..
有三个功能,SelectOption()
,DisplayOption()
,DeleteOption()
,问题具体是如何添加所选表来删除查询..
因为如果您放置一个表名并从下拉列表中选择它,它将起作用..但我希望它是用户可选的。
这是我的代码:
<?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));
/// 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>" . $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="";
mysqli_close($dbCIE);
//
} else { }
///
};// end of function SelectOption
function DeleteOption(){
include('../CIEcon.php');
$tbl = $GLOBALS['$table'] ;
echo $tbl ." beggning of function" ;
if(isset($_POST['delete'])) {
if( empty($_POST['Id']) || $_POST['Id'] == 0 ){
echo"<h4> please choose something to delete </h4>";
}else{
echo "test 1: pass ";
$impid = implode("' , '" , $_POST['Id']);
echo $GLOBALS['$table']."before SQL QUERY" ;
$sqlDelete = "DELETE FROM ". $GLOBALS['$table']. " WHERE Id IN ('" . $impid . "')";
$DeleteQuery = mysqli_query($dbCIE,$sqlDelete) or die ("Error : ".mysqli_error($dbCIE));
}
}// end of delete...
}; // End of Delete Function..
function CloseDB(){};
?>
</style>
<head><title> ..DeleteFFF.. </title></head>
DeleteFFF.php
<body>
<!-- <a href= "../AdminIndex.php" > <button> Main Page </button></a> -->
</body>
</html>
答案 0 :(得分:1)
在DELETE查询中使用全局变量(可能在执行删除POST时可能未设置),而是在单击“删除”时使用POST传递表名。
您已使用$_POST['Id']
echo "<td> <input type='checkbox' name='Id[]' value='". $row['Id'] ."' /> </td>";
你可以用表名做同样的事情:
echo "<td> <input type='checkbox' name='Table[]' value='". $GLOBALS['$table'] ."' /> </td>";
然后您可以使用$_POST['Table']
在删除查询中使用它。
理想情况下,你可以在任何地方删除全局变量(一般都是个坏主意),但这是最快的解决方案。