If there is data the first part works great. When there are no $res the else portion is not echoing.
IO have tried using this but the ! $row stops and will not echo anything inside. so I rewrote it to the php script below which is more straight forward. I was now that worried about sql injection since I am cleaning id as well as verifying the user is actually logged into the system.
$stmt = $conn->prepare('SELECT * FROM table WHERE ID=?');
$stmt->bindParam(1, $_GET['id'], PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if( ! $row)
{
die('nothing found');
}else {
enter code here
}
Thjs is my entire php page.
$db = new PDO (connection string. )
<?php
error_reporting(0);
include_once("php_includes/db_conx.php");
if($_GET['id'] != ''){
$id = preg_replace('#[^0-9]#', '', $_GET['id']);
$sql="SELECT nextdue, alert, completed, page_id, page_type FROM medical where id=$id limit 1";
$alertdiv = "";
if ($res = $db->query($sql)) {
$alertdiv = "";
foreach( $db->query($sql) as $data ) {
//if alert is set to Y create div to show the type and stuff.
$timestamp = $data[0];
if ($data[1]=='y'){
if($timestamp > date("Y-m-d")) {
$alertdiv .= "This alert is in the Future.<br>";
}
if($timestamp < date("Y-m-d")) {
$alertdiv .= "This alert is pastdue.<br>";
}
if($timestamp == date("Y-m-d")) {
$alertdiv .= "This alert is due Today.<br>";
}
$alertdiv .= "<table><tr><td width='50'>Method</td><td>Description</td><td>Destination</td><td>Completed</td><td>Date Due</td><td>Action</td></tr><br>";
$completed = $data[2];
$page_id = $data[3];
$page_type = $data[4];
//check completed
//get page details
switch ($page_type) {
case "d":
$alertdiv .= "<tr id='selectedmethodtr' value='d0'>";
$alertdiv .= "<td width='10'>Default</td><td>Dashboard</td><td>Dashboard</td>";
break;
case "e":
$alertdiv .= "<tr id='selectedmethodtr' value='e$data[3]'>";
$sql1 = "SELECT description, email from page_email where page_email_id=$page_id";
foreach( $db->query($sql1) as $data1 ) {
$alertdiv .= "<td width='50'>E-Mail</td><td>$data1[0]</td><td>$data1[1]</td>";
}
break;
case "p":
$alertdiv .= "<tr id='selectedmethodtr' value='p$data[3]'>";
$sql1 = "SELECT description, phone, carriervalue from page_phone where page_phone_id=$page_id";
foreach( $db->query($sql1) as $data1 ) {
$alertdiv .= "<td width='50' >E-Mail</td><td>$data1[0]</td><td>$data1[1]</td>";
}
break;
}
switch ($completed) {
case "0":
$alertdiv .= "<td>No</td>";
break;
case "1":
$alertdiv .= "<td>Yes</td>";
break;
}
$alertdiv .= "<td>$timestamp</td>";
$alertdiv .= "<td><a onClick=deleteAlert($id) id='deleteAlert'><i class='fa fa-trash-o fa-lg'></i></a></td></tr>";
}
}//end if data[1]
$alertdiv .= "</table>";
echo $alertdiv;
}else {
$alertdiv .= "Alert Type is set Default Dashboard! <br>";
$alertdiv .= "<table id='selectedmethodtable'>";
$alertdiv .= "<tr>";
$alertdiv .= "<td>Description</td>";
$alertdiv .= "<td >Method</td><option id='selectedmethodtr' value='d0' ></option>";
$alertdiv .= "</tr>";
$alertdiv .= "<tr ><td>DashBoard</td><td>Default</td></tr>";
$alertdiv .= "</table>";
echo $alertdiv;
}
}
?>