Jump to content

Insert command not working right


Parkie02

Recommended Posts

Can somebody maybe tell me why my insert command does not work correct. An user register and log in. Then the user can send a complaint. In my one table I want to show which user send the complaint via the complainant id.

 

Here is the part of code not working:

$log = $_SESSION['email'];

$complain_id = "select complainant_id from complainant where email = '$log'";
		$run2=mysql_query($complain_id);
		
echo $run2;
				
$query2 = "insert into complaint(complain,d_name,complainant_id) values ('$complain_det','$comp_name','$run2')";
		
if (mysql_query($query2))
{
 echo "<script>alert('Complaint Added Successful')</script>";
}
	

Here is the whole complaint.php part:

<?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($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);
		
		echo $run2;
				
		$query2 = "insert into complaint(complain,d_name,complainant_id) values ('$complain_det','$comp_name','$run2')";
		
		if (mysql_query($query2))
		{
			echo "<script>alert('Complaint Added Successful')</script>";
		}
	
	
	}	

?>

There is also a complainant table which consist of a complainant_id, email and password.

Link to comment
Share on other sites

mysql_query() does not return any data from the query. it only returns a result resource. To get the data from the query you need to use a function like mysql_fetch_assoc

 

So change

echo $run2;
				
$query2 = "insert into complaint(complain,d_name,complainant_id) values ('$complain_det','$comp_name','$run2')";

to

$row = mysql_fetch_assoc($run2); // get the data from the query		
$query2 = "insert into complaint(complain,d_name,complainant_id) values ('$complain_det','$comp_name','{$row['complainant_id']}')";
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.