0

I wrote a query to select list from job table and display it in table but now I want to get only one value from the table that is id to activate or deactivate the job I am struck here my code is

<table class="table table-bordered table-hover table-striped">
<thead>
    <tr>
        <th>JOb ID</th>
        <th>CompanyID</th>
        <th>Job Description</th>
        <th>Duration</th>
        <th>Budget</th>
        <th>KeySkills</th>
        <th>Joining Date</th>
        <th>Expiry Date</th>
        <th>Min Experience</th>
        <th>Max Experience</th>
        <th>Status</th>
        <th>Set Status</th>        
    </tr>
</thead>
<tbody>
<?php

$res=mysql_query("SELECT * FROM job ORDER BY jid DESC");

while($row=mysql_fetch_array($res))
{
$num_rows = mysql_num_rows($res);

 ?>
<tr>
<td><p><?php echo $row['jid']; ?></p></td>
<td><p><?php echo $row['cid']; ?></p></td>
<td><p><?php echo $row['jdesc']; ?></p></td>
<td><p><?php echo $row['duration']; ?></p></td>
<td><p><?php echo $row['budget']; ?></p></td>
<td><p><?php echo $row['keyskills']; ?></p></td>
<td><p><?php echo $row['jdate']; ?></p></td>
<td><p><?php echo $row['edate']; ?></p></td>
<td><p><?php echo $row['cdexmin']; ?></p></td>
<td><p><?php echo $row['cdexmax']; ?></p></td>
<td><p>
<?php if($row['status']=="active")
{ 
echo "Active";
}
else {
echo "Inactive";
} ?></p></td>
<td>
  <form  action="" method="post"> <input type="submit"  name="submit"      value="activate"  />
<?php
if($_POST['submit']=='activate')
{

   $jid=$_POST['jid'];
    $delet_query = mysql_query("UPDATE job SET status='active' WHERE jid='". $jid."' ") or die(mysql_error());

 if($delet_query) {
  echo 'job with id '.$row[jid].' is activated, to refresh your page, click'.  '<a href='.$_SERVER["PHP_SELF"].' > here </a>';

   }
   }?>
</form>
<form action="" method="post"><?php $_POST['jid']=$row['jid'] ?><input type="submit"   name="submit"   value="deactivate"  />

<?php 
if($_POST['submit']=='deactivate')
{
     $jid=$_POST['jid'];
     $delet_query = mysql_query("UPDATE job SET status='deactive' WHERE jid='".     $jid."'  ") or die(mysql_error());

   if($delet_query) {
    echo 'job with id '.$row[jid].' is deactivated, to refresh your page, click'.       '<a href='.$_SERVER["PHP_SELF"].' > here </a>';

     }
    }?>
</form>
</td>        
</tr>
<?php
}
?>

<tr> 
<td> <p>Total Jobs:<?php echo $num_rows; ?></p></td>
</tr>

                                    </tbody>
                                </table>

I need to activate only one value from the table with id so please help me in a way that I created database table

1
  • if you want only one record, either of the 2 things can be done : 1. use mysql_fetch_row() instead of mysql_fetch_array, or 2. use where clause in your select query Commented Apr 1, 2016 at 5:15

3 Answers 3

1
<?php
if ($_POST['submit'] == 'activate') {

    $jid = $_POST['jid'];
    $delet_query = mysql_query("UPDATE job SET status='active' WHERE jid='" . $jid . "' ") or die(mysql_error());

    if ($delet_query) {
        echo 'job with id ' . $row[jid] . ' is activated, to refresh your page, click' . '<a href=' .
            $_SERVER["PHP_SELF"] . ' > here </a>';

    }
} elseif ($_POST['submit'] == 'deactivate') {
    $jid = $_POST['jid'];
    $delet_query = mysql_query("UPDATE job SET status='deactive' WHERE jid='" . $jid . "'  ") or die(mysql_error());

    if ($delet_query) {
        echo 'job with id ' . $row[jid] . ' is deactivated, to refresh your page, click' . '<a href=' .
            $_SERVER["PHP_SELF"] . ' > here </a>';

    }
}
?>
<table class="table table-bordered table-hover table-striped">
    <thead>
    <tr>
        <th>JOb ID</th>
        <th>CompanyID</th>
        <th>Job Description</th>
        <th>Duration</th>
        <th>Budget</th>
        <th>KeySkills</th>
        <th>Joining Date</th>
        <th>Expiry Date</th>
        <th>Min Experience</th>
        <th>Max Experience</th>
        <th>Status</th>
        <th>Set Status</th>


    </tr>
    </thead>
    <tbody>
    <?php

    $res = mysql_query("SELECT * FROM job ORDER BY jid DESC");

    while ($row = mysql_fetch_array($res)) {
        $num_rows = mysql_num_rows($res);

        ?>
        <tr>
            <td><p><?php echo $row['jid']; ?></p></td>
            <td><p><?php echo $row['cid']; ?></p></td>
            <td><p><?php echo $row['jdesc']; ?></p></td>
            <td><p><?php echo $row['duration']; ?></p></td>
            <td><p><?php echo $row['budget']; ?></p></td>
            <td><p><?php echo $row['keyskills']; ?></p></td>
            <td><p><?php echo $row['jdate']; ?></p></td>
            <td><p><?php echo $row['edate']; ?></p></td>
            <td><p><?php echo $row['cdexmin']; ?></p></td>
            <td><p><?php echo $row['cdexmax']; ?></p></td>
            <td><p>
                    <?php if ($row['status'] == "active") {
                        echo "Active";
                    } else {
                        echo "Inactive";
                    } ?></p>
            </td>
            <td>

                <form action="" method="post">
                    <input type="hidden" name="jid" value="<?php echo $row['jid']; ?>"/>
                    <input type="submit" name="submit" value="activate"/>
                </form>

                <form action="" method="post">
                    <input type="hidden" name="jid" value="<?php echo $row['jid']; ?>"/>
                    <input type="submit" name="submit" value="deactivate"/>
                </form>
             </td>
        </tr>
        <?php
    }
    ?>

    <tr>
        <td><p>Total Jobs:<?php echo $num_rows; ?></p></td>
    </tr>

    </tbody>
</table>
Sign up to request clarification or add additional context in comments.

1 Comment

thank you ajay it's working after i moved my form submission function out of while loop
0

Just modified your coding (must improve your coding)

<table class="table table-bordered table-hover table-striped">
<thead>
    <tr>
        <th>JOb ID</th>
        <th>CompanyID</th>
        <th>Job Description</th>
        <th>Duration</th>
        <th>Budget</th>
        <th>KeySkills</th>
        <th>Joining Date</th>
        <th>Expiry Date</th>
        <th>Min Experience</th>
        <th>Max Experience</th>
        <th>Status</th>
        <th>Set Status</th>        
    </tr>
</thead>
<tbody>
<?php

$res=mysql_query("SELECT * FROM job ORDER BY jid DESC");
$jid = isset($_POST['jid']) ? $_POST['jid'] : '';

while($row=mysql_fetch_array($res))
{
$num_rows = mysql_num_rows($res);

 ?>
<tr>
<td><p><?php echo $row['jid']; ?></p></td>
<td><p><?php echo $row['cid']; ?></p></td>
<td><p><?php echo $row['jdesc']; ?></p></td>
<td><p><?php echo $row['duration']; ?></p></td>
<td><p><?php echo $row['budget']; ?></p></td>
<td><p><?php echo $row['keyskills']; ?></p></td>
<td><p><?php echo $row['jdate']; ?></p></td>
<td><p><?php echo $row['edate']; ?></p></td>
<td><p><?php echo $row['cdexmin']; ?></p></td>
<td><p><?php echo $row['cdexmax']; ?></p></td>
<td><p>
<?php if($row['status']=="active")
{ 
echo "Active";
}
else {
echo "Inactive";
} ?></p></td>
<td>
  <form  action="" method="post"> 
  <input type="submit"  name="submit" value="activate"  />
  <input type="hidden"  name="jid" value="<?php echo $row['jid']; ?>"  />
<?php
if($_POST['submit']=='activate' && $row['jid']==$jid)
{
    $delet_query = mysql_query("UPDATE job SET status='active' WHERE jid='". $jid."' ") or die(mysql_error());

 if($delet_query) {
  echo 'job with id '.$row[jid].' is activated, to refresh your page, click'.  '<a href='.$_SERVER["PHP_SELF"].' > here </a>';

   }
   }
   ?>
</form>


<form action="" method="post">
<input type="submit"   name="submit"   value="deactivate"  />
<input type="hidden"  name="jid" value="<?php echo $row['jid']; ?>"  />
<?php 
if($_POST['submit']=='deactivate' && $row['jid']==$jid)
{
     $delet_query = mysql_query("UPDATE job SET status='deactive' WHERE jid='".     $jid."'  ") or die(mysql_error());

   if($delet_query) {
    echo 'job with id '.$row[jid].' is deactivated, to refresh your page, click'.       '<a href='.$_SERVER["PHP_SELF"].' > here </a>';

     }
    }
?>
</form>
</td>        
</tr>
<?php
}
?>

<tr> 
<td> <p>Total Jobs:<?php echo $num_rows; ?></p></td>
</tr>

    </tbody>
</table>

Note: The coding you need to improve. At present your display tables, updates inside while loop are messy. During the submit, the page should goes to next page, do the ncessary actions and make return to the actual page

Comments

0

Use the below code it will work

  <?php 
  if(isset($_GET['jid'])) 
  {
  $jid=$_GET['jid'];
  $status=$_GET['status'];
  if($status=='active') { $new_status='inactive'; } else { $new_status='active'; }
  $delet_query = mysql_query("UPDATE job SET status='$new_status' WHERE jid='". $jid."' ") or die(mysql_error());
  if($delet_query) {
   echo 'job with id '.$row[jid].' is activated, to refresh your page, click'.  '<a href='.$_SERVER["PHP_SELF"].' > here </a>';
   }
   }
    <table class="table table-bordered table-hover table-striped">
     <thead>
       <tr>
       <th>JOb ID</th>
       <th>CompanyID</th>
       <th>Job Description</th>
       <th>Duration</th>
       <th>Budget</th>
       <th>KeySkills</th>
       <th>Joining Date</th>
       <th>Expiry Date</th>
       <th>Min Experience</th>
       <th>Max Experience</th>
       <th>Status</th>
       <th>Set Status</th>
       </tr>
  </thead>
  <tbody>
<?php
$res=mysql_query("SELECT * FROM job ORDER BY jid DESC");
while($row=mysql_fetch_array($res))
{
?>
<tr>
<td><p><?php echo $row['jid']; ?></p></td>
<td><p><?php echo $row['cid']; ?></p></td>
<td><p><?php echo $row['jdesc']; ?></p></td>
<td><p><?php echo $row['duration']; ?></p></td>
<td><p><?php echo $row['budget']; ?></p></td>
<td><p><?php echo $row['keyskills']; ?></p></td>
<td><p><?php echo $row['jdate']; ?></p></td>
<td><p><?php echo $row['edate']; ?></p></td> 
<td><p><?php echo $row['cdexmin']; ?></p></td>
<td><p><?php echo $row['cdexmax']; ?></p></td>
<td><p><?php if($row['status']=="active") { echo "Active"; } else { echo "Inactive"; } ?></p></td>
<td><a href="?jid=<?php echo $row['jid']; ?>&status=<?php echo $row['status']; ?>"><?php if($row['status']=="active") { echo "Activate"; } else { echo "DeActivate"; } ?></a></td></tr>
<?php } ?>
</tbody>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.