I messed up. Form not posting/working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thefreebielife
Forum Contributor
Posts: 126
Joined: Thu Apr 26, 2007 2:59 pm

I messed up. Form not posting/working

Post by thefreebielife »

i know this might be a simple thing, but its really confusing. there are no errors in my log file so i dont know.

basically every since i added the "ocost" stuff, everything has been messed up. when i try to edit, the ocost doesnt even post. and now i cant even add any new offers.

Code: Select all

<? //edit gift 
if (isset($_GET['change'])) { 
	$num = $_GET['change']; 
	$data2 = mysql_query("SELECT * FROM `offers` WHERE oId='$num'")
	or die(mysql_error()); 
	$r = mysql_fetch_array($data2);
	$oname = $r['oName']; 
	$ocampid = $r['oCampID']; 
	$od = $r['oDescription'];
	$olink = $r['oLink'];
	$owait = $r['oWait'];
	$oi = $r['oInstructions'];
	$oimage = $r['oImage'];
	$oweight = $r['oWeight'];
	$ocost = $r['ocost'];

}


if (isset($_GET["form"]) && $_GET["form"] == "change") 
{ 
	$error = "none";
	$ooname = $_POST['name']; 
	$ood = $_POST['description']; 
	$oolink = $_POST['link'];
	$oocampid = $_POST['oCampID']; 
	$oowait = $_POST['wait'];
	$ocredit = $r['oCredit'];
	$ooimage = $_POST['image'];
	$num = $_POST['offerid'];
	$ooweight = $_POST['oWeight'];
	$oocost = $r['ocost'];
	$ooi=$_POST['i'];
	print "abcdefg";
	print $_POST['oWeight'];

	//validate

	if (!other_fields($ooname)  or $ooname == "") {  $error = "name";   }
	if (!other_fields($ood)  or $ood == "") {  $error = "description";  }
	if (!other_fields($oocost)  or $ooi == "") {   $error = "cost";}
	if (!other_fields($oowait)  or $oowait == "") { $error = "wait";    }
	if (!other_fields($ooi)  or $ooi == "") {   $error = "instructions";}
	if ($error == "none")
	{ 
		$d = $ood;
		$query="UPDATE offers SET oName='$ooname', oCampID='$oocampid', oDescription='$d', oWait='$oowait', oImage='$ooimage', oLink='$oolink', oInstructions='$ooi',oweight='$ooweight',ocost='$oocost' WHERE oId='$num'";
		mysql_query($query) or die("Could not insert data because ".mysql_error());
		echo '<div align=center><font color=red>Offer Updated</font></div>';
		echo '<meta http-equiv="refresh" content="1;url=aoffers.php">';
	}
}
?>


<? //validate form
//get vars
if (isset($_GET["form"]) && $_GET['form'] == "submit") { 
	$error = "none";
	$oname = $_POST['name']; 
	$ocampid = $_POST['oCampID']; 
	$od = $_POST['description']; 
	$olink = $_POST['link'];
	$owait = $_POST['wait'];
	$oi = $_POST['i'];
	$oimage = $_POST['image'];
	$oweight =$_POST['oWeight'];
	$ocost = $r['ocost'];
	//validate

	if (!other_fields($oname)  or $oname == "") {
		$error = "name";
	}
	if (!other_fields($od)  or $od == "") {
		$error = "description";
	}
	if (!other_fields($owait)  or $owait == "") {
		$error = "wait";
	}
	if (!other_fields($oi)  or $oi == "") {
		$error = "instructions";
	}
	if (!other_fields($ocost)  or $ocost == "") {
		$error = "cost";
	}

	if ($error == "none") { 
		$d = $od;
		$insert = mysql_query("insert into offers values ('', '$ocampid', '$oname',  '$d', '$olink', '$oimage', '$oi', '$owait','$oweight', 
		'$ocost')")
		or die("Could not insert data because ".mysql_error());
		echo '<center><font color="#ff0000"><h4>The Offer Has Been Entered Into The Database</h4>'; 
		echo '<meta http-equiv="refresh" content="1;url=aoffers.php">';
	}
}
?>


<form action="<? if (isset($_GET['change'])) { ?>aoffers.php?form=change <? } else { ?>aoffers.php?form=submit <? } ?>" method="post" name="addoffers" enctype="multipart/form-data">        
<table border="0" align="center" style="border: 1px dashed red; padding: 4px 4px 4px 4px;">
<tr>
<td colspan="2">
<? if ($error == "name") { echo "<center><font color=red> Error in the name field</font></center>"; } 
if ($error == "description") { echo "<font color=red><center> Error in the Description field</center></font>"; }
if ($error == "wait") { echo "<font color=red><center> Error in the Wait Field</center></font>"; }
if ($error == "instructions") { echo "<font color=red><center> Error in the Instructions field</center></font>"; }		
?>
<tr>
<td colspan="2"><div align="center"><strong>Add An Offer</strong></div></td>
</tr>
<tr>
<td width="146">Offer Name:</td>
<td width="278"><input name="name" type="text" id="name" value="<? echo "$oname"; ?>" size="40" /></td>
</tr>
<tr>
<td width="146">Offer Campaign ID:</td>
<td width="278"><input name="oCampID" type="text" id="oCampID" value="<? echo "$ocampid"; ?>" size="40" /></td>
</tr> <tr>
<td width="146">Offer Link: <font color="#FF0000">&nbsp;</font></td>
<td width="278"><input name="link" type="text" id="link" value="<? echo "$olink"; ?>" size="40" /></td>
</tr>
<tr>
<td width="146">Offer Instructions:</td>
<td width="278"><input name="i" type="text" id="i" value="<? echo "$oi"; ?>" size="40" /></td>
</tr>
<tr>
<td width="146">Offer Image URL:</td>
<td width="278"><input name="image" type="text" id="image" value="<? echo "$oimage"; ?>" size="40" /></td>
</tr>
<tr>
<td nowrap="nowrap">Offer Description:</td>
<td><textarea name="description" cols="30" rows="10" id="description"><? echo "$od"; ?></textarea></td>
</tr>
<tr>
<td nowrap="nowrap">Offer Wait Time:</td><td>			
<input name="wait" type="text" id="wait" value="<? echo "$owait"; ?>" size="40" /></td>
</tr>
<tr>
<td nowrap="nowrap">Offer Weight : </td><td>			
<select name = "oWeight">
<option value="1/4" <?if($oweight=='1/4') echo "selected"; ?> >1/4 </option>
<option value="1/3" <?if($oweight=='1/3') echo "selected"; ?> > 1/3 </option>
<option value="1/2" <?if($oweight=='1/2') echo "selected"; ?> > 1/2 </option>
<option value="2/3" <?if($oweight=='2/3') echo "selected"; ?> > 2/3 </option>
<option value="3/4" <?if($oweight=='3/4') echo "selected"; ?> > 3/4 </option>
<option value="1/1" <?if($oweight=='1/1') echo "selected"; ?> > 1/1 </option>

</select>
</tr>
<tr>
<td width="146">Offer Cost:</td>
<td width="278"><input name="cost" type="text" id="cost" value="<? echo "$ocost"; ?>" size="20" /></td>
</tr>







<tr>
<td colspan="2"><div align="center">
<input name="submit" type="submit" value="Submit" />
<input name="offerid" type="hidden" id="offerid" value="<? echo "$num"; ?>">
</div></td>

</tr>
</table>
</form>
<div class="border"></div><br>

<table align="center">
<Tr>

<?
//select the table
$result = mysql_query("select * from offers order by oname");
$i = 1;
//grab all the content
while($r=mysql_fetch_array($result))
{	
	$oid = $r['oId']; 
	$ocampid = $r['oCampID']; 
	$oname = $r['oName']; 
	$od = $r['oDescription']; 
	$olink = $r['oLink'];
	$owait = $r['oWait'];
	$oi = $r['oInstructions'];
	$oimage = $r['oImage'];
	$oweight= $r['oweight'];
	$ocost = $r['ocost'];
	$uId = 1;
	?>
	<Td><Table style="width:220px; margin-left:35px;" height="300"><tr><td><div align="center"><a href="<? echo "$olink"."$uId"; ?>"><img src="<? echo "$oimage"; ?>" border="0"></a></div></td></tr>
	<tr><td><div class="gname"><? echo "$oname"; ?></div></td></tr>
	<tr><td><div class="gname"><? echo "$ocampid"; ?></div></td></tr>
	<tr><td><div class="odescription"><? echo stripslashes($od); ?></div></td></tr>
	<tr><td><div class="odescription"><strong>Instructions: </strong><? echo $oi; ?></div></td></tr>
	<tr><td><div class="gprice" align="left"><font color="#000000">Approval: </font> <? echo "$owait"; ?><br></td></tr>
	<tr><td><div class="gprice" align="left"><font color="#000000">Weight: </font> <? echo "$oweight"; ?><br></td></tr>
	<tr><td><div class="gprice" align="left"><font color="#000000">Cost: </font> <? echo "$ocost"; ?><br></td></tr>

	<tr><td><div align="center"><font color="#000000"><a href="aoffers.php?change=<? echo "$oid"; ?>">Edit Offer</a></font></div></td></tr>
	<tr><td><div align="center"><font color="#000000"><a href="aoffers.php?remove=<? echo "$oid"; ?>">Remove Offer</a></font></div></td></tr>
	
	</table></Td>
	<? 
	if ($i % 3 == 0) { echo "</tr><tr>"; }
	$i++;
}
?>
i would really appreciate help, as this is what makes my business run.. :(
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

That's a lot of code to look through. I an take a couple of guesses. You don't need the enctype="multipart/form-data" because you are not uploaded images. You also might want to put the "form=submit/change" into a hidden variable.

Also, as this form "makes your business run", if non-employees are using the form then you should validate, filter and escape appropriately all data from the user that you put into SQL or display.
(#10850)
thefreebielife
Forum Contributor
Posts: 126
Joined: Thu Apr 26, 2007 2:59 pm

Post by thefreebielife »

it seems to be more of an input error as whatever i put into "cost" doesnt show

users dont see this page, its an admin page
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Looks like you need a debugger, the simplest beeing the famed printf-debugger.
You add code that prints what's happening and when you're done debugging you remove that code again. Crude but in this case hopefully sufficient.

Code: Select all

function MyEchoDebugger($m1, $m2=null) {
  echo '<pre>Debug: ';
  foreach( array($m1,$m2) as $m) {
    if ( is_null($m) ) {
      continue;
    }
    else if ( is_string($m) ) {
      echo $m;
    }
    else {
      var_export($m);
    }
  }
  echo "\n<pre>\n";
}

if (isset($_GET['change'])) {
  MyEchoDebugger('_GET[change] is set');
  $num = $_GET['change'];

  $query = "SELECT * FROM `offers` WHERE oId='$num'";
  MyEchoDebugger('query: ', $query);
  $data2 = mysql_query($query) or die(mysql_error());

  $r = mysql_fetch_array($data2);
  MyEchoDebugger('result: ', $r);

  $oname = $r['oName'];
  $ocampid = $r['oCampID'];
  $od = $r['oDescription'];
  $olink = $r['oLink'];
  $owait = $r['oWait'];
  $oi = $r['oInstructions'];
  $oimage = $r['oImage'];
  $oweight = $r['oWeight'];
  $ocost = $r['ocost'];
} 
else {
  MyEchoDebugger('_GET[change] is not set');
}
continue this until you've found the source of the error.
thefreebielife
Forum Contributor
Posts: 126
Joined: Thu Apr 26, 2007 2:59 pm

Post by thefreebielife »

hm i've never used this function before.

what does this response from it imply:

Code: Select all

Debug: _GET[change] is not set
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

thefreebielife wrote:hm i've never used this function before.
You certainly didn't ...because I just wrote it (it's untested btw).
what does this response from it imply:

Code: Select all

Debug: _GET[change] is not set
And that makes sense? If yes, continue, add more MyEchoDebugger code to see what the script is doing.
Post Reply