2016-04-01 29 views
0

我寫了一個查詢,以便從工作表中選擇列表,並在表中顯示,但現在我想從即ID激活或取消我在這裏打了那份工作,我的代碼是如何在php表中寫入while循環時從php中獲取一個值?

<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> 
表只得到一個價值

我需要激活與ID表只有一個值,請幫助我,我創建數據庫表

+0

如果你只想要一個記錄,可以做的兩件事之一: 1.使用'mysql_fetch_row()'而不是'mysql_fetch_array',或 2.在'select query'中使用'where子句'' – Nehal

回答

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> 
+0

它不工作,仍然顯示相同輸出 –

+0

謝謝你ajay它工作後,我移動我的表單提交樂趣ction out while循環 –

0

剛剛修改的方式您的編碼(必須提高你的編碼)

<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> 

注意: 您需要改進的編碼。目前你的顯示錶,while循環裏面的更新都很麻煩。 在提交時,網頁應該進入下一個頁面,執行ncessary行動,使返回的實際頁面

0

使用下面的代碼,將工作

<?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>