2013-07-18 19:22
浏览 57


This is a weird problem. I have a field called status in a database table called Parents. Via a php script, I entered a bunch of parents with status of 'active'. Later, I used phpmyadmin to change two of them to a status of 'dormant'. When I run a query asking for rows with status of active, all is well in both phpmyadmin and in my php script. When I run a query asking for rows with status of dormant, phpmyadmin returns the two rows whose status is dormant, but my php script does not return any rows. Here are the relevant pieces of the code:

// get value of radio button for $active
$active = stripslashes(strip_tags($_POST['active']));
if (!$active) {
  $active = 'active';

// Similar process for value of $myOrderby, 
// which can have value of 'name' or 'email' and works fine

$query = "SELECT 
  FROM Parents, Students
  WHERE Parents.parentID=Students.parentID
    AND Parents.status=:active
  ORDER BY $myOrderby ASC" ;
$stmt = $db->prepare($query);
$stmt->bindValue(':active', $active, PDO::PARAM_STR);

try {
   $affected_rows = $stmt->rowCount();
   if ($affected_rows > 0) {
      // various actions...
} catch (PDOException $ex) {
   $message = $ex->getMessage() ;
   $filename = 'admin-parents' ;
   notifyMe($message, $filename) ;

Curiously, however, if I again use phpmyadmin to change the two rows whose status is 'dormant' back to a status of 'active' (not using any punctuation in the input box) those two rows do NOT show up in the php results for all active parents. That is the problem that started me on this entire quest.

CLARIFICATION: myphpadmin query ALWAYS returns correct result for these two rows, both when status is 'active' and when status is 'dormant'. (I change them via myphpadmin.) However, php script NEVER returns these two rows, regardless of their status.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyong9224 2013-07-26 00:42

    It turns out: The two rows in Parent table with status of 'dormant' did NOT have any rows in the Student table. The query returns only rows where a Parent also has a Student. Which is not exactly what I intended. Back to the drawing board! But I am SO relieved to understand the source of the problem! Thanks everyone! :-)

    打赏 评论
  • drl971115 2013-07-18 23:58

    You need to set which button was selected by the user, use result as parameter in query Assuming radio buttons set up as

    <Input type = 'radio' Name ='active' value= 'active'>Active
    <Input type = 'radio' Name ='active' value= 'dormant'>Dormant
    // get value of radio button for $active
    $active = $_POST['active'];

    Assign parameters

    $stmt->bindValue(':active', $active, PDO::PARAM_STR);
    打赏 评论

相关推荐 更多相似问题