Jump to content

Insert double record instead of only one


Parkie02
Go to solution Solved by waynewex,

Recommended Posts

Can somebody tell me why there is two complaint records added to the complaint table when I am entering information of somebody and how I can fix it. Here is the new code

<?php
session_start();

if(!$_SESSION['email'])
{
	header("location:login.php");
}
?>

<html>
	<head>
		<title> 
			Complaint
		</title>
	</head>
	
<body>
<form method='post' action='complaint.php'>
	<table width='800' border='10' align='left'>
	<tr>
		<td align='center' colspan='5'><h1>Complaint Form</h1></td>
	</tr>

	<tr>
		<td>Comany's Name:</td>
		<td><input type='text' name='comp_name' /></td>
	</tr>
	
	<tr>
		<td>Registration Number:</td>
		<td><input type='text' name='reg_nr' /></td>
	</tr>
	
	<tr>
		<td>Email:</td>
		<td><input type='text' name='comp_email' /></td>
	</tr>
	
	<tr>
		<td>Contact Person:</td>
		<td><input type='text' name='comp_contact_person' /></td>
	</tr>
	
	<tr>
		<td>Contact Number:</td>
		<td><input type='text' name='comp_contactnr' /></td>
	</tr>
	
	<tr>
		<td>Details of complain:</td>
		<td><input type='text' name='complain_details' /></td>
	</tr>
	
	<tr>
		<td>Order Number:</td>
		<td><input type='text' name='order_nr' /></td>
	</tr>
	
	<tr>
		<td>Order Description:</td>
		<td><input type='text' name='order_desc' /></td>
	</tr>
	
	<tr>
		<td>Order Date:</td>
		<td><input type='date' name='order_date' /></td>
	</tr>
	
	<tr>
		<td>Delivery Date:</td>
		<td><input type='date' name='delivery_date' /></td>
	</tr>
	
	<tr>
		<td>Invoice Date:</td>
		<td><input type='date' name='invoice_date' /></td>
	</tr>
	
	<tr>
		<td>Delivery Number:</td>
		<td><input type='text' name='delivery_nr' /></td>
	</tr>
	
	<tr>
		<td>Order Amount:</td>
		<td><input type='number' step='0.01' name='order_amount' /></td>
	</tr>
	
	<tr>
		<td>Amount Already Paid:</td>
		<td><input type='number' step='0.01' name='amount_paid' /></td>
	</tr>
	
	<tr>
		<td>Amount Outstanding:</td>
		<td><input type='number' step='0.01' name='amount_outstanding' /></td>
	</tr>
	
	<tr>
		<td colspan='5' align='center'><input type='submit' name='add_debtor' value='Submit' /></td>
	</tr>
	
	
	</table>
</form>
</body>
</html>
<?php
mysql_connect("localhost","root","mj2015");
mysql_select_db("whodidntpay");

$log = $_SESSION['email'];

	if(isset($_POST['add_debtor']))
	{
		$comp_name = $_POST['comp_name'];
		$comp_regnr = $_POST['reg_nr'];
		$comp_email = $_POST['comp_email'];
		$comp_contact_person = $_POST['comp_contact_person'];
		$comp_contact_nr = $_POST['comp_contactnr'];
		$complain_det = $_POST['complain_details'];
		$ordernr = $_POST['order_nr'];
		$orderdesc = $_POST['order_desc'];
		$orderdate = $_POST['order_date'];
		$deliverydate = $_POST['delivery_date'];
		$invoicedate = $_POST['invoice_date'];
		$deliverynr = $_POST['delivery_nr'];
		$orderamount = $_POST['order_amount'];
		$paidamount = $_POST['amount_paid'];
		$outstandamount = $_POST['amount_outstanding'];
		
		if($comp_name=='')
		{
			echo "<script>alert('Please Enter Company Name')</script>";
			exit();
		}
		
		if($comp_regnr=='')
		{
			echo "<script>alert('Please Enter Company Registration Number')</script>";
			exit();
		}
		
		if($comp_email=='')
		{
			echo "<script>alert('Please Enter Company Email')</script>";
			exit();
		}
		
		if($comp_contact_person=='')
		{
			echo "<script>alert('Please Enter Contact Person')</script>";
			exit();
		}
		
		if($comp_contact_nr=='')
		{
			echo "<script>alert('Please Enter Contact Number')</script>";
			exit();
		}
		
		if($complain_det=='')
		{
			echo "<script>alert('Please Enter Complain Details')</script>";
			exit();
		}
		
		if($ordernr=='')
		{
			echo "<script>alert('Please Enter Order Number')</script>";
			exit();
		}
		
		if($orderdesc=='')
		{
			echo "<script>alert('Please Enter Order Description')</script>";
			exit();
		}
		
		if($orderdate=='')
		{
			echo "<script>alert('Please Enter Order Date')</script>";
			exit();
		}
		
		if($deliverydate=='')
		{
			echo "<script>alert('Please Enter Delivery Date')</script>";
			exit();
		}
		
		if($invoicedate=='')
		{
			echo "<script>alert('Please Enter Invoice Date')</script>";
			exit();
		}
		
		if($deliverynr=='')
		{
			echo "<script>alert('Please Enter Delivery Number')</script>";
			exit();
		}
		
		if($orderamount=='')
		{
			echo "<script>alert('Please Enter Order Amount')</script>";
			exit();
		}
		
		if($paidamount=='')
		{
			echo "<script>alert('Please Enter Amount Paid')</script>";
			exit();
		}
		
		if($outstandamount=='')
		{
			echo "<script>alert('Please Enter The Amount Outstanding')</script>";
			exit();
		}
		
		$num_debt = "select * from debtor where d_name= '$comp_name' AND registration_nr= '$comp_regnr'";
		$run = mysql_query($num_debt);
		$num_comp = mysql_num_rows($run);
		
		if ($num_comp ==0)
		$query = "insert into debtor(d_name,registration_nr,email,contact_person,contact_number,companies_not_paid,amount_not_paid) 
		values ('$comp_name','$comp_regnr','$comp_email','$comp_contact_person','$comp_contact_nr','1','$outstandamount')";
		if ($num_comp ==1)
		$query = "update debtor set companies_not_paid = companies_not_paid + 1, amount_not_paid = amount_not_paid + '$outstandamount' where d_name='$comp_name'";
		
		if (mysql_query($query))
		{
			echo "<script>alert('Company Added Successful')</script>";
		}
		
		$complain_id = "select complainant_id from complainant where email = '$log'";
		$run2=mysql_query($complain_id);
		
		$row = mysql_fetch_assoc($run2); 	
		$query2 = "insert into complaint(complain,d_name,complainant_id) values ('$complain_det','$comp_name','{$row['complainant_id']}')";

		mysql_query($query2);

		$the_auto_id_that_was_just_created = mysql_insert_id();

		if (mysql_query($query2))
		{
			echo "<script>alert('Complaint Added Successful')</script>";
		}
	
		$query3 = "insert into complaint_details(order_nr,order_description,order_date,delivery_date,invoice_date,delivery_nr,complaint_nr,order_amount,amount_paid,amount_outstanding)
		values ('$ordernr','$orderdesc','$orderdate','$deliverydate','$invoicedate','$deliverynr','$the_auto_id_that_was_just_created','$orderamount','$paidamount','$outstandamount')";
		
		if (mysql_query($query3))
		{
			echo "<script>alert('Complaint Details Added Successful')</script>";
		}
	}	

?>

The insert command of complaint is at query2 almost at the end of the code.

Thank you

Link to comment
Share on other sites

  • Solution

The code:

$query2 = "insert into complaint(complain,d_name,complainant_id) values ('$complain_det','$comp_name','{$row['complainant_id']}')";

mysql_query($query2);

$the_auto_id_that_was_just_created = mysql_insert_id();

if (mysql_query($query2))
{
    echo "<script>alert('Complaint Added Successful')</script>";
}

The issue:

  1. You construct your INSERT query.
  2. You execute it.
  3. You retrieve the primary key of the last inserted row.

Then, you do this:

if (mysql_query($query2))
{
    echo "<script>alert('Complaint Added Successful')</script>";
}

Which basically runs the query again because it calls the mysql_query function.

 

Change your code to something like:

$query2 = "insert into complaint(complain,d_name,complainant_id) values ('$complain_det','$comp_name','{$row['complainant_id']}')";

$inserted = mysql_query($query2);

$the_auto_id_that_was_just_created = null;
if ($inserted)
{
    $the_auto_id_that_was_just_created = mysql_insert_id();
    echo "<script>alert('Complaint Added Successful')</script>";
}
Edited by waynewex
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.