Jump to content

Data not being stored, no errors - Don't know why!


Jax2

Recommended Posts

Hi guys. I am going to quote the second part of my script that stores information back into the database. It is set up so that it updates the first table (recipe), deletes all the information from the ingredients table that share the same ID number, and then re-enters all the ingredients.

 

To explain a little better, the code below takes all the data from the form on a previous page and re-displays it. Everything is displayed exactly as it should be, so I KNOW that the variables are all correct as they're supposed to be... they're just not being saved to the database. Here is the code, and sorry if it's sloppy, I've not cleaned it up yet... just trying to get it to work :(

 

<?php
function clean($array) {
    return array_map('mysql_real_escape_string', $array);
}
if (isset($_POST['edit'])) { 
$recipe_name = mysql_real_escape_string($_REQUEST['name']);
$directions = mysql_real_escape_string($_REQUEST['directions']);
$ingred = clean($_REQUEST['ingred']);
$category = $_REQUEST['cat'];
$counts="0";?>

<!-- DISPLAY NEW DATA -->
<table width="630" height="430" cellpadding="2" cellspacing="0" frame=box align="center">
<tr>
	<td colspan="3" align="center"><h3>Saving Recipe</h3></td>
</tr>
<tr>
	<td colspan="3" align="center"><h3>Name: <?php echo $recipe_name;?></h3></td>
</tr>
<tr>
	<td colspan="3" align="center"><h3>Ingredients:</h3></td>
</tr>
<tr>
<?php
for ($i = 0; $i < 16; $i++) {
              if ($ingred[$i] != "") { ?>
		  	
	<td align="center"><?php echo $ingred[$i]?></td>
<?php
$counts++;
		if ($counts=="3") {
			echo "</tr><tr>";
			$counts="0";
		}

	};
};
?>	
</tr>
<tr>
	<td width="25%">
	 
	</td>
	<td width="50%">
<div align="center"><h3>Directions:</h3></div>
<?php echo $directions;?>
	</td>
	<td width="25%">
	 
	</td>
</tr>
<tr>
	<td colspan="3" align="center">
<?php 
$sql="SELECT * FROM ".$prefix."categories WHERE ID = $category";
$result=mysql_query($sql, $db);
	if (!$result) {
		echo "Error on display Category Name: ".mysql_error()."";
		}
	while ($row=mysql_fetch_array($result)) {
?>
Category: <?php echo $row['category_name'];?> <br>
<!-- END DISPLAY NEW DATA -->
<?php
};

$sql = "update ".$prefix."recipes set recipe_name = '$recipe_name', directions = '$directions', category = '$category' where recipeID = '$ID'";
           $result = mysql_query($sql ,$db);
if (!$result) {
	echo "Updating Recipes Error: ".mysql_error()."<br><br>";
	};

$sql = "delete from ".$prefix."ingredients where recipe_id = '$ID'";
           $result = mysql_query($sql ,$db);
	   if (!$result) {
	echo "Deleting from ingredients Error: ".mysql_error()."<br><br>";
	};

for ($i = 0; $i < 16; $i++) {
              if ($ingred[$i] != "") {
                 $sql = "insert into ".$prefix."ingredients (recipe_id, ing) values ('$recipeID', '$ingred[$i]')";
                 $result = mysql_query($sql ,$db);
			 	if (!$result) {
				echo " Adding Ingredients Error: ".mysql_error()."<br><br>";
			};
              }
	   }
printf("<p><font face=arial size=-1>The recipe has been saved.</font></p>");
?>
	</td>
</tr>
<tr>
	<td colspan="3" align="center">
<div align="center"><form method="post">
<input type="button" value="Close Window"
onclick="window.close()" if (window.opener && !window.opener.closed) {
window.opener.location.reload();
} >
</form></div>
	</td>
</tr>
</table>
<?php
}
?>

 

Does anyone see an error there that would prevent it from saving the data? I use a clean function to clean all the ingredients (In case anyone uses a ' or " or such in one of them) and I escape all the rest other than category, which is a single digit (or two max)

 

Doesn't make any sense. The other part of the script I use to save the original recipe (This is to save an edited version of it) works perfectly fine and saves everything, so I'm assuming this has something to do with updating?

 

I hate it when I make stupid mistakes that cost me an hour or two of trying to figure out what I'm doing wrong....

 

I echoed all the values to make sure they were being passed right, all except one: the ID of the recipe. No error, because it just used ID# 0 then, so noting showed up.

 

*duh* moment.

 

 

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.