Jump to content

One submit button for multiple pieces of data


gitn32

Recommended Posts

Here is my code:

$result = mysql_query ("SELECT * FROM $tbl_name WHERE username='$user'");
	
        
        while($row = mysql_fetch_array($result))
            {   
                
                
                
				$username = $row['username'];
				$email = $row['password'];
				$age = $row['age'];
				$id = $row['id'];
             
		
				
			
                
                
                echo "
				
				
                <font size='3' face='arial'>
                
				
				
                <table width='250' border='0' cellspacing='1' cellpadding='0'>
				<tr>
					<td><b>$id, $username,$password,$email,$age</b></td>
				</tr>
				<tr>
					<td><form action=delete.php method=POST><input type=submit name=submit value=Remove></form></td>
				</tr>

				
	
				</table>
					
			
                    
                    
					
				
                </font>
                ";
                
     
	if(isset($_POST['submit'])) {
	
	$got_error = 0;
	
	
			mysql_query(" DELETE $id FROM $tbl_name);

		 
        }
		
		else {
			$got_error = 1;
			echo "Error.";
			
		}
		}

I have multiple pieces of data. Each piece of data has its own submit buttton when displayed. If there is 2 pieces of data and I hit the top button, the bottom data is deleted. I want to make it so that if I hit the top button the top button is deleted. Please help.

Link to comment
Share on other sites

Each time the form appears you need to include a hidden field containing that records id.

 

Something like this:

 

<input type="hidden" name="id" value="<?php echo $id ?>">

 

Then change your query to:

 

DELETE FROM $tbl_name WHERE id = $_POST['id']; 

 

Of course, format it in a way where you won't get errors. My formatting isn't like yours but you get the idea.

Link to comment
Share on other sites

Ok the first problem is solved, how about this:

	$result = mysql_query ("SELECT * FROM invoices WHERE username='$user'");
	
        
        while($row = mysql_fetch_array($result))
            {   
                
                
                
				$username = $row['username'];
				$bill = $row['invoice'];
				$ammount = $row['ammount'];
				$id = $row['id'];
             
		
				
			
                
                
                echo "
				
				
                <font size='3' face='arial'>
                
				
				
                <table width='250' border='0' cellspacing='1' cellpadding='0'>
				<tr>
					<td><b>$id, $bill: $$ammount</b></td>
				</tr>
				<tr>
					<td><form action=bills.php method=POST<input type=submit name=submit value=Remove</form></td>
				</tr>

				
	
				</table>
					
			
                    
                    
					
				
                </font>
                ";
                
     
	if(isset($_POST['submit'])) {
	
	
	$got_error = 0;
	
	
	
	if ($accbal > $ammount){  
        
		
		 
		$naccbal = $accbal - $ammount; 
			require ("userconnect.php");
			mysql_query("UPDATE users SET accbal = '$naccbal' WHERE username = '$user'");

		
        }
		
		else {
			$got_error = 1;
			echo "Not enough money in account.";
			
		}
		}

//$accbal IS defined before code starts
//And there is an extra $ before $ammount so it displays a dollar sign, and the ammount.

I'm also having the same problem with this, and this is different because your updating not deleting. This is a virtual funding system.

Edited by gitn32
Link to comment
Share on other sites

Why would you bother escaping an integer? Just make it an int.

How do I know that id is an integer? Probably is but I don't think that you can tell from the code. If it is an integer and is never going to change to a string then you are right making it an int will be faster.

Link to comment
Share on other sites

Ok the first problem is solved, how about this:

	$result = mysql_query ("SELECT * FROM invoices WHERE username='$user'");
	
        
        while($row = mysql_fetch_array($result))
            {   
                
                
                
				$username = $row['username'];
				$bill = $row['invoice'];
				$ammount = $row['ammount'];
				$id = $row['id'];
             
		
				
			
                
                
                echo "
				
				
                <font size='3' face='arial'>
                
				
				
                <table width='250' border='0' cellspacing='1' cellpadding='0'>
				<tr>
					<td><b>$id, $bill: $$ammount</b></td>
				</tr>
				<tr>
					<td><form action=bills.php method=POST<input type=submit name=submit value=Remove</form></td>
				</tr>

				
	
				</table>
					
			
                    
                    
					
				
                </font>
                ";
                
     
	if(isset($_POST['submit'])) {
	
	
	$got_error = 0;
	
	
	
	if ($accbal > $ammount){  
        
		
		 
		$naccbal = $accbal - $ammount; 
			require ("userconnect.php");
			mysql_query("UPDATE users SET accbal = '$naccbal' WHERE username = '$user'");

		
        }
		
		else {
			$got_error = 1;
			echo "Not enough money in account.";
			
		}
		}

//$accbal IS defined before code starts
//And there is an extra $ before $ammount so it displays a dollar sign, and the ammount.

I'm also having the same problem with this, and this is different because your updating not deleting. This is a virtual funding system.

Can someone please reply.

Link to comment
Share on other sites

I believed that I answered the question: told you what the errors were and quoted the two lines of code that had the errors in them. I then went on to suggest that if you were coding in a good IDE all of the errors you posted would have been far less likely to occur and easier to spot. Please read my posts again.

Link to comment
Share on other sites

Yes that was helpful. But not to this question:

 

I have multiple pieces of data. Each piece of data has its own submit button when displayed. If there is 2 pieces of data and I hit the top button, the bottom data is the accbalance. I want to make it so that if I hit the top button the top button the newaccbal is the amount of the top.

Link to comment
Share on other sites

Ok the first problem is solved, how about this:

	$result = mysql_query ("SELECT * FROM invoices WHERE username='$user'");
	
        
        while($row = mysql_fetch_array($result))
            {   
                
                
                
				$username = $row['username'];
				$bill = $row['invoice'];
				$ammount = $row['ammount'];
				$id = $row['id'];
             
		
				
			
                
                
                echo "
				
				
                <font size='3' face='arial'>
                
				
				
                <table width='250' border='0' cellspacing='1' cellpadding='0'>
				<tr>
					<td><b>$id, $bill: $$ammount</b></td>
				</tr>
				<tr>
					<td><form action=bills.php method=POST<input type=submit name=submit value=Remove</form></td>
				</tr>

				
	
				</table>
					
			
                    
                    
					
				
                </font>
                ";
                
     
	if(isset($_POST['submit'])) {
	
	
	$got_error = 0;
	
	
	
	if ($accbal > $ammount){  
        
		
		 
		$naccbal = $accbal - $ammount; 
			require ("userconnect.php");
			mysql_query("UPDATE users SET accbal = '$naccbal' WHERE username = '$user'");

		
        }
		
		else {
			$got_error = 1;
			echo "Not enough money in account.";
			
		}
		}

//$accbal IS defined before code starts
//And there is an extra $ before $ammount so it displays a dollar sign, and the ammount.

I'm also having the same problem with this, and this is different because your updating not deleting. This is a virtual funding system.

There it is.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.