Forum Moderators: coopster

Message Too Old, No Replies

$ GET Variable from one form to another - creates ghost record

Transfer a value from one form to another

         

texmansru47

2:58 pm on Jul 16, 2008 (gmt 0)

10+ Year Member



Im having the most strange action occurring. First of all, I'm a complete newbie to php programming so please bare with me. I have one form where I do a search on one table in a database I have. I then take one variable from that first form, and transfer it to a second form (the second for will be for entering large amounts of data into a second table in some sort of a looping action). However when I do this, it seems to generate a record in my second table (what I do is process a SELECT * FROM mytable WHERE field1 = $variable from first page) then process and INSERT INTO table once I get the $_POST values from the second form... now that I confused everyone here is the code and a hopefully better breakdown:

Code from first form where the variable is collected to the second page: (the $_POST[BatchNum] is from the first form input).


$sql = "SELECT * FROM `batch` WHERE `BatchNum`= '$_POST[BatchNum]'";
$results = mysql_query($sql);
if ($results) { //this will check if the query failed or not
if (mysql_num_rows($results) > 0) { //this will check if results were returned
while($copy = mysql_fetch_array($results)) {
$variable1=$copy['BatchNum'];
$variable2=$copy['1Num'];
$variable3=$copy['Sku'];
$variable4=$copy['Type'];
$variable5=$copy['Model'];
$variable6=$copy['Qty'];
$variable7=$copy['ModelID'];
$variable8=$copy['RecvDate'];
//table layout for results

print ("<tr>");
print ("<td><a href='testrecv.php?variable1=".$variable1."'>".$variable1."</a></td>");
print ("<td>$variable2</td>");
print ("<td>$variable3</td>");
print ("<td>$variable4</td>");
print ("<td>$variable5</td>");
print ("<td>$variable6</td>");
print ("<td>$variable7</td>");
print ("<td>$variable8</td>");
print ("</tr>"); }


I then transfer $variable1 over to my second page via the $_GET['variable1']; command. Here is the code from the second form:

<P>
<form method="POST" name="Option_Recv" action="testrecv.php">

<table width="400" border="0" cellspacing="2" cellpadding="3">
<tr>
<td colspan="2" bgcolor="#000000"><span class="style1">Receiving IMEI</span></td>
</tr>
<tr>
<td bgcolor="#FFCC66">Serial Number: </td>
<td bgcolor="#CCCCCC"><input name="SerNum" type="text" id="SerNum"></td>
</tr>
<tr>
<td width="135" bgcolor="#FFCC66">Prod Number:</td>
<td width="300" bgcolor="#CCCCCC"><input name="ProdNum" type="text" id="ProdNum"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Submit Data" /></td>
</tr>
</table>

</form>
<?PHP

// Connect to mysql database
$con = mysql_connect("localhost","user","#*$!x") or die('Connection: ' . mysql_error());;
mysql_select_db("test", $con) or die('Database: ' . mysql_error());

$BatchNumData=$_GET['variable1'];
$growit= 0;
echo "Your Batch Number is:\n$BatchNumData";

// Insert form values into database
$sql = mysql_fetch_array(mysql_query("SELECT * FROM `batch` WHERE `BatchNum`= '$BatchNumData'")) or die(mysql_error());
// ^^ SELECT THE DATA TO BE COPIED ^^
//Set Variables for Scripting
$Batch=$sql['BatchNum'];
$Qty=$sql[Qty];
$1Num=$sql['1Num'];
$ModelType=$sql['ModelType'];
$Sku=$sql['Sku'];
$Card=$sql['Card'];
?>
<?PHP
@mysql_query("INSERT INTO `recv` (`BatchNum`, `ProdNum`, `SerNum`, `ModelType`, `Sku`, `1Num`,`Card`, `DateRecv`) VALUES ('$Batch', '" . $_POST['ProdNum'] . "', '" . $_POST['SerNum'] . "','$ModelType','" . $Sku . "','$1Num', '$Card', Now())") or die(mysql_error());

mysql_close($con);

?>


The problem is BEFORE I even enter any data in the two fields on the second form I have a record in my second table which shows a "0" for the Prod Num (since it is an integer) and a blank for the SerNum (VarChar). My goal is to transfer this Batch Number (since this is the key to the bulk data entry since each batch will have a particular quantity assoicated to it. I need to have this second form where I can transfer the one value (Batch Number) and run a SELECT Search since I need to pull the QTY value to use as a constant for my loop variable to feed against. At this point I have no ideas what I'm doing wrong. Any ideas?

mrscruff

3:26 pm on Jul 16, 2008 (gmt 0)

10+ Year Member



Hello there,

You are processing the php as soon as the form loads. Would suggest that you do something like:

<?php
if (!isset($_POST['submit']) {
?>
//put for code here
<?php
} else {
//put form processing here
}
?>

texmansru47

3:36 pm on Jul 16, 2008 (gmt 0)

10+ Year Member



Just so I understand it would look something like this:

<?php
if (!isset($_POST['submit']) {
?>
//put for code here
<P>
<form method="POST" name="Option_Recv" action="testrecv.php">

<table width="400" border="0" cellspacing="2" cellpadding="3">
<tr>
<td colspan="2" bgcolor="#000000"><span class="style1">Receiving IMEI</span></td>
</tr>
<tr>
<td bgcolor="#FFCC66">Serial Number: </td>
<td bgcolor="#CCCCCC"><input name="SerNum" type="text" id="SerNum"></td>
</tr>
<tr>
<td width="135" bgcolor="#FFCC66">Prod Number:</td>
<td width="300" bgcolor="#CCCCCC"><input name="ProdNum" type="text" id="ProdNum"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Submit Data" /></td>
</tr>
</table>

</form>

<?php
} else {
//put form processing here
// Connect to mysql database
$con = mysql_connect("localhost","user","#*$!x") or die('Connection: ' . mysql_error());;
mysql_select_db("test", $con) or die('Database: ' . mysql_error());

$BatchNumData=$_GET['variable1'];
$growit= 0;
echo "Your Batch Number is:\n$BatchNumData";

// Insert form values into database
$sql = mysql_fetch_array(mysql_query("SELECT * FROM `batch` WHERE `BatchNum`= '$BatchNumData'")) or die(mysql_error());
// ^^ SELECT THE DATA TO BE COPIED ^^
//Set Variables for Scripting
$Batch=$sql['BatchNum'];
$Qty=$sql[Qty];
$1Num=$sql['1Num'];
$ModelType=$sql['ModelType'];
$Sku=$sql['Sku'];
$Card=$sql['Card'];
?>
<?PHP
@mysql_query("INSERT INTO `recv` (`BatchNum`, `ProdNum`, `SerNum`, `ModelType`, `Sku`, `1Num`,`Card`, `DateRecv`) VALUES ('$Batch', '" . $_POST['ProdNum'] . "', '" . $_POST['SerNum'] . "','$ModelType','" . $Sku . "','$1Num', '$Card', Now())") or die(mysql_error());

}
?>

Would this be along the lines?

texmansru47

6:03 pm on Jul 16, 2008 (gmt 0)

10+ Year Member



My last post provided me a blank screen... when I modified it and put in the following:

if (!isset($_POST['ProdNum'] == 0)) {

$produ = $_POST['ProdNum'];
$sql = "DELETE FROM `snarecv` WHERE `ProdNum` = $produ";
} else {

I STILL get a blank page. No offense to anyone but I have NEVER been able to get the isset funciton to work... EVER! According to the php manual this is for validating variables in a boolean aspect (true or false). The problem I'm having is not validating whether or not someone pushed the SUBMIT button, because it is not happening. I need to delete the freaking ghost record that generated from the page beening called and posted... since I could not get the original code to work. But again, this stupid function is giving me a blank freaking page. This is killing me. I have a deadline I very past due on and I cannot even get to phase II of this page because the this BS. This is the only form I'm having trouble with and I know it is directly associated to the $_GET variable from the first form.

Since I'm getting a blank page my first assumption is this due to poor syntax.... but I cannot see where I'm messing up. Even on the PHP manual the syntax looks like me... kinda of like telling me to F up or something.

If anyone can see where this code is wrong and why I would get a blank page, PLEASE show what I'm doing wrong because I'm sick of staring at code that looks right to me.

texmansru47

7:06 pm on Jul 16, 2008 (gmt 0)

10+ Year Member



I got the page to stop being blank and post correctly, however I still get the "ghost Character" (all the insert data plus a big fat "0" for the ProdNum and a blank for the SerNum). Honestly, I cannot see how this can be completed... I mean several of you have completed this very task I'm sure... lookup data on one form them pull one variable via a hyperlink to a second form for processing additional data... is there no one out there that has come across this very problem of the freaking form running before anyone clicks the SUBMIT button?

And this !isset code is not working very well. How can a True False boolean such as this process multiple tasks? In addition how do I even know this code is running since no one has pressed the freaking SUBMIT button. As soon as the form is spawned (am trying from a javascript to a popup as well as a full php page - both get the same results). I would need it to delete the bad record then run the process I need ran (insert data in a loop until QTY has bene satisfied)... then on the FALSE side I would have to run the exact same code with a check to verify that there is no record with a "0" in the ProdNum field.

In addition, the Delete command I have is not working either. I have:

@mysql_query("DELETE FROM `recv` WHERE `ProdNum` = 0");

I have as well has nothing there since it is not deleting the record.

How come this one action is kicking my butt? I'm sure it is something Im not seeing or catching - but it would be nice to have some feedback to figure out what the heck is going on.

texmansru47

7:26 pm on Jul 16, 2008 (gmt 0)

10+ Year Member



Nevermind.... I found a better way... it may not be the way of a proper developer... but it works.