trouble with my edit form code

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

User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: trouble with my edit form code

Post by edawson003 »

cool. no worries. Here's what displayed: NULL bool(true)
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: trouble with my edit form code

Post by jackpf »

That shouldn't be the case. It should tell you its a resource or something.

I can't really test it either...since I don't have the same environment. I'd suggest you just look through the logic of your code. Make sure all the queries are running and stuff.

I'll have another look when I get home...
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: trouble with my edit form code

Post by edawson003 »

Will do. I think messed up something with mysql query that I attempted to set up as a function, "function updateFood()".
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: trouble with my edit form code

Post by edawson003 »

Okay, I combed through my code as I found a few tidbits that were off and fixed em. Now when I run the form, I get bool(true) int(0). That seems a lil better than what I got before NULL bool(true), eventhough I'm not quite sure what it means. However, the edits made on form still didn't post. :(

What does bool(true) int(0) mean?

Here's how I setup my input boxes to pull in previously provided in (from my mysql table):

Code: Select all

<input type="text" [color=#FF0000]name="vitd"[/color] maxlength="30" value="<?php echo isset([color=#0000FF]$vitd[/color]) ? [color=#0000FF]$vitd[/color] : ''; ?>" />
Could the fact that the input box name (in red) is the same as the variable called out in the echo isset (in blue), possible why my edited data is not posting? Did I set this up right?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: trouble with my edit form code

Post by jackpf »

I think you want to use $_POST['vitd']...
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: trouble with my edit form code

Post by edawson003 »

So, don't rename it:
<input type="text" name="vitd2" maxlength="30" value="<?php echo isset($vitd) ? $vitd : ''; ?>" />
and update my:

Code: Select all

$iron = $_POST['iron'];
   $vitd2 = $_POST['vitd2'];
   $vite = $_POST['vite'];
???
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: trouble with my edit form code

Post by jackpf »

I'm not sure what you mean. If you want to get the contents of the input called "vitd", then use $_POST['vitd'].
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: trouble with my edit form code

Post by edawson003 »

Oh, I was just wondering whether the field name used to define the mysql data pull $variable was some how conflicting with the POST $variable for the same data field because the field name part was the same.

For instance [the mysql pull code];

Code: Select all

<?
            if(isset($_GET['fid'])){  
            $fid=$_GET['fid'];  
//connect  to the database  
$sql="SELECT * FROM FoodLib WHERE foodid=" . $fid;  
//-run  the query against the mysql query function  
$result=mysql_query($sql);  
//-create  while loop and loop through result set  
while($row=mysql_fetch_array($result)){  
   $foodname =$row['foodname'];  
   $foodsubname =$row['foodsubname'];
   $vitd = $row['[color=#FF0000]vitd[/color]'];
   ........etc etc etc
vs. field name used in the post $variable defining code;

Code: Select all

function updateFood()
   {
 /* Add the new account to the database */
   $foodname = mysql_real_escape_string($_POST['foodname']);
   $foodsubname = mysql_real_escape_string($_POST['foodsubname']);
   $vitd = $_POST['[color=#FF0000]vitd[/color]'];
 
Forget about it! It was a half baked theory anyway :). Just trying to figure out why this darn update form won't work.

BTW, does the fact that I'm now getting bool(true) int(0) instead of NULL bool(true) from your die(var_dump($ureturn), mysql_affected_rows()); at all seem promising? What does that mean anyway?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: trouble with my edit form code

Post by jackpf »

Well yeah. The query is returning true...so there are no parse errors. But it's not updating any rows. It's probably something to do with your WHERE clause.

Try echoing out the query, and see if it's what you expect.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: trouble with my edit form code

Post by edawson003 »

Good advice! I'll try that. I tell yah, I'm on verge of just deleting this file and rebuilding it from scratch (carefully). Or maybe I work the syntax out on a smaller form first (way less fields, say 5 or so). This one may have too many variables (lol --forgive the pun), too many opportunities for error, especially for a newbie like myself.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: trouble with my edit form code

Post by edawson003 »

You were right. There was something that was wrong with the where statement of the update query.

Code: Select all

 
[color=#FF0000]code came b4......[/color]$manganese = $_POST['manganese'];
   $chromium = $_POST['chromium'];
   $molybdenum = $_POST['molybdenum'];
   
   $q = "UPDATE FoodLib SET foodname = '$foodname', foodsubname = '$foodsubname', brand = '$brand', description = '$description', foodtype = '$foodtype', upccode = '$upccode', servingsize = '$servingsize', servingunits = '$servingunits', servingsize2 = '$servingsize2', servingunits2 = '$servingunits2', servingsize3 = '$servingsize3', servingunits3 = '$servingunits3', calories = '$calories', calfromfat = '$calfromfat', totalfat = '$totalfat', satfat = '$satfat', transfat = '$transfat', polyunsatfat = '$polyunsatfat', monounsatfat = '$monounsatfat', cholesterol = '$cholesterol', sodium = '$sodium'
   , potassium = '$potassium' , totalcarb = '$totalcarb', dietaryfiber = '$dietaryfiber', sugar = '$sugar', protein = '$protein', vita  = '$vita', vitc  = '$vitc', calcium  = '$calcium', iron = '$iron', vitd = '$vitd', vite = '$vite', vitk = '$vitk', thiamin = '$thiamin', riboflavin = '$riboflavin', niacin = '$niacin', vitb6 = '$vitb6', folicacid = '$folicacid', vitb12 = '$vitb12', biotin = '$biotin', pantothenicacid = '$pantothenicacid', phosphorus = '$phosphorus', iodine = '$iodine', magnesium = '$magnesium', zinc = '$zinc', selenium = '$selenium', copper = '$copper', manganese = '$manganese', chromium  = '$chromium', molybdenum = '$molybdenum' WHERE foodid = [color=#FF0000]'$fid'[/color]";
   $ureturn = mysql_query($q) or die(mysql_error());   
   }  
The $fid variable wasn't coming across for the second query (the update query), eventhough it was set for the 1st query (the select query), so I just set the $variable again. like so:

Code: Select all

  
$manganese = $_POST['manganese'];
   $chromium = $_POST['chromium'];
   $molybdenum = $_POST['molybdenum'];
  [color=#FF0000] $fid=$_GET['fid'];[/color]
 
$q = "UPDATE FoodLib SET foodname = '$foodname', foodsubname = '$foodsubname', brand = '$brand', description = '$description', foodtype = '$foodtype', upccode = '$upccode', servingsize = '$servingsize', servingunits = '$servingunits', servingsize2 = '$servingsize2', servingunits2 = '$servingunits2', servingsize3 = '$servingsize3', servingunits3 = '$servingunits3', calories = '$calories', calfromfat = '$calfromfat', totalfat = '$totalfat', satfat = '$satfat', transfat = '$transfat', polyunsatfat = '$polyunsatfat', monounsatfat = '$monounsatfat', cholesterol = '$cholesterol', sodium = '$sodium'
   , potassium = '$potassium' , totalcarb = '$totalcarb', dietaryfiber = '$dietaryfiber', sugar = '$sugar', protein = '$protein', vita  = '$vita', vitc  = '$vitc', calcium  = '$calcium', iron = '$iron', vitd = '$vitd', vite = '$vite', vitk = '$vitk', thiamin = '$thiamin', riboflavin = '$riboflavin', niacin = '$niacin', vitb6 = '$vitb6', folicacid = '$folicacid', vitb12 = '$vitb12', biotin = '$biotin', pantothenicacid = '$pantothenicacid', phosphorus = '$phosphorus', iodine = '$iodine', magnesium = '$magnesium', zinc = '$zinc', selenium = '$selenium', copper = '$copper', manganese = '$manganese', chromium  = '$chromium', molybdenum = '$molybdenum' WHERE foodid = [color=#FF0000]'$fid'[/color]";
   $ureturn = mysql_query($q) or die(mysql_error());   
   }
 
Any thoughts on why the variable didn't stay set after the 1st query (the select query)? Is it because it is based on $_GET?

Eitherway, the record actually updates Thanx!!! :lol:
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: trouble with my edit form code

Post by jackpf »

Cool, finally :D

I can't really say...are you using register globals at all?
What's your whole code? Just interested myself tbh :)

Anyway, good stuff :)
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: trouble with my edit form code

Post by edawson003 »

Here's the whol ething accept for all the field validation stuff. The validation stuff goes on and on and there's a character limit with these posts.

Code: Select all

<?
            if(isset($_GET['fid'])){  
            $fid=$_GET['fid'];  
//connect  to the database  
$sql="SELECT * FROM FoodLib WHERE foodid=" . $fid;  
//-run  the query against the mysql query function  
$result=mysql_query($sql);  
//-create  while loop and loop through result set  
while($row=mysql_fetch_array($result)){  
   $foodname =$row['foodname'];  
   $foodsubname =$row['foodsubname'];
   $brand = $row['brand'];
   $description = $row['description'];
   $foodtype = $row['foodtype'];
   $upccode = $row['upccode'];
   $servingsize = $row['servingsize'];
   $servingunits = $row['servingunits'];
   $servingsize2 = $row['servingsize2'];
   $servingunits2 = $row['servingunits2'];
   $servingsize3 = $row['servingsize3'];
   $servingunits3 = $row['servingunits3'];
   $calories = $row['calories'];
   $calfromfat = $row['calfromfat'];
   $totalfat = $row['totalfat'];
   $satfat = $row['satfat'];
   $transfat = $row['transfat'];
   $polyunsatfat = $row['polyunsatfat'];
   $monounsatfat = $row['monounsatfat'];
   $cholesterol = $row['cholesterol'];
   $sodium = $row['sodium'];
   $potassium = $row['potassium'];
   $totalcarb = $row['totalcarb'];
   $dietaryfiber = $row['dietaryfiber'];
   $sugar = $row['sugar'];
   $protein = $row['protein'];
   $vita = $row['vita'];
   $vitc = $row['vitc'];
   $calcium = $row['calcium'];
   $iron = $row['iron'];
   $vitd = $row['vitd'];
   $vite = $row['vite'];
   $vitk = $row['vitk'];
   $thiamin = $row['thiamin'];
   $riboflavin = $row['riboflavin'];
   $niacin = $row['niacin'];
   $vitb6 = $row['vitb6'];
   $folicacid = $row['folicacid'];
   $vitb12 = $row['vitb12'];
   $biotin = $row['biotin'];
   $pantothenicacid = $row['pantothenicacid'];
   $phosphorus = $row['phosphorus'];
   $iodine = $row['iodine'];
   $magnesium = $row['magnesium'];
   $zinc = $row['zinc'];
   $selenium = $row['selenium'];
   $copper = $row['copper'];
   $manganese = $row['manganese'];
   $chromium = $row['chromium'];
   $molybdenum = $row['molybdenum'];
   $optionname = $row['optionname'];
}  
}  
?> 
<?
include("includes/profilecredentials.php");
?> 
<?php
if (get_magic_quotes_gpc()) {
        $in = array(&$_GET, &$_POST, &$_COOKIE);
        while (list($k,$v) = each($in)) {
                foreach ($v as $key => $val) {
                        if (!is_array($val)) {
                                $in[$k][$key] = stripslashes($val);
                                continue;
                        }
                        $in[] =& $in[$k][$key];
                }
        }
        unset($in);
}
?>
 
<?
if(isset($_POST['subjoin'])){
 
/* A whole lot of validation code was here --- Removed it so I can post it. There's a character count limit :) */  
 
   function updateFood()
   {
 /* Add the new account to the database */
   $foodname = mysql_real_escape_string($_POST['foodname']);
   $foodsubname = mysql_real_escape_string($_POST['foodsubname']);
   $brand = mysql_real_escape_string($_POST['brand']);
   $description = mysql_real_escape_string($_POST['description']);
   $foodtype = $_POST['foodtype'];
   $upccode = $_POST['upccode'];
   $servingsize = trim($_POST['servingsize']);
   $servingunits = trim($_POST['servingunits']);
   $servingsize2 = $_POST['servingsize2'];
   $servingunits2 = $_POST['servingunits2'];
   $servingsize3 = $_POST['servingsize3'];
   $servingunits3 = $_POST['servingunits3'];
   $calories = trim($_POST['calories']);
   $calfromfat = $_POST['calfromfat'];
   $totalfat = $_POST['totalfat'];
   $satfat = $_POST['satfat'];
   $transfat = $_POST['transfat'];
   $polyunsatfat = $_POST['polyunsatfat'];
   $monounsatfat = $_POST['monounsatfat'];
   $cholesterol = $_POST['cholesterol'];
   $sodium = $_POST['sodium'];
   $potassium = $_POST['potassium'];
   $totalcarb = $_POST['totalcarb'];
   $dietaryfiber = $_POST['dietaryfiber'];
   $sugar = $_POST['sugar'];
   $protein = $_POST['protein'];
   $vita = $_POST['vita'];
   $vitc = $_POST['vitc'];
   $calcium = $_POST['calcium'];
   $iron = $_POST['iron'];
   $vitd = $_POST['vitd'];
   $vite = $_POST['vite'];
   $vitk = $_POST['vitk'];
   $thiamin = $_POST['thiamin'];
   $riboflavin = $_POST['riboflavin'];
   $niacin = $_POST['niacin'];
   $vitb6 = $_POST['vitb6'];
   $folicacid = $_POST['folicacid'];
   $vitb12 = $_POST['vitb12'];
   $biotin = $_POST['biotin'];
   $pantothenicacid = $_POST['pantothenicacid'];
   $phosphorus = $_POST['phosphorus'];
   $iodine = $_POST['iodine'];
   $magnesium = $_POST['magnesium'];
   $zinc = $_POST['zinc'];
   $selenium = $_POST['selenium'];
   $copper = $_POST['copper'];
   $manganese = $_POST['manganese'];
   $chromium = $_POST['chromium'];
   $molybdenum = $_POST['molybdenum'];
   $fid=$_GET['fid'];
   
   $q = "UPDATE FoodLib SET foodname = '$foodname', foodsubname = '$foodsubname', brand = '$brand', description = '$description', foodtype = '$foodtype', upccode = '$upccode', servingsize = '$servingsize', servingunits = '$servingunits', servingsize2 = '$servingsize2', servingunits2 = '$servingunits2', servingsize3 = '$servingsize3', servingunits3 = '$servingunits3', calories = '$calories', calfromfat = '$calfromfat', totalfat = '$totalfat', satfat = '$satfat', transfat = '$transfat', polyunsatfat = '$polyunsatfat', monounsatfat = '$monounsatfat', cholesterol = '$cholesterol', sodium = '$sodium'
   , potassium = '$potassium' , totalcarb = '$totalcarb', dietaryfiber = '$dietaryfiber', sugar = '$sugar', protein = '$protein', vita  = '$vita', vitc  = '$vitc', calcium  = '$calcium', iron = '$iron', vitd = '$vitd', vite = '$vite', vitk = '$vitk', thiamin = '$thiamin', riboflavin = '$riboflavin', niacin = '$niacin', vitb6 = '$vitb6', folicacid = '$folicacid', vitb12 = '$vitb12', biotin = '$biotin', pantothenicacid = '$pantothenicacid', phosphorus = '$phosphorus', iodine = '$iodine', magnesium = '$magnesium', zinc = '$zinc', selenium = '$selenium', copper = '$copper', manganese = '$manganese', chromium  = '$chromium', molybdenum = '$molybdenum' WHERE foodid = '$fid'";
   $ureturn = mysql_query($q) or die(mysql_error());    
     header( 'Location: http://www.url.com/membersarea/fooddeta ... fid='.$fid);
   }   
         
   if(empty($reqerrors) && empty($errors) && empty($reqverrors)){
   updateFood();
   }
   else {  
   $anyerror = "\t<div class=pred>FOOD UPDATE NOT YET SUBMITTED!</div>\n";
   }
   
  if(empty($reqerrors)) {  
   }
  else {
  $reqberror = "\t<div class=pred><b>ERROR: </b>You didn't fill in a required field. See below.</div>\n";
  }
 
  if(empty($reqverrors)) {  
  }
  else {
  $reqverror = "\t<div class=pred><b>ERROR: </b>You entered an invalid value in a required field. See below.</div>\n";
  }
   
  if(empty($errors)) {
  }
  else {
  $optverror = "\t<div class=pred><b>ERROR: </b>You entered an invalid value in an optional field. See below.</div>\n";
  }
 
   }
   ?>
 
 
Post Reply