Jump to content

Recommended Posts

Below is a piece of code that I am using to query two tables at the same time. The problem I am having is that the company name doesnt appear in the result. What am I doing wrong?

 

<?php

require "config.php";

$selcomp = $_REQUEST['selcomp'];
$newprojname = $_REQUEST['newprojname'];
$newprojman= $_REQUEST['newprojman'];
$newprojdead= $_REQUEST['newprojdead'];

//check to see if a project already exists with this project name for this company
$q1 = mysql_query("SELECT * FROM project WHERE project_name = '$newprojname' AND company_name = '$selcomp'");
$count = mysql_num_rows($q1);
if ($count == 0) {
$q2 = mysql_query("INSERT INTO project (project_name, proj_manager, company_name, deadline) VALUES ('$newprojname', '$newprojman', '$selcomp', '$newprojdead')") or die (mysql_error()); 

$q3 = mysql_query("SELECT proj_id, project_name, project.company_name, acc_no FROM company, project WHERE company.company_name = project.company_name AND project_name = '$newprojname' AND project.company_name = '$selcomp'");
$r = mysql_fetch_assoc($q3);
}
$return = $count . "%%" . $r['proj_id'] . "%%" . $r['project_name'] . "%%" . $r['project.company_name'] . "%%" . $r['acc_no'];
echo $return;

?>

The JOIN syntax you are using seems off, try this:

 

$q1 = mysql_query("SELECT * FROM project WHERE project_name = '$newprojname' AND company_name = '$selcomp'");
$count = mysql_num_rows($q1);
if ($count == 0) {
   $q2 = mysql_query("INSERT INTO project (project_name, proj_manager, company_name, deadline) VALUES ('$newprojname', '$newprojman', '$selcomp', '$newprojdead')") or die (mysql_error()); 
   
   $q3 = mysql_query("SELECT p.proj_id, p.project_name, c.company_name, c.acc_no FROM company c, project p WHERE c.company_name = p.company_name AND p.project_name = '$newprojname' AND c.company_name = '$selcomp'");
   $r = mysql_fetch_assoc($q3);
}
$return = $count . "%%" . $r['proj_id'] . "%%" . $r['project_name'] . "%%" . $r['company_name'] . "%%" . $r['acc_no'];
echo $return;

<?php

require "config.php";

$selcomp = $_REQUEST['selcomp'];
$newprojname = $_REQUEST['newprojname'];
$newprojman= $_REQUEST['newprojman'];
$newprojdead= $_REQUEST['newprojdead'];

//check to see if a project already exists with this project name for this company
$q1 = mysql_query("SELECT * FROM project WHERE project_name = '$newprojname' AND company_name = '$selcomp'");
$count = mysql_num_rows($q1);
if ($count == 0) {
   $q2 = mysql_query("INSERT INTO project (project_name, proj_manager, company_name, deadline) VALUES ('$newprojname', '$newprojman', '$selcomp', '$newprojdead')") or die (mysql_error()); 
   
   $q3 = mysql_query("SELECT proj_id, project_name, project.company_name AS pcompname, acc_no FROM company, project WHERE company.company_name = project.company_name AND project_name = '$newprojname' AND project.company_name = '$selcomp'");
   $r = mysql_fetch_assoc($q3);
}
$return = $count . "%%" . $r['proj_id'] . "%%" . $r['project_name'] . "%%" . $r['pcompname'] . "%%" . $r['acc_no'];
echo $return;

?>

 

Give that a try

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.