Page 1 of 1

using $_POST in an SQL Query

Posted: Tue Mar 03, 2009 11:38 am
by nishmgopal
Hi guy

I am trying to use $_POST in my query like:

$jobname=$_POST['job'];
$query = "SELECT * FROM Job_Spec WHERE Project_Name ='$jobname'";

But the page returns nothing, not even an error. The full code is pasted below.

Can anyone help?


Code: Select all

$jobname=$_POST['job'];
$query = "SELECT * FROM Job_Spec WHERE Project_Name ='$jobname'";
            
  $result = mysql_query($query)
       or die ("Couldn't execute query.");
 
while ($row=mysql_fetch_array($result)){
 
        $name=$row['Project_Name'];
        $skill1=$row['Java'];
        $skill2=$row['Team_Work'];
        $skill3=$row['Leadership'];
 
 
echo "<table width='200' border='1'>
<strong>$name</strong> <br></br>
  <tr bgcolor='#4096EE'> <div align='center'>
    <td><div align='center'><strong>Skill</strong></div></td>
    <td><div align='center'><strong>Score</strong></div></td>
  </tr>
  <tr>
    <td>Teamwork</td>
    <td>$skill2</td>
  </tr>
  <tr>
    <td>Leadership</td>
    <td>$skill3</td>
  </tr>
  <tr>
    <td>Java</td>
    <td>$skill4</td>
  </tr>

Re: using $_POST in an SQL Query

Posted: Tue Mar 03, 2009 11:59 am
by ben.artiss
Where is the form at? In a different file? It sounds like your $_POST['job'] is empty or something. Could you paste the code from the form?

Re: using $_POST in an SQL Query

Posted: Tue Mar 03, 2009 12:01 pm
by nishmgopal
the code for the form is:

Code: Select all

$query = "SELECT * FROM Job_Spec";
 $result = mysql_query($query);
  
 echo "
     <h1 class='title2'>Upcoming Project Roles</h1>
     <p>From the menu below please select the Project Role:</p>
     <form id='form2' method='post' action='go.php'>
         <p>
             <label>
                 <select name='job' id='job'>
 ";
  
 while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
     $Project_Name= $row['Project_Name'];
    echo "<option value>$Project_Name</option>";
 }
   echo "</select></label></p></form>";
?>
<form id="form2" method="post" action="go.php">
  <p>
    <label>
    <input type="submit" name="job" id="button" value="Go" />
      </label>
  </p>
</form>

Re: using $_POST in an SQL Query

Posted: Tue Mar 03, 2009 12:07 pm
by ben.artiss
You've got two inputs with name="job" and they're in separate forms. If you're wanting the value from the select list you need to set a value in the option too. Try something like this:

Code: Select all

$query = "SELECT * FROM Job_Spec";
$result = mysql_query($query);
 
echo "<h1 class='title2'>Upcoming Project Roles</h1>
<p>From the menu below please select the Project Role:</p>
 
<form id='form2' method='post' action='go.php'>
<p>
<label>
<select name='job' id='job'>";
 
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    $Project_Name= $row['Project_Name'];
    echo "<option value=\"$Project_Name\">$Project_Name</option>";
}
 
echo "</select></label></p>";
?>
 
<p>
<label>
<input type="submit" name="notjob" id="button" value="Go" />
</label>
</p>
</form>

Re: using $_POST in an SQL Query

Posted: Tue Mar 03, 2009 12:22 pm
by nishmgopal
once again thank you so much!

Before i start another thread, just wanted to know if you could help me with the following

as you've seen from my code I am only displaying records that I am calling. For example:

$name1=$row1['Project_Name'];
$skill1=$row1['Java'];
$skill2=$row1['Team_Work'];
$skill3=$row1['Leadership'];

will only display Project_Name, Java, Team_Work and Leadership

How can I get it to display all the records? Because if another record is added to the database I wil have to alter my code. Is there any way of doing this dynamically, so if a new record got added, then it will display in the relevant page without any extra coding?


Thank you!

Re: using $_POST in an SQL Query

Posted: Tue Mar 03, 2009 12:28 pm
by ben.artiss
From what you're code says here you're getting everything from the table, so if a new record is added it'll load that too. Is that what you mean?

Re: using $_POST in an SQL Query

Posted: Tue Mar 03, 2009 12:37 pm
by nishmgopal
if you look at my code below:

Code: Select all

$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$getjob'";
            
  $result1 = mysql_query($query1)
       or die ("Couldn't execute query.");
 
while ($row1=mysql_fetch_array($result1)){
 
        $name1=$row1['Project_Name'];
        $skill1=$row1['Java'];
        $skill2=$row1['Team_Work'];
        $skill3=$row1['Leadership'];
        $skill4=$row1['Communication'];
        $skill5=$row1['Problem_Solving'];
 
Right now the above is displayed in Table format, and it works fine, but what if I was to add another field?
For example Cooking, then I would physically have to go into the code and create another array, for example, $skill6=$row1['Cooking'];

Is there anyway that this process can be taken out?

Re: using $_POST in an SQL Query

Posted: Tue Mar 03, 2009 12:47 pm
by ben.artiss
To the best of my knowledge it sounds like you'll have to get your fingers dirty. It would be pretty complicated to make it load info from undefined columns, but it's certainly not impossible. If you really don't like going back in to edit the code maybe you could check out some info on 'SHOW COLUMNS' for mysql and compare the result to an array of existing columns. But I think your best bet is manual labor!

Re: using $_POST in an SQL Query

Posted: Tue Mar 03, 2009 12:50 pm
by nishmgopal
just as I feared! will try and complete one part first then perhaps look to further develop.

Thank you for your help today.