我试图允许用户通过向上或向下移动来重新排序mySQL表中的行。我有2页允许这个工作。它不起作用,老实说我不知道从哪里开始。我收到此错误:Fatal error: Call to undefined method Database::GetAll() in /home/www/thetotempole.ca/phptester/moveupdown.php on line 35
这里是我的php:
moveupdown.php
<?php
class Database extends mysqli {
function __construct() {
parent::__construct("","","","");
if (mysqli_connect_errno()) {
throw new Exception(mysqli_connect_error(),
mysqli_connect_errno());
}
}
}
include 'connect.php';
$db = new Database();
if(isset($_POST['do'])){
extract($_POST);
//determine what direction in relation to $_POST['position']
$otherpos = $do=='⇑'? $position-1:$position+1;
//get the two ID that should change order place
$sql = "SELECT id FROM employees WHERE emp_id=$position";
$posid = $db->GetRow($sql);
$sql = "SELECT id FROM employees WHERE emp_id=$otherpos";
$other = $db->GetRow($sql);
//change place for those two
$sql = "UPDATE employees SET position=$otherpos WHERE id=$posid->id";
$db->Query($sql);
$sql = "UPDATE forums SET position=$position WHERE id=$other->id";
$db->Query($sql);
}else{
// make sure all forums positions are numbered 1,2,3,4,5 etc.
$sql = "SELECT id FROM employee ORDER BY position";
$forums = $db->GetAll($sql);
foreach($forums AS $f){
$items[] = $f->id;
}
foreach($items AS $k=>$id){
$k++;
$sql = "UPDATE employee SET position=$k WHERE id=$id";
$db->Query($sql);
}
}
//get the number of forums to display
$sql = "SELECT COUNT(*) AS max FROM employee";
$pos = $db->GetRow($sql);
//get them by order of position
$sql = "SELECT * FROM employee ORDER BY position";
$forums = $db->GetAll($sql);
//display with up/down arrows for each
//except first forum has only down-arrow
//and last forum has only up-arrow
$out ='<table>'."\n";
foreach($forums as $f){
$out .= '<form method="post">';
$out .= '<input type="hidden" name="position" value="'.$f->position.'">'."\n".'<tr>';
if($f->position != 1)
$out .= '<td><input type="submit" name="do" value="⇑"></td>'; //up
else $out .= '<td></td>';
if($f->position != $pos->max)
$out .= '<td><input type="submit" name="do" value="⇓"></td>'; //down
else $out .= '<td></td>';
$out .= '<td>'.$f->name.'</td></tr>'."\n";
$out .= '</form>'."\n";
}
$out .= '</table>';
echo $out;
?>
connect.php
<?
//the example of MySQL database connection
//connect.php
$continued = mysql_connect("","","","");
if ($continued) {
echo ("Connection is succeed");
} else {
echo ("Connection is fail");
}
?>
答案 0 :(得分:1)
这意味着Database类中没有GetAll方法,或者方法签名不同。在这种情况下,我没有在mysqli类中看到任何这些方法。检查docs of the mysqli class是否有效方法。
由于你扩展了你的Database
类(不像其他评论者提到的那样,你可以只使用new mysqli()
类),你继承了列出的所有mysqli方法在上面的链接。其中有一个mysqli
方法,用于将SQL查询发送到服务器。所以你的代码部分看起来应该像(我不是在这里检查你的逻辑,只是替换与数据库相关的东西):
query
}