What seems to be a relatively minor coding issue is giving me a massive headache. I currently have a set up that grabs from my SQL database information about services (it's ID, name, a file location for company logo, and a click location) and throws it into an array.

	$qry = mysql_query(sprintf("SELECT * FROM manageme WHERE element IN ('%s')", $element));
				$count = 0;
				while (($services = mysql_fetch_array($qry)) != null && $count < 52) { $count++;?>

				<li>
					<a href="">                                                           
						<img src="<?php print $services['fileLocation']; ?>"; 
						title="<?php print $services['title']; ?>"; 
						name="<?php print $services['clickLocation']; ?>";
						alt="wat" 
						class="btnshow" /> 
					</a>

Right now, everything displays as I would like it to, that's not my issue. I'm trying to add a button that displays under each company logo that would on click insert the corresponding ID from each value that the array displays into a different MySQL table, coupled with the logged in user's $_COOKIE['user_id'].

<?PHP
							$sql= sprintf("INSERT INTO user_service (user_id, service_id)
										   VALUES (%s, %s);", mysql_real_escape_string($_COOKIE['user_id']),
								                              mysql_real_escape_string($gotcha));
							$result=mysql_query($sql);

						if($result) {
						echo "YESSSS!!!!!!!!";
						}else{
						echo "NOOOOO!!!!!";
						}
					?>
				<form id="button" method="POST">
				<fieldset>
				<input type="submit" value="Add" name="button" />
				</fieldset>
				</form>

When I attempt this, clicking the button currently INSERT's a record for every ID recognized from the table referenced in the array. So, if there are 20 ID's in the table, upon attempting to insert (108, 1) , it will also insert (108, 2), (108, 3)...and so on. Basically clicking on one button inserts an ID value from every row recognized in the array when I'm only trying to INSERT the current value of the array. Any help would be greatly appreciated, I'm about to pull my hair out!

    The code you've shown in the second code snippet doesn't make any sense; you're already executing the INSERT query at the same time you display the HTML for the button. In other words, it doesn't matter if you click the button or not - you've already executed the INSERT query (one for each button you're displaying, based on your description).

      I'm very sorry I think this is what I was going for programmatically, still the same issues though...

      				<form id="button" method="POST">
      					<fieldset>
      					<input type="submit" value="Add" name="button" />
      					</fieldset>
      					</form>
      				    <?php
      
      				if(isset($_POST['button'])) {
      				$sql= sprintf("INSERT INTO user_service (user_id, service_id)
      				               VALUES (%s, %s);", mysql_real_escape_string($_COOKIE['user_id']),
      							                      mysql_real_escape_string($service['imageID'])
      							                      );
      				$result=mysql_query($sql);
      
      				if($result) {
      				echo "YESSSS!!!!!!!!";
      				}else{
      				echo "NOOOOO!!!!!";
      				}
      				}
      				?>

      So basically if the button is clicked, the POST on the form reloads the page runs the SQL statement and currently states yes or no based on whether or not the INSERT worked. On click, like you said it inserts every ID from the corresponding array into the new table. Does this help?

        Well, is the last piece of code above found inside the loop in the first piece of code you posted? And, how is PHP supposed to distinguish between the identical buttons?

          johanafm;11014745 wrote:

          Well, is the last piece of code above found inside the loop in the first piece of code you posted? And, how is PHP supposed to distinguish between the identical buttons?

          Well, I've tried both keeping it in the loop and moving it just outside. Keeping it inside the loop like so...

          <?php
          				$qry = mysql_query(sprintf("SELECT * FROM manageme WHERE element IN ('%s')", $element));
          				$count = 0;
          				while (($services = mysql_fetch_array($qry)) != null && $count < 52) { $count++;?>
          
          				<?php if(isset($services['imageID'])) {
          				$gotcha = $services['imageID'];
          				}
          
          				if(isset($_POST['button'])) {
          				$sql= sprintf("INSERT INTO user_service (user_id, service_id)
          				               VALUES (%s, %s);", mysql_real_escape_string($_COOKIE['user_id']),
          							                      mysql_real_escape_string($gotcha)
          							                      );
          				$result=mysql_query($sql);
          
          				if($result) {
          				echo "YESSSS!!!!!!!!";
          				}else{
          				echo "NOOOOO!!!!!";
          				}
          				}
          
          				?>
          				<li>
          					<a href="">                                                           
          						<img src="<?php print $services['fileLocation']; ?>"; 
          						title="<?php print $services['title']; ?>"; 
          						name="<?php print $services['clickLocation']; ?>";
          						alt="wat" 
          						class="btnshow" /> 
          				</a>
          								<form id="button" method="POST">
          				<fieldset>
          				<input type="submit" value="Add" name="button" />
          				</fieldset>
          				</form>
          				</li>
          
          		<?php } ?>

          INSERT's all "id" values in the array into the database. If I move it just outside the loop, it INSERT's only one value (which is what I'm shooting for) but it simply grabs the ID of the last row in the loop and moves that value to the database. So I guess my initial question should have been worded much like yours...how can I distinguish each button and thus each INSERT statement attached to the button so that they are specified to the ID value of the current row being grabbed from the array.

            Make the buttons actually post a value that identifies the row they belong to (e.g. either using a hidden form field or by adding the ID in the query string of the form's action URL, which is currently missing from your <form> tag by the way...).

              bradgrafelman;11014753 wrote:

              Make the buttons actually post a value that identifies the row they belong to (e.g. either using a hidden form field or by adding the ID in the query string of the form's action URL, which is currently missing from your <form> tag by the way...).

              I passed in the value through a hidden form field and it works!!Thank you so much for your help you have no idea the joy I am feeling now that this issue is worked out, I am truly grateful!!

                Write a Reply...