Jump to content

using Post varible to select and display data


telsiin

Recommended Posts

hi all I am missing something in my syntax I am trying to retrieve and display data, base on user input from a form 

thanks in advance

 

<?php require_once('Connections/testdb.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
$giftcards = '$_POST[giftcard]';

mysql_select_db($database_testdb, $testdb); 
$query_Recordset1 = sprintf("SELECT * FROM giftcard WHERE card_id =  '%s'",   mysql_real_escape_string($giftcards)); 
$Recordset1 = mysql_query($query_Recordset1, $testdb) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Validate Gift Card</title>
</head>

<body>

<form action="<?php echo $editFormAction; ?>"  method="Post" enctype="application/x-www-form-urlencoded" name="validategiftcard" target="_self">
<input name="giftcard" type="text" value="234oeo4" maxlength="16" />
<input name="validate" type="reset" value="Validate Giftcard" />
</form>

<table>
  <tr>
    <td>id</td>
    <td>transaction_id</td>
    <td>card_id</td>
    <td>purchaser</td>
    <td>phone_pur</td>
    <td>recipient</td>
    <td>phone_rec</td>
    <td>purchase_date</td>
    <td>expiration_date</td>
    <td>amount</td>
    <td>location</td>
    <td>memo</td>
    <td>occasion</td>
    <td>validation</td>
    <td>use_date</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><?php echo $row_Recordset1['id']; ?></td>
      <td><?php echo $row_Recordset1['transaction_id']; ?></td>
      <td><?php echo $row_Recordset1['card_id']; ?></td>
      <td><?php echo $row_Recordset1['purchaser']; ?></td>
      <td><?php echo $row_Recordset1['phone_pur']; ?></td>
      <td><?php echo $row_Recordset1['recipient']; ?></td>
      <td><?php echo $row_Recordset1['phone_rec']; ?></td>
      <td><?php echo $row_Recordset1['purchase_date']; ?></td>
      <td><?php echo $row_Recordset1['expiration_date']; ?></td>
      <td><?php echo $row_Recordset1['amount']; ?></td>
      <td><?php echo $row_Recordset1['location']; ?></td>
      <td><?php echo $row_Recordset1['memo']; ?></td>
      <td><?php echo $row_Recordset1['occasion']; ?></td>
      <td><?php echo $row_Recordset1['validation']; ?></td>
      <td><?php echo $row_Recordset1['use_date']; ?></td>
    </tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

I am not quite sure what was wrong I think it was the form, : :) I started again fresh and solved it

 

<?php require_once('Connections/testdb.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_validate = "-1";
if (isset($_GET['giftcard'])) {
  $colname_validate = $_GET['giftcard'];
}
mysql_select_db($database_testdb, $testdb);
$query_validate = sprintf("SELECT * FROM giftcard WHERE card_id = %s", GetSQLValueString($colname_validate, "text"));
$validate = mysql_query($query_validate, $testdb) or die(mysql_error());
$row_validate = mysql_fetch_assoc($validate);
$totalRows_validate = mysql_num_rows($validate);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Validate Gift Card</title>
</head>

<body>

<form action="getgift2.php" method="GET">
Gift Card: <input type="text" name="giftcard" />

<input type="submit" value="Validate your Gift Card " />
</form> 

Your Gift card Number is <?php echo $_GET["giftcard"]; ?>!<br />


<table border="1">
   <?php do { ?>
   
   
   	<tr><td><strong>Validation       :</strong></td><td><strong><?php echo $row_validate['validation']; ?></strong></td></tr>
    <tr><td><strong>Amount   :</strong></td><td><strong><?php echo $row_validate['amount']; ?></strong></td></tr>
    <tr><td>Purchaser        :</td><td><?php echo $row_validate['purchaser']; ?></td></tr>
    <tr><td>Purchaser Phone  :</td><td><?php echo $row_validate['phone_pur']; ?></td></tr>
    <tr><td>Recipient        :</td><td><?php echo $row_validate['recipient']; ?></td></tr>
    <tr><td>Recipient Phone  :</td><td><?php echo $row_validate['phone_rec']; ?></td></tr>
    <tr><td>Purchase Date    :</td><td><?php echo $row_validate['purchase_date']; ?></td></tr>
    <tr><td>Expiration Date  :</td> <td><?php echo $row_validate['expiration_date']; ?></td></tr>
    <tr><td>Location         :</td><td><?php echo $row_validate['location']; ?></td></tr>
    <tr><td>Memo             :</td> <td><?php echo $row_validate['memo']; ?></td></tr>
    <tr><td>Occasion         :</td><td><?php echo $row_validate['occasion']; ?></td></tr>  
  
      <?php } while ($row_validate = mysql_fetch_assoc($validate)); ?>
</table>






</body>
</html>
<?php
mysql_free_result($validate);
?>

when I change to

 

$giftcards = $_POST['giftcard'];

 

 

I get the following error

Notice: Undefined index: giftcard in C:\xampp\htdocs\pacino\getgift2.php on line 33

 

In addition to the difference mentioned by Picachu, in the new version you did validate $_GET['giftcard'] with isset() ... in the previous version you didn't , hence the error.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.