2014-02-07 40 views
-2

我想允许用户通过移动它们向上或向下重新排序MySQL表行。我有2页让这个工作。它不工作,我真的不知道该从哪里出发。我收到此错误:Fatal error: Call to undefined method Database::GetAll() in /home/www/thetotempole.ca/phptester/moveupdown.php on line 35这里是我的PHP: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

和'不working'手段? –

+0

致命错误:调用未定义的方法Database :: GetAll()在/home/www/thetotempole.ca/phptester/moveupdown.php在第35行是我的错误。我不确定这意味着什么。 – Kelsey

+1

好的,我们在哪里可以在你的类或'mysqli'中定义一个'GetAll()'方法? –

回答

1

这意味着在数据库类或没有GETALL方法方法签名是不同的。在这种情况下,我没有在mysqli类中看到任何这些方法。检查docs of the mysqli class有效的方法。

既然你从mysqli类扩展您的Database类(顺便说一句不必要的其他评论者所说,你可以只使用new mysqli()),你继承的mysqli的所有方法,这是在上面的链接中列出。其中有一个query方法用于将您的SQL查询发送到服务器。所以,你的那部分代码应该像类似的信息(我不是在这里检查你的逻辑,只需更换数据库相关的东西):

$db = new Database(); 

if(isset($_POST['do'])) { 
    ... 
} else { 
    // make sure all forums positions are numbered 1,2,3,4,5 etc. 
    $sql = "SELECT id FROM employee ORDER BY position"; 
    $forums = $db->query($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); 
    } 

}

+0

我试图在您的链接中搜索,但我没有看到任何方法。我对PHP很新,很抱歉。有什么方法可以将该方法添加到我的$ db类中? – Kelsey

+0

就将此一些细节我的答案,但你需要对面向对象的编程基础教程一些,如[这一个](http://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--网12762)。 – Arthur