insert then delete

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

insert then delete

Post by jayson.ph »

Hi all,

i have a two table table: name as a table1 and table2 which is from the record table1 i transferred it to a table2 which is i use insert to transfer and now after transferring record to another or into a table2 i want to delete the record i transferred to a table2 in 1 click. basically it is insert/transfer first before delete.

here the code below, that delete function is not work.

Code: Select all

if(isset($_POST['received_submit']))
		{
			$pingsql = ("INSERT INTO tbl_recieve(id,uid,cat_name,product_name,product_model,rec_on,return_on,sold,balancs,re_qty,dt_nwo,dt_response,commnt,dt_recve) 
			VALUES('$xvalid','$xvaluid','$xvalcat_name','$xvalproduct_name','$xvalproduct_mode','$xvalrec_on','$xvalreturn_on','$xvalsold','$xvalbalancs','$xvalre_qty','$xvaldt_nwo','$xvaldt_response','$xvalcommnt','$xvaldt_recve')");
			$pingresult = mysql_query($pingsql);
			
			if($pingresult)	
				{
				header('Location:processdone.php');
				}
			else
				{
				header('Location:octagon-portal-geterror2979002921113.php');
				}
				
		}
	else
	{
		header('Location:octagon-portal-geterror2979002921183.php');
	}



processdone.php

Code: Select all

					if(isset($_POST['id']))
					{	
						$xvalid = $_GET['id'];
							$pingsql = ("DELETE FROM tbl_adm_rpl WHERE id = '$xvalid'");
								$pingresult = mysql_query($pingsql);
									if($pingresult)
									{
										header('Location:YES.php');
									}
									else
									{
										header('Location:NO.php');
									}
					}
					else
					{
					header('Location:NOOOO.php');
					}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: insert then delete

Post by Christopher »

I would put both the INSERT and DELETE in the same script. The DELETE should be in the if($pingresult) block, not in processdone.php. I would also suggest adding START TRANSACTION and COMMIT/ROLLBACK around the INSERT/DELETE.
(#10850)
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: insert then delete

Post by jayson.ph »

I would also suggest adding START TRANSACTION and COMMIT/ROLLBACK around the INSERT/DELETE.
i have no idea how it to use you mission above, do you have a sample or even link and i trying to join both you mission earlier but it not work/same. here's

Code: Select all

if(isset($_POST['received_submit']))
		{
			$pingsql = ("INSERT INTO tbl_recieve(id,uid,cat_name,product_name,product_model,rec_on,return_on,sold,balancs,re_qty,dt_nwo,dt_response,commnt,dt_recve) 
			VALUES('$xvalid','$xvaluid','$xvalcat_name','$xvalproduct_name','$xvalproduct_mode','$xvalrec_on','$xvalreturn_on','$xvalsold','$xvalbalancs','$xvalre_qty','$xvaldt_nwo','$xvaldt_response','$xvalcommnt','$xvaldt_recve')");
			$pingresult = mysql_query($pingsql);
			
			if($pingresult)	
				{
				
				
				
				
					
							$pingsql = ("DELETE tbl_adm_rpl.id FROM tbl_adm_rpl WHERE tbl_adm_rpl.id = tbl_adm_rpl.id");
								$pingresult = mysql_query($pingsql);
									
								if($pingresult)
								{
								echo 'success';
								}
								else
								{
								echo 'unsuccessful';
								}	
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: insert then delete

Post by Christopher »

Code: Select all

if(isset($_POST['received_submit']))
		{
			$sql = ('BEGIN TRANSACTION');
			$result = mysql_query($sql);

			$pingsql = ("INSERT INTO tbl_recieve(...) VALUES(...)");
			$pingresult = mysql_query($pingsql);
			
			if($pingresult)	
			{
				$pingsql = ("DELETE tbl_adm_rpl.id FROM tbl_adm_rpl WHERE tbl_adm_rpl.id = tbl_adm_rpl.id");
				$pingresult = mysql_query($pingsql);
							
				if($pingresult)
				{
					$sql = ('COMMIT');
					$result = mysql_query($sql);

					echo 'success';
				}
				else
				{
					$sql = ('ROLLBACK');
					$result = mysql_query($sql);

					echo 'unsuccessful';
				}
(#10850)
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: insert then delete

Post by jayson.ph »

Thank

Code: Select all

Christopher 
but there is something wrong, i can't get in to success
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: insert then delete

Post by jayson.ph »

i wander in tbl_adm_rpl i think server cant find it.
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: insert then delete[SOLVE]

Post by jayson.ph »

Thank you, [text]Christopher [/text]
Solve:

Code: Select all

	mysql_query("BEGIN");
	$pingsql = ("INSERT INTO tbl_recieve(id,uid,cat_name,product_name,product_model,rec_on,return_on,sold,balancs,re_qty,dt_nwo,dt_response,commnt,dt_recve) 
				VALUES('$_POST[id]',
					   '$_POST[uid]',
					   '$_POST[cat_name]',
					   '$_POST[product_name]',
					   '$_POST[product_model]',
					   '$_POST[rec_on]',
					   '$_POST[return_on]',
					   '$_POST[sold]',
					   '$_POST[balancs]',
					   '$_POST[re_qty]',
					   '$_POST[dt_nwo]',
					   '$_POST[dt_response]',
					   '$_POST[commnt]',
					   '$_POST[tx_date]')");
				$pingresult = mysql_query($pingsql);
				
			if($pingresult)
				{		
						
						$pingsqla = ("DELETE FROM tbl_adm_rpl WHERE id = '$_POST[id]'");
								$pingresulta = mysql_query($pingsqla);
									if($pingresulta)
										{
											mysql_query("COMMIT");
											echo "Save Done.";					
										}
									else
										{
											mysql_query("ROLLBACK");
											echo "no";
										}
				}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: insert then delete[SOLVE]

Post by Christopher »

It is important for security that you escape every $_POST value before you put it into the SQL statement.

FYI - The mysql extension is outdated. It is recommend that you use PDO with prepared statements.
(#10850)
Post Reply