Jump to content

SQL query


taz321

Recommended Posts

Hi

 

I have two tables

 

Employee

Form

 

The foreign key of the form is EmployeeID.

 

Now heres the problem.

I want to display the EmployeeID in my form but as you know if i did then the ID number of the Employee would display instead of the Full Name (which is what i want).

 

 

e.g

 

The employeeID of Tom is 2.

 

In my form i want to display the name Tom by using an SQL query which recognises the ID and returns the name of that ID.

 

I have had a start but its wrong.

 

<?php

 

require "connect.php";

$query1 = "select * from form WHERE empName = surname";

$result1 = mysql_query($query1, $connection)

or die ("MySQL Error: ".mysql_error());

 

?>

 

empName (displays the primary key of the employee table)

surname  (i want it to display the surname of the employee by recognising the empName)

 

Any help would be appreciated.

 

Thanks

 

 

Link to comment
Share on other sites

Basically you'd have to use a simple join.

 

$query = "SELECT e.surName FROM `Employee` e, `Form` f WHERE e.empName = f.empName";
$run = mysql_query($query);

 

This is assuming that surName is the actual name of the employee, and empName is the id of the employee.

Link to comment
Share on other sites

Try this I am not sure what the key field is in the employee table, but in this example I will assume it is the same as in the form table, employeeID

 

$query1 = "SELECT * FROM form JOIN employee ON form.employeeID=employee.employeeID WHERE empName = 'surname'";

 

if you give me the structure of both tables I could help out a little more.

 

 

Ray

Link to comment
Share on other sites

This is the 'employee' Table

 

employeeID       

title           

firstname           

surname             

username               

password               

active

 

 

This is the 'form' Table

 

  formID   

  issuetitle             

  datesubmitted       

  timesubmitted             

  systemaffected             

  prioritylevel               

  issuedetails               

  supportcomments             

  clientFname             

  clientSurname               

  teamname         

  empName         

  clientID             

 

In the form table, the empName displays the employeeID from the employee table, hope this helps.

 

Thanks           

 

Link to comment
Share on other sites

Sorry I was editing the last post when time ran out so disregard.

 

I replaced the surname with xxx, because empname is a reference to the employee id which is an interger. the empname should be, in the case above, 2. The xxx should be some value passed from the web page.

 

<?php
$query1 = "SELECT * FROM form JOIN employee ON form.empname = employee.employeeID WHERE empName = 'xxxx'";
$res = mysql_query($query1) or die(mysql_error());
while($rows = mysql_fetch_assoc($res)){
echo $rows['firstname']." ".$rows['lastname']."<br>";
}
?>

 

That should give you your first and last name based off toms employeeid of 2

 

Ray

Link to comment
Share on other sites

So this is the statement im now using

 

<?php

 

 

$query1 = "SELECT * FROM form JOIN employee ON form.empname = employee.employeeID WHERE empName = 'surname'";

$result = mysql_query($query1) or die(mysql_error());

while($rows = mysql_fetch_assoc($result)){;

echo $rows['surname'];

 

}

?>

 

 

 

And then i echo it

 

<?php echo $row['surname'];?>

 

but it doesnt seem to work, any reason why ?

Link to comment
Share on other sites

This is my code now

 

<?php

 

 

$query1 = "SELECT * FROM form JOIN employee ON form.empname = employee.employeeID WHERE empName = 'surname'";

$result1 = mysql_query($query1) or die(mysql_error());

while($rows = mysql_fetch_array($result1)){

echo $rows['surname'];

 

}

?>

 

and i display it by doing this (but still doesnt work)

 

<?php echo $rows['surname'];?>

 

But nothing displays

 

Is there anything im doing wrong.

 

 

Link to comment
Share on other sites

You need to check it actually finds a result.

 

<?php

  $sql = "SELECT * FROM form JOIN employee ON form.empname = employee.employeeID WHERE empName = 'surname'";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) { 
      while ($row = mysql_fetch_array($result)) {
        echo $row['surname'];
      }
    } else {
      echo "No results found";
    }
  } else {
    die("Query failed<br />$sql<br />" . mysql_error());
  }

?>

 

What do you get?

Link to comment
Share on other sites

That still doesnt display the employee surname. :-(

 

This is the code you gave me

 

<?php

 

  $sql = "SELECT * FROM form JOIN employee ON form.empname = employee.employeeID WHERE empName = 'surname'";

  if ($result1 = mysql_query($sql)) {

    if (mysql_num_rows($result1)) {

      while ($row = mysql_fetch_array($result1)) {

        echo $row['surname'];

      }

    } else {

      echo "No results found";

    }

  } else {

    die("Query failed<br />$sql<br />" . mysql_error());

  }

 

?>

 

 

And i echo it using this

 

<?php echo $rows['surname'];?>

 

 

Link to comment
Share on other sites

If it helps, this is my whole code for displaying this data.

 

 

i have indicated below by '''''''  '''''''''''  where i echoed the code where i want the surname to display.

 

 

 

<?php

session_start();

if (isset($_SESSION['username']) == false){

header("Location:ClientLogin.php");

exit();

}

?>

 

<?php

 

require "connect.php";

$query = "select * from form";

$result = mysql_query($query, $connection)

or die ("MySQL Error: ".mysql_error());

 

?>

 

<?php

 

  $sql = "SELECT * FROM form JOIN employee ON form.empname = employee.employeeID WHERE empName = 'surname'";

  if ($result1 = mysql_query($sql)) {

    if (mysql_num_rows($result1)) {

      while ($row = mysql_fetch_array($result1)) {

        echo $row['surname'];

      }

    } else {

      echo "No results found";

    }

  } else {

    die("Query failed<br />$sql<br />" . mysql_error());

  }

 

?>

 

 

<head>

<title>Jupiter Development Support</title>

<link href="Images/mystyle.css" rel="stylesheet" type="text/css">

 

 

<style type="text/css">

<!--

#Layer2 {position:absolute;

width:200px;

height:52px;

z-index:2;

left: 16px;

top: 143px;

}

#Layer1 {position:absolute;

width:200px;

height:80px;

z-index:1;

left: 254px;

top: 84px;

}

.style3 {font-family: "Copperplate Gothic Bold"; font-size: x-large; }

#Layer5 { position:absolute;

width:441px;

height:36px;

z-index:3;

left: 390px;

top: 207px;

}

.style4 {font-size: x-small}

.style6 {font-family: "Copperplate Gothic Bold"}

#Layer3 {

position:absolute;

width:156px;

height:43px;

z-index:4;

left: 24px;

top: 470px;

}

#Layer6 { position:absolute;

width:126px;

height:118px;

z-index:4;

left: 1079px;

top: 186px;

}

.style7 {font-size: xx-small; }

.style8 {font-size: x-small; font-weight: bold; }

-->

</style>

</head>

 

<body>

 

<div id="header">

<h1 align = "center" class="style1"> </h1>

<p align = "center" class="style1"> </p>

<div id="Layer2"><img src="Images/logo.jpg" width="173" height="43"></div>

<p align = "center" class="style1"> </p>

<p align = "center" class="style1"> </p>

 

<div id="Layer5"><span class="style3">Analyst Enquiry Screen </span></div>

<div id="Layer6"><img src="Images/Symbol.jpg" width="118" height="111"></div>

<p> </p>

<table width="393" border="0">

  <tr>

    <td width="17"> </td>

    <td width="495"><span class="style4"><?php echo $_SESSION['firstname']?> <?php echo $_SESSION['surname']?> <span class="style6"> LOGGED IN </span></span></td>

  </tr>

</table>

<p> </p>

<table width="1582" border="0">

  <tr>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td width="128"> </td>

    <td width="125"> </td>

    <td width="136"> </td>

    <td width="118"> </td>

    <td width="131"> </td>

    <td width="49"> </td>

    <td width="89" class="style8"> </td>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td class="style8"> </td>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <td width="64"> </td>

    <td width="64"> </td>

    <td width="20"> </td>

    <td width="68"> </td>

    <td><div align="center" class="style8">Enquiry Number </div></td>

    <td><div align="center" class="style8">Issue Title </div></td>

    <td><div align="center" class="style8">Date Submitted </div></td>

    <td><div align="center" class="style8">System Affected </div></td>

    <td><div align="center" class="style8">Priority Level </div></td>

    <td><div align="center" class="style8">

      <div align="center">Team </div>

    </div></td>

    <td class="style8"><div align="center">Employee</div></td>

    <td width="16"> </td>

    <td width="520"><span class="style4"></span></td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td width="16"> </td>

    <td> </td>

  </tr>

 

  <tr>

    <td width="64"> </td>

    <td width="64"> </td>

    <td width="20"> </td>

    <td width="68"><?php

while($row= mysql_fetch_array($result)){?></td>

    <td height="35"><div align="center" class="style7"><?php echo $row['formID'];?></div></td>

    <td height="35"><div align="center" class="style7"><?php echo $row['issuetitle'];?></div></td>

    <td height="35"><div align="center" class="style7"><?php echo $row['datesubmitted'];?></div></td>

    <td height="35"><div align="center" class="style7"><?php echo $row['systemaffected'];?></div></td>

    <td height="35"><div align="center"><span class="style7"><?php echo $row['prioritylevel'];?></span></div></td>

    <td height="35"><div align="center"><span class="style7"><?php echo $row['teamname'];?></span></div></td>

    '''''''''''''<td height="35"><div align="center" class="style7"><?php echo $row['surname'];?></div></td>'''''''''''''''''''''''

    <td width="16"> </td>

    <td><a href="analystformedit.php?formID=<?php echo $row['formID'];?>">

      <div align="left"><img src="Images/smallopenbutton.jpg" width="66" height="22"></div></td>

  <tr>

    <td width="64"> </td>

    <td width="64"> </td>

    <td width="20"> </td>

    <td width="68"><?php } ?></td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td width="16"> </td>

    <td width="520"> </td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td><div align="center"></div></td>

    <td> </td>

    <td width="16"> </td>

    <td> </td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

    <td> </td>

    <td><div id="Layer3"><a href="Adminlogout.php"><img src="Images/logOffButton.jpg" width="156" height="44" border="0"></a></div></td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td> </td>

    <td width="16"> </td>

    <td> </td>

  </tr>

</table>

<p> </p>

<div id="Layer2"><img src="Images/logo.jpg" width="173" height="43"></div>

<div id="Layer1"><img src="Images/JDS.jpg" width="704" height="79"></div>

</body>

Link to comment
Share on other sites

<?php
while($row= mysql_fetch_array($result)){?></td>
    <td height="35"><div align="center" class="style7"><?php echo $row['formID'];?></div></td>
    <td height="35"><div align="center" class="style7"><?php echo $row['issuetitle'];?></div></td>
    <td height="35"><div align="center" class="style7"><?php echo $row['datesubmitted'];?></div></td>
    <td height="35"><div align="center" class="style7"><?php echo $row['systemaffected'];?></div></td>
    <td height="35"><div align="center"><span class="style7"><?php echo $row['prioritylevel'];?></span></div></td>
    <td height="35"><div align="center"><span class="style7"><?php echo $row['teamname'];?></span></div></td>
    <td height="35"><div align="center" class="style7"><?php echo $row['surname'];?></div></td>

 

 

Above is the main code which displays everything i want.

And iv realised now wot u mean.

 

Your second while loop tries to loop through a result called $result, this is never defined anywhere

 

But if i change it to $result1 then even though it wouldnt display for all the other fields, should it not still display surname ?

 

 

 

 

 

 

Link to comment
Share on other sites

no, becasue youve already ran that $result1 through a while loop at the top of your script. The would be at the end of its internal pointer.

 

You basically want to replace the last piece of code you posted with...

 

<?php

  $sql = "SELECT * FROM form JOIN employee ON form.empname = employee.employeeID WHERE empName = 'surname'";
  if ($result1 = mysql_query($sql)) {
    if (mysql_num_rows($result1)) { 
      while ($row = mysql_fetch_array($result1)) {?>
        <td height="35"><div align="center" class="style7"><?php echo $row['formID'];?></div></td>
    <td height="35"><div align="center" class="style7"><?php echo $row['issuetitle'];?></div></td>
    <td height="35"><div align="center" class="style7"><?php echo $row['datesubmitted'];?></div></td>
    <td height="35"><div align="center" class="style7"><?php echo $row['systemaffected'];?></div></td>
    <td height="35"><div align="center"><span class="style7"><?php echo $row['prioritylevel'];?></span></div></td>
    <td height="35"><div align="center"><span class="style7"><?php echo $row['teamname'];?></span></div></td>
    <td height="35"><div align="center" class="style7"><?php echo $row['surname'];?></div></td>
      <?php}
    } else {
      echo "No results found";
    }
  } else {
    die("Query failed
$sql
" . mysql_error());
  }

?>

 

And remove the other part from the top.

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.