Jump to content

Select query from $_POST


yandoo

Recommended Posts

Hi there, this really is starting to hurt me now!

 

 

I have 2 identical pages the BOTH contain the same form for inputting details on an animal such as: name, age, sex, animal type & BREED.

 

The only differece is that on 2nd page there is an added detail list/menu "Breed"!

 

On the first page the user enter details in the form which is then inserted into the db and they are taken to the 2nd page of the form (a query selects the most recent entry in db to displays record).

 

Here the user is to input the animals Breed from list/menu. In order to make it ONLY the appropriate breeds appear list/menu (e.g. If animal type is of a CAT then only Breeds of cats will be displayed)

 

I have this code to try to acheieve this:

$query_Recordset1 = "SELECT * FROM breed WHERE AnimalTypeID='{$_POST[`AnimalTypeID`]}'";

 

 

In addition i have creted a hidden field that hold this  record.

 

My problem is i cant get this thing working..??! When i test the query it returns 0 records???! I have echoed the hidden form to see if it  holding the correct value and it is???

 

Please help me ive been this for days now!

 

Heres the entire 2nd page of code:

 

<?php require_once('Connections/woodside.php');?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE animal SET AnimalTypeID=%s, BreedID=%s, Name=%s, Age=%s, Sex=%s, `Date`=%s, HouseTrained=%s, ExpCat=%s, ExpDog=%s, ExpCar=%s, ExpChild=%s, ExpAlone=%s, ExpOnlead=%s, ExpOffLead=%s, Valid=%s WHERE AnimalID=%s",
                       GetSQLValueString($_POST['AnimalTypeID'], "text"),
                       GetSQLValueString($_POST['BreedID'], "text"),
                       GetSQLValueString($_POST['Name'], "text"),
                       GetSQLValueString($_POST['Age'], "int"),
                       GetSQLValueString($_POST['Sex'], "text"),
                       GetSQLValueString($_POST['Date'], "date"),
                       GetSQLValueString(isset($_POST['HouseTrained']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString(isset($_POST['ExpCat']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString(isset($_POST['ExpDog']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString(isset($_POST['ExpCar']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString(isset($_POST['ExpChild']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString(isset($_POST['ExpAlone']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString(isset($_POST['ExpOnlead']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString(isset($_POST['checkbox']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString(isset($_POST['Valid']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString($_POST['AnimalID'], "text"));

  mysql_select_db($database_woodside, $woodside);
  $Result1 = mysql_query($updateSQL, $woodside) or die(mysql_error());

  $updateGoTo = "stage2.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

mysql_select_db($database_woodside, $woodside);
$query_animal = "SELECT * FROM animal ORDER BY AnimalID DESC";
$animal = mysql_query($query_animal, $woodside) or die(mysql_error());
$row_animal = mysql_fetch_assoc($animal);
$totalRows_animal = mysql_num_rows($animal);

mysql_select_db($database_woodside, $woodside);
$query_Recordset1 = "SELECT * FROM breed WHERE AnimalTypeID='{$_POST[`AnimalTypeID`]}'";
$Recordset1 = mysql_query($query_Recordset1, $woodside) 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=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">AnimalID:</td>
      <td><?php echo $row_animal['AnimalID']; ?></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">AnimalTypeID:</td>
      <td><input type="text" name="AnimalTypeID" value="<?php echo $row_animal['AnimalTypeID']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">BreedID:</td>
      <td><label></label>
<input type="text" name="BreedID" value="<?php echo $row_animal['BreedID']; ?>" size="32">
<label>
<select name="select">
</select>
</label></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Name:</td>
      <td><input type="text" name="Name" value="<?php echo $row_animal['Name']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Age:</td>
      <td><input type="text" name="Age" value="<?php echo $row_animal['Age']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Sex:</td>
      <td><input type="text" name="Sex" value="<?php echo $row_animal['Sex']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Date:</td>
      <td><input type="text" name="Date" value="<?php echo $row_animal['Date']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">HouseTrained:</td>
      <td><input type="text" name="HouseTrained" value="<?php echo $row_animal['HouseTrained']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">ExpCat:</td>
      <td><input type="text" name="ExpCat" value="<?php echo $row_animal['ExpCat']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">ExpDog:</td>
      <td><input type="text" name="ExpDog" value="<?php echo $row_animal['ExpDog']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">ExpCar:</td>
      <td><input type="text" name="ExpCar" value="<?php echo $row_animal['ExpCar']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">ExpChild:</td>
      <td><input type="text" name="ExpChild" value="<?php echo $row_animal['ExpChild']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">ExpAlone:</td>
      <td><input type="text" name="ExpAlone" value="<?php echo $row_animal['ExpAlone']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">ExpOnlead:</td>
      <td><input type="text" name="ExpOnlead" value="<?php echo $row_animal['ExpOnlead']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">ExpOffLead:</td>
      <td><input type="text" name="ExpOffLead" value="<?php echo $row_animal['ExpOffLead']; ?>" size="32">
        <label>
        <input <?php if (!(strcmp($row_animal['ExpOffLead'],1))) {echo "checked=\"checked\"";} ?> type="checkbox" name="checkbox" value="checkbox" />
      </label></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Valid:</td>
      <td><input type="text" name="Valid" value="<?php echo $row_animal['Valid']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"> </td>
      <td><input type="submit" value="Update record"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_update" value="form1">
  <input type="hidden" name="AnimalID" value="<?php echo $row_animal['AnimalID']; ?>">
  
  <input name="hiddenField" type="hidden" value="<?php echo $row_animal['AnimalTypeID']; ?>" />
</form>

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

mysql_free_result($Recordset1);
?>

 

 

If anybody can help me that be great

 

Thank You

Link to comment
Share on other sites

Hi there,

 

thanks for reply, i find i get an error message when testing this code it says:

 

mysql error 1064: you ahve an error in your myqsl syntax; check manual that correponds to mysql server version for right syntax to use near 'AnimalTypeID']."" at line 1.

 

 

What else could it be???

 

Thank you

Link to comment
Share on other sites

HHHmm i think perhaps dreamweaver is quite limiting!

 

Because in the recordset of that query it says the error:

 

mysql error 1064: you ahve an error in your myqsl syntax; check manual that correponds to mysql server version for right syntax to use near 'AnimalTypeID']."" at line 1.

 

 

EVEN though my acutal code is :

 

$query_Recordset1 = "SELECT * FROM breed WHERE AnimalTypeID='".$_POST['AnimalTypeID']." ' "; With the single quote in between!

 

The only other time i have managed to get this query to NOT produce an error is if using:

 

$query_Recordset1 = "SELECT * FROM breed WHERE AnimalTypeID='{$_POST[`AnimalTypeID`]}'";

 

 

But even then it doesnt retrieve any records at all??!

 

I know there are multiple records of breeds per animal type....i have 2 for a cat and 2 for a dog...

 

 

HHm what should i do???

 

Thanks

 

Link to comment
Share on other sites

LOL

 

I must be going mad!!!

 

I try changing to:

 AnimalTypeID=".$_POST['AnimalTypeID']."  ";

 

only to get syntax error opening page:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

The AnimalTypeID that is being $_POSTED and searched for in BREED table is a varchar

 

 

If i remove quotes i get syntax error if i have quotes in no records are found....

 

Ohh man...whats going on??

 

Thank you :)

 

Link to comment
Share on other sites

Hi there thanks for reply, i appreciate what your saying

 

Then could there be something wrong with how i have added the AnimalTypeID into the hidden field and $_Post it???

 

Because it is not bring up any records and i know for fact that these records exist!??

 

Thanks

Link to comment
Share on other sites

Hi thanks for reply,

 

 

So currently using:

 

 $query_Recordset1 = "SELECT * FROM breed WHERE AnimalTypeID='{$_POST[`AnimalTypeID`]}'";

 

This code doesnt produce error message but doesnt produce any ressults either...

 

 

When i change to your suggestion :

 

 

 

 
die($updateSQL);
$Result1 = mysql_query($updateSQL, $woodside) or die(mysql_error());

 

...and click up date i get:

UPDATE animal SET AnimalTypeID='DOG', BreedID=NULL, Name='Guss', Age=1, Sex='M', `Date`='2008-01-29', HouseTrained=1, ExpCat=1, ExpDog=1, ExpCar=1, ExpChild=1, ExpAlone=1, ExpOnlead=1, ExpOffLead=0, Valid=1 WHERE AnimalID='21'

 

But i wanted the list/menu of Breed to display breed  relative to AnimalType and the user to make appropriate selection and then to be updated into database....

 

Thanks

 

Link to comment
Share on other sites

Hi there,

 

Thats great thanks for reply, i see hat you mean there with text and int! I think that was donea while ago after i decided to go with int for animal id number.

 

I have made the appropriate changes....And so far everything all works including first initial insert and the update on the 2nd page...The only thing that is not working is the Breed list/menu on page 2 that is suppose to use bring up all Breeds that have the same animal type as the current record...

 

No records are found at all...??? Even though there are records of breed avaliable...

 

 

 

realise im probably a pain in the ass right now and do appreciate and am thankful of any help you all offer

 

 

Thank you

 

Tom - the almost broken man lol

Link to comment
Share on other sites

Hi there,

 

UPDATE animal SET AnimalTypeID=22, BreedID=NULL, Name='Lizzie', Age=14, Sex='M', `Date`='2008-01-29', HouseTrained=1, ExpCat=1, ExpDog=1, ExpCar=1, ExpChild=1, ExpAlone=1, ExpOnlead=1, ExpOffLead=0, Valid=1 WHERE AnimalID=22

 

I noticed that AnimalTypeID is being updated as an int (as Animal ID) and not txt as it i..i have made changes and all works as it should, accept for breed list/menu that doesnt seem to bring up any of the records...????

 

 

Any ideas b gr8 :)

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.