I’ve the following code for filename usa.php
$query = "SELECT * FROM usa WHERE deleted='0' AND filename<>''";
$stmt = $conn->prepare($query);
$stmt->execute(array());
$stmtcounterview = $conn->prepare("SELECT COUNT(*) FROM usa WHERE deleted='0' AND filename<>''");
$stmtcounterview->execute(array());
$countview = $stmtcounterview->fetchColumn();
$getstate = isset($_GET['state']) ? htmlspecialchars($_GET['state']) : '';
if(!empty($getstate)){
$query = "SELECT * FROM usa WHERE deleted='0' AND filename<>'' AND state=?";
$stmt = $conn->prepare($query);
$stmt->execute(array($getstate));
$stmtcounterview = $conn->prepare("SELECT COUNT(*) FROM usa WHERE deleted='0' AND filename<>'' AND state=?");
$stmtcounterview->execute(array($getstate));
$countview = $stmtcounterview->fetchColumn();
} else {
$query = "SELECT * FROM usa WHERE deleted='0' AND filename<>''";
$stmt = $conn->prepare($query);
$stmt->execute(array());
$stmtcounterview = $conn->prepare("SELECT COUNT(*) FROM usa WHERE deleted='0' AND filename<>''");
$stmtcounterview->execute(array());
$countview = $stmtcounterview->fetchColumn();
}
The code is working fine, but when i pass usa.php?state=ABC, where ABC doesn’t exists in my database, then it should redirect to usa.php and fetch all records.
So how can i achieve this by passing the right value to $_GET[‘state’] parameter.
New contributor
Chetan Soni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.