Jump to content

Recommended Posts

why my code can't execute? when i execute, it output all the staff_no, name instead of specific staff_no only

 

<?php
//$id=$_GET["id"];
$staff_no = $_POST['staff_no'];

$host = 'localhost';
$user = 'root';
$password = '';
$dbase = 'claim';

$dblink = mysql_connect($host,$user,$password);
mysql_select_db($dbase,$dblink);

$sql="SELECT report1.staff_no, user.name FROM report1,user WHERE report1.staff_no = '".$staff_no."'";
$result = mysql_query($sql);

echo "<table border='1'>
<tr>

<th>Staff No</th>
<th>Name</th>

</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";

echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";

echo "</tr>";
}
echo "</table>";

?>


Link to comment
https://forums.phpfreaks.com/topic/104041-solved-help-me-with-these/
Share on other sites

You are using an array here... you need to specify what part of the array with a variable.

while($row = mysql_fetch_array($result))
{
echo "<tr>";

echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";

echo "</tr>";
}
echo "</table>";

 

should be:

 

while($row = mysql_fetch_array($result))
{
     $report1_staffno = $row['report1.staffno'];
     $username = $row['user.name'];
     echo "<tr>";
     echo "<td>$report1_staffno</td>";
     echo "<td>$username</td>";
     echo "</tr>";
}
echo "</table>";

 

 

ok, i test and this code got error

 

SELECT report1.staff_no, user.name FROM report1,user WHERE report1.staff_no = '$staff_no'

 

when i replace sk200023 into $staff_no

all the name in the database will came out and they auto change their staff_no into sk200023. why?

As sasa said, you haven't set the join relationship in the query

$sql="SELECT report1.staff_no, user.name 
       FROM report1 
           JOIN user ON report1.staff_no = user.staff_no
       WHERE report1.staff_no = '".$staff_no."'";

<?php
//$id=$_GET["id"];
$staff_no = $_POST['staff_no'];

$host = 'localhost';
$user = 'root';
$password = '';
$dbase = 'claim';

$dblink = mysql_connect($host,$user,$password);
mysql_select_db($dbase,$dblink);

$sql="SELECT report1.staff_no, user.name 
       FROM report1 
           JOIN user ON report1.staff_no = user.staff_no
       WHERE report1.staff_no = '.$staff_no.'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>

<th>Staff No</th>
<th>Name</th>

</tr>";

while($row = mysql_fetch_array($result))
{
     $report1_staffno = $row['report1.staff_no'];
     $username = $row['user.name'];
     echo "<tr>";
     echo "<td>$report1_staffno</td>";
     echo "<td>$username</td>";
     echo "</tr>";
}
echo "</table>";
?>

 

this is the user input code

 

<html>
<head>
<script src="selectuser.js"></script>
</head>
<body><form id="form1" name="form1" method="post" action="checking.php">
  <p><strong>KEV Tool Store - Loan Transaction Page</strong></p>
  <table width="497" border="0">
    <tr>
      <td width="284">Choose</td>
      <td width="203"><select name="staff_no" onChange="showUser(this.value)">
        	  <?php
 $host     = 'localhost';
$user     = 'root';
$password = '';
$dbase    = 'claim';

$dblink = mysql_connect($host, $user, $password);
mysql_select_db($dbase, $dblink);

$query = "SELECT staff_no
FROM user
ORDER BY staff_no
";
      		
		$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$staffno = $row['staff_no'];
echo "<option value=\"$staffno\">$staffno</option>";
     }
?>
</select></td>
    </tr>
    <tr>
      <td colspan="2"><p>
<div id="txtHint"><b>info will be listed here.</b></div>
</p> </td>
    </tr>
  </table>
  <p> </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form></body>
</html>

 

this whole code just can't get me the name and staff no when user input their staff_no . why? i been trying for 3 days....

Then it could be your data.

 

It will only return rows where there are records on both tables which match on staff_no.

 

You can see which have matches and which dont by running this query. (if there is a match it will show a name)

 

$sql="SELECT report1.staff_no, user.name 
       FROM report1 
           LEFT JOIN user ON report1.staff_no = user.staff_no";

now i redo the html file again

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="checking2.php">
  <label>Name 
  <input name="staff_no" type="text" id="staff_no" />
  </label>
  <p>
    <label>
    <input type="submit" name="Submit" value="Submit" />
    </label>
  </p>
</form>
</body>
</html>

 

 

php file

 

<?php
// echo " New Entry <a href=add.html>click here</a><br></br>";
$host = "localhost";
$user = "root";
$password = "";
$dbase = "claim";

$dblink = mysql_connect($host,$user,$password);
mysql_select_db($dbase,$dblink);
$staff_no = $_POST['staff_no'];

echo "<table border=3 align=center width=90%>";
echo "<tr><td width=12% align=center><span class=style5>ID</td></span>";
echo "<td width=12% align=center><span class=style5>Month</td></span>";
echo "<td width=12% align=center><span class=style5>Start Date </td></span>";
echo "<td width=12% align=center><span class=style5>End Date</td></span>";
echo "<td align=center><span class=style5>Name</td></span>";
echo "<td align=center><span class=style5>Staff No</td></span>";
echo "<td align=center><span class=style5>Details</td></span>";
echo "<td align=center><span class=style5>Amount</td></span>";
echo "<td align=center><span class=style5>GL code</td></span>";
  echo "<td width=12% align=center><span class=style5>Bank </td></span>";
echo "<td align=center ><span class=style5>Account No. </td></span>";

echo "<td align=center ><span class=style5>Total </td></span>";
echo "<td align=center colspan=3><span class=style5>Action </td></span></tr>";


$query = "SELECT report1.id, month, start_date, end_date, user.name, report1.staff_no,details,amount,gl_code,user.bank,user.acc_no FROM user,report1 WHERE report1.staff_no = '$staff_no'";
$result = mysql_query($query, $dblink);
while($myrow = mysql_fetch_row($result))
{
echo "<tr><td><span class=style2>" . $myrow[0] . "</span</td>";
    echo "<td><span class=style2>" . $myrow[1] . "</span</td>";
    echo "<td><span class=style2>" . $myrow[2] . "</span</td>";
    echo "<td><span class=style2>" . $myrow[3] . "</td></span>";
    echo "<td><span class=style2>" . $myrow[4] . "</span></td>";
    echo "<td><span class=style2>" . $myrow[5] . "</span></td>";
    echo "<td><span class=style2>" . $myrow[6] . "</span></td>";
    echo "<td><span class=style2>" . $myrow[7] . "</span></td>";
echo "<td><span class=style2>" . $myrow[8] . "</span></td>";
echo "<td><span class=style2>" . $myrow[9] . "</span></td>";
echo "<td><span class=style2>" . $myrow[10] . "</span></td>";


$query1 = "SELECT SUM(amount) FROM report1 where staff_no = '$myrow[5]'";
$result1 = mysql_query($query1, $dblink);
while ($myrow1 = mysql_fetch_row($result1))
{
echo "<td><span class=style2>" .$myrow1[0]. "</span></td>";

  }
}
echo "</table>";
?>

 

why when i input one particular staff_no, it display blank when i click on submit button

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.