troubleshooting Question

PHP - Updating multiple rows and tables in one form. What am I doing wrong?

Avatar of tonybobdesign
tonybobdesignFlag for United States of America asked on
Web DevelopmentScripting Languages
20 Comments1 Solution448 ViewsLast Modified:
I am creating a very insecure form at the moment so I will fix my post values and secure them when I have it working..  I have a friend that is a Realtor and needs a cpanel to create and modify and delete her property listings.  Well besides the typical record information such as title, summary, and description...  the administrator also has the ability to upload multiple images that associate to that specific listing they were creating.  When you grab the id of the record to modify it, I would like to change a single value by the name of `default`to 1 or 0 that is attached to each image associating with the listing that is currently being edited.  This value represents the default image in the gallery.  When viewing the record from the front end, You have a default image on the left and then the rest of the images in another part of the layout.  

My issue is that before I added this functionality to update multiple rows, I was able to click on submit get my success message and then title, date, price, summary, and description were all updated.  Now when I added in the loop and query to update the multiple records, I can't get to the success message and the multiple records update like they should, but then the rest of the fields don't get updated.  Please help and I greatly appreciate it.  
<script type="text/javascript">
<!--
$(document).ready(function()	{
	$('#markItUp').markItUp(mySettings);
});
-->
</script>
 
<?php
require('../connect.php');
$request_id = $_REQUEST['id'];
 
if($_POST['cmd'] == "save") {
	
	$title = $_POST['title'];
	$date = $_POST['date'];
	$price = $_POST['price'];
	$summary = $_POST['summary'];
	$description = $_POST['description'];
	$default_str = $_POST['default'];
	$id_str = $_POST['id'];
	
	for ($i = 0; $i < count($default_str); $i++) {
		$default = $_POST['default'][$i];
		$id = $_POST['id'][$i];
		$query = "UPDATE uploads SET `default`='$default' WHERE id='$id'";
		mysql_query($query) or die(mysql_error());
	}
 
	mysql_query("UPDATE listings SET title='$title' WHERE id='$request_id'") or die(mysql_error());
	mysql_query("UPDATE listings SET date='$date' WHERE id='$request_id'") or die(mysql_error());
	mysql_query("UPDATE listings SET price='$price' WHERE id='$request_id'") or die(mysql_error());
	mysql_query("UPDATE listings SET summary='$summary' WHERE id='$request_id'") or die(mysql_error());
	mysql_query("UPDATE listings SET description='$description' WHERE id='$request_id'") or die(mysql_error()); 
	
	echo '
	<script type="text/javascript" language="javascript">
		$(function() {
			feedBackMessage("formMessage", "positive", "Listing has been updated");
		});
	</script>';
 
}
 
$result = mysql_query("SELECT * FROM listings WHERE id='$request_id'");
while($listing = mysql_fetch_array($result)) {
 
?>
 
<div id="form" class="component red">
	<h2>Edit a Listing</h2>
	<div class="component lightBorder">	
		<p id="formMessage"></p>
		<form id="editListing" name="editListing" action="<?=$_SERVER['php_self']?>" onsubmit="return validateForm(this);" method="post">
			<input type="hidden" name="cmd" value="save" style="border: 0px; background: transparent;"/>
			<p style="float: right;"><span style="color: red;">*</span> Indicates required fields</p>
			<p style="clear: right;">
				<label><span style="color: red;">*</span> Title:</label>
				<span style="display: block; margin-left: 110px;">
					<input type="text" name="title" value="<?=$listing['title']?>" maxlength="250" class="required" style="width: 97%;" />
				</span>
			</p>
			<p>
				<label><span style="color: red;">*</span> Date:</label>
				<span style="display: block; margin-left: 110px;">
					<input type="text" name="date" value="<?=$listing['date']?>" maxlength="10" class="required" style="width: 97%;" />
				</span>
			</p>
			<p>
				<label><span style="color: red;">*</span> Price:</label>
				<span style="display: block; margin-left: 110px;">
					<input type="text" name="price" value="<?=$listing['price']?>" maxlength="250" class="required" style="width: 97%;" />
				</span>
			</p>
			<p>
				<label>Summary:</label>
				<span style="display: block; margin-left: 110px;">
					<textarea name="summary" id="summary" rows="5" cols="40" style="width: 97%;"><?=$listing['summary']?></textarea>
				</span>
			</p>
			<p>
				<label>Description:</label>
				<textarea id="markItUp" class="markItUpEditor" name="description" rows="5" cols="40"><?=$listing['description']?></textarea>
			</p>
			<p>
				<label>Images:</label>
				<span class="listingImages" style="display: block; margin-left: 110px;">
					
					<?
					$request_lid = $listing['lid'];
					$result1 = mysql_query("SELECT * FROM uploads WHERE listing='$request_lid'") or die (mysql_error());
					
					$i=0;
					while ($images = mysql_fetch_array($result1)) {
						$i++;
						echo "<span class='item'>";
					  	echo "<a href='{$images['upload_dir']}{$images['filename']}' title='{$images['filename']}' class='thickbox' rel='listingImages' style='text-decoration: none;'><img src='{$images['upload_dir']}{$images['filename']}' width='100' border='0' alt='{$images['filename']}' style='margin-bottom: 5px;' /></a><br />";
						echo "<input type='hidden' name='id[]' value='".$images['id']."'/><br />";
					  	echo "<input type='text' size='1' id='default' name='default[]' value='{$images['default']}'/>";
						echo "</span>";
						
					}
					
					?>
 
					<br style="clear: left;" />
				</span>
			</p>
 
			<input class="submit" type="submit" name="submit" value="Submit" />
		</form>
		
	</div>	
</div>
 
<? } mysql_close($connection); ?>
ASKER CERTIFIED SOLUTION
seippg

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 20 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 20 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros