Jump to content

And another question xD (php callout doesnt work properly)


AzeS
Go to solution Solved by AzeS,

Recommended Posts

So first of all there is no error msg at all but as i mentioned ther is something wrong i want to give my users the oppertunity to check on theyre historie i also want them to be able to check on there activities by an specific date though... 
so here is whats happening :
 

First i try to hand over the var :
 

if (isset($_POST['submit'])) {
  $IND_ = date( "Y-m-d", strtotime($_POST['y']   . "-" . $_POST['m'] . "-" . $_POST['d']));
  echo $IND_;
  echo "<<<";
  $ID = $userRow['userId'];
  $result = $crud->thouhistory($ID, $IND_);
  echo $IND_;
  echo "<<<";
 #var_dump($result);
}

So far so good echos the correct date taht the user have been submitted...

But then apperantly this happens in the crud.php 
 

public function thouhistory($UID,$IND) { #Abruf history für user 
	echo " >0 ";
	echo $IND;
	if ($IND == "NONE") {
	try {
	echo " >1 ";
	echo $IND;
	$sql = $this->Db->prepare("SELECT * FROM history WHERE uid=:id");
	$sql->bindparam(":id",$UID);
	$sql->execute(array(':id' => $UID));
	$res = $sql->fetchAll();
		if (count($res)) {
			return $res;
		} else {
			return 0;
		}
	return $row;
	} catch (PDOException $ex) {
		echo $ex->getMessage();
	} 
	} elseif($IND = "--") {
		return 0;
		
	} else {
		echo " >2 ";
		echo $IND;
	try {
	$sql = $this->Db->prepare("SELECT * FROM history WHERE uid=:id AND DATE_IND=:IND");
		echo " >3 ";
		echo $IND;
	$sql->bindparam(":id",$UID);
	$sql->bindparam(":IND",$IND);
	$sql->execute(array(':IND' => $IND,':id' => $UID));
	$res = $sql->fetchAll();
		if (count($res)) {
			return $res;
		} else {
			return 0;
		}
	return $row;
	} catch (PDOException $ex) {
		echo $ex->getMessage();
	} # history $CREDIT['CREDIT'] !!!!!!!!!!!!!!!!!!!!!!!!!!!!!

	}

echos : >0 NONE >1 NONE (1970-01-01<<<)<-submited (>0 1970-01-01)crud (1970-01-01<<<)submitted 
but the hilarios point on this is that it displays : no items to display yet (return 0;)

so in conclusion there is something wrong...

Full code PHP CALL OUT :

 

<?php
 ob_start();
 session_start();
 require_once '../dbconnect.php';
 
 if( !isset($_SESSION['user']) ) {
  header("Location: index.php?reqlog");
  exit;
 }

$userRow=$crud->getuser($_SESSION['user']);
$IND_ = "NONE";
$ID = $userRow['userId'];
$result = $crud->thouhistory($ID, $IND_);
if (isset($_POST['submit'])) {
  $IND_ = date( "Y-m-d", strtotime($_POST['y']   . "-" . $_POST['m'] . "-" . $_POST['d']));
  echo $IND_;
  echo "<<<";
  $ID = $userRow['userId'];
  $result = $crud->thouhistory($ID, $IND_);
  echo $IND_;
  echo "<<<";
 #var_dump($result);
}
if (isset($_POST['submitall'])) {
  $IND_ = "NONE";
  $ID = $userRow['userId'];
  $result = $crud->thouhistory($ID, $IND_);
}
?>

Crud-->
 

public function thouhistory($UID,$IND) { #Abruf history für user 
	echo " >0 ";
	echo $IND;
	if ($IND == "NONE") {
	try {
	echo " >1 ";
	echo $IND;
	$sql = $this->Db->prepare("SELECT * FROM history WHERE uid=:id");
	$sql->bindparam(":id",$UID);
	$sql->execute(array(':id' => $UID));
	$res = $sql->fetchAll();
		if (count($res)) {
			return $res;
		} else {
			return 0;
		}
	return $row;
	} catch (PDOException $ex) {
		echo $ex->getMessage();
	} 
	} elseif($IND = "--") {
		return 0;
		
	} else {
		echo " >2 ";
		echo $IND;
	try {
	$sql = $this->Db->prepare("SELECT * FROM history WHERE uid=:id AND DATE_IND=:IND");
		echo " >3 ";
		echo $IND;
	$sql->bindparam(":id",$UID);
	$sql->bindparam(":IND",$IND);
	$sql->execute(array(':IND' => $IND,':id' => $UID));
	$res = $sql->fetchAll();
		if (count($res)) {
			return $res;
		} else {
			return 0;
		}
	return $row;
	} catch (PDOException $ex) {
		echo $ex->getMessage();
	} # history $CREDIT['CREDIT'] !!!!!!!!!!!!!!!!!!!!!!!!!!!!!

	}

Html --> 
 

<div class="Space_0 Background_1"></div>
<div class="HeZC">
	<h4><?php echo $userRow['UzRI']; ?> this is your history</h4>
  <div class="HeZ Body_E01">
		
    <ul>
      <?php 
        if (isset($result)) {
          if ($result != 0) {
            foreach($result as $row) {
            echo "<li><p>" . $row['subject'] . "</p><p>". $row['DATE_'] . "</p></li>";
          } 
        } else {
          echo "<li><p>there are currently no items to display yet..." . "</p></li>";
        }
        }

 
      ?>


		</ul>
	</div>
</div>
<div class="UcZc">
  <div class="UcZ  Body_E01">
    <h4>Or maybe your searching for an specific entry... </h4>
    <form class="UcZf" method="POST">
      <input type="number" name="y" maxlength="4" placeholder="Year">
      <input type="number" name="d" maxlength="2" placeholder="Day"> 
      <input type="number" name="m" maxlength="2" placeholder="Month">
      <button name="submit">Search</button>
      <p>Or</p>
      <button name="submitall">display all</button>
    </form>
  </div>
Edited by AzeS
Link to comment
Share on other sites

Why are you trying to bind the statement parameters twice? You either use PDOStatement::bindParam(), or you pass an array to PDOStatement::execute(), not both.

 

The function design is also unfortunate. It's a lot of duplicate code, it uses magic strings like “NONE” to control the behavior, and the error handling is weird (never catch exceptions to print the error message on the screen, avoid magic values like “0” to indicate problems).

 

A better approach would be something like this:

public function getUserHistory($userID, $date = null)
{
    // basic query
    $historySQL = '
        SELECT
            -- always select specific columns, not just "everything"
        FROM
            history
        WHERE
            uid = :user_id
    ';
    $historyParameters = ['user_id' => $userID];

    // add date condition if a date is given
    if ($date)
    {
        $historySQL .= ' AND date_ind = :date';
        $historyParameters['date'] = $date;
    }

    $historyStmt = $this->Db->prepare($historySQL);
    $historyStmt->execute($historyParameters);

    return $historyStmt->fetchAll();
}

This has no duplicate code (which makes it much shorter), it uses optional parameters instead of magic strings, and it has proper error handling. If the query fails, you get an exception, otherwise you get a valid result set (which may be empty).

Link to comment
Share on other sites

  • Solution

Thanks alot !!!  :happy-04: 
 

public function thouhistory($UID,$IND) { #Abruf history für user 
	if ($IND == null) {
	try {
	$sql = $this->Db->prepare("SELECT * FROM history WHERE uid=:id");
	$sql->execute(array(':id' => $UID));
	$res = $sql->fetchAll();
		if (count($res)) {
			return $res;
		} else {
			return 0;
		}
	return $row;
	} catch (PDOException $ex) {
		echo $ex->getMessage();
	} 
	} else {
	try {
	$sql = $this->Db->prepare("SELECT * FROM history WHERE uid=:id AND DATE_IND=:IND");
	$sql->execute(array(':IND' => $IND,':id' => $UID));
	$res = $sql->fetchAll();
		if (count($res)) {
			return $res;
		} else {
			return 0;
		}
	return $row;
	} catch (PDOException $ex) {
		echo $ex->getMessage();
	} # history $CREDIT['CREDIT'] !!!!!!!!!!!!!!!!!!!!!!!!!!!!!

	}
	

}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.