We help IT Professionals succeed at work.
Get Started

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

446 Views
Last Modified: 2013-11-19
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); ?>

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 20 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE