Gæst
Gæst

Reputation: 133

Finding reason for MySQL query not working

Okay. I have this code on my site:

<?php
session_start();
include "database.php";
include "bruger.php";

$bruger = new bruger();
if( $bruger->logget_ind() ){

  $brugernavn = $_SESSION['brugernavn'];
  $bruger_slash = addslashes($brugernavn);

  $hnavnn = mysql_query(" SELECT hnavn FROM bruger WHERE brugernavn = '" . $bruger_slash . "'");
  $hnavn = mysql_fetch_row($hnavnn);
  $hinfoo = mysql_query(" SELECT besked, Afsender FROM $hnavn[0]");
  while ($hinfo = mysql_fetch_array($hinfoo)){

    echo "<form method = post action = $PHP_SELF>\n";
    echo "Ja<input type=radio name=valg1 value=\"ja\"> Nej<input type=radio name=valg value=\"nej\">";
    echo "<input type=submit value=\"Svar\">\n";

    if (isset($_POST["valg1"])){
      $brugernavn = $_SESSION['brugernavn'];
      $bruger_slash = addslashes($brugernavn);
      $frahvem = $hinfo["Afsender"];
      $query = "UPDATE bruger SET `Status` = '$frahvem' WHERE brugernavn = '$bruger_slash'";
      mysql_query($query) or die (mysql_error());

      $query1 = "UPDATE bruger SET `Ansfra` = 'Nej' WHERE hnavn = '$frahvem'";
      mysql_query($query1) or die (mysql_error());

      echo "Du har nu givet dit svar!";
    }
    echo "<br>";
  }
}
else {
  echo "Du er ikke logget ind";
}
?>

I have been trying to find some help in eight days now, therefore I'm desperate :) It looks as if it only runs the first mysql_query, but not the next one. Hmm..

Some one who can see what the fault(s) is?

(sorry for my bad english ;) .. )

Upvotes: 0

Views: 576

Answers (2)

Chuck Burgess
Chuck Burgess

Reputation: 11574

There could be anything wrong. There is not quite enough information to see this all the way through the debugging process. However, here are a few things to try:

1- Implement error handling to see if there is something wrong with the DB connectivity.

2- Confirm that $bruger_slash actually contains a value.

3- Try adding back ticks to the $hinfoo query:

$hinfoo = mysql_query(" SELECT besked, Afsender FROM `$hnavn[0]`");

4- Update the array reference variable $frahvem = $hinfo["Afsender"]; to use single ticks:

$frahvem = $hinfo['Afsender'];

Upvotes: 1

VolkerK
VolkerK

Reputation: 96159

Any of the mysql_* function can fail. In this case they will return false and mysql_error() can tell you more about the cause.
Use this to implement some kind of error handling.

Rudimentary example:

include "bruger.php";
define('DEBUGOUTPUT', 1);
...

  $bruger_slash = mysql_real_escape_string($brugernavn); // you should pass the mysql connection resource as 2nd paramter here

  $hnavnn = mysql_query("SELECT hnavn FROM bruger WHERE brugernavn = '" . $bruger_slash . "'");
  if ( !$hnavnn ) {
    if ( defined('DEBUGOUTPUT') && DEBUGOUTPUT ) {
      echo 'mysql_error: ', mysql_error(), "\n";
    }
    echo "query failed.";
    return;
  }
  $hnavn = mysql_fetch_row($hnavnn);
  if ( !$hnavn ) {
    echo 'no such brugernavn';
    return;
  }
  $hinfoo = mysql_query(" SELECT besked, Afsender FROM `$hnavn[0]`");
  if ( !$hinfoo ) {
    if ( defined('DEBUGOUTPUT') && DEBUGOUTPUT ) {
      echo 'mysql_error: ', mysql_error(), "\n";
    }
    echo "query failed.";
    return;
  }

Upvotes: 4

Related Questions