从一列更新一个数据名称,所有具有相同名称且在同一列中的数据也将更新

时间:2013-11-26 06:39:18

标签: php mysql

我需要一些帮助,我有一张名为POSITION的桌子。该表的列是p_no,p_name和p_training_type。 p_name代表位置名称。一个p_name将参加许多类型的培训,例如它可以像这样

p_no | p_name | p_training_type

1 |总经理|海上生存  2 |总经理|安全驾驶  3 |技术助理|海上生存  4 | HSSE MANAGER |海上生存

我想要做的是更新一个p_name例子总经理所有甲肝相同p_name也将change..for例如p_name,用户将搜索他们想要在update1.php页面更新p_name,但是用户不记得位置名称的全名..所以用户将输入MANAGER ..所以结果是。

  1. 总经理
  2. HSSE MANAGER
  3. 接下来,用户将单击他们想要更新的位置名称。例如,用户单击GENERAL MANAGER。

    所以位置名称将被带到另一个页面(update2.php),其中GENERAL MANAGER将以可编辑的形式出现。通过键入GENERAL MANAGER作为ADMIN MANAGER编辑名称n单击更新按钮..它将转到update3.php,此页面中包含更新查询。

    对于结果,没有错误显示..但数据stil不更新仍然是GENERAL MANAGER而不是ADMIN MANAGER。我不知道问题出在哪里..以下是更新过程的相关页面..

    update1.php

            <form action="update1.php" method="post" name="form1" id="form1">
            <table><tr>
               <th scope="col"><span class="style1"></span>Type:</span></th>
               <th colspan="2"><select name="method">
                  <option value="" selected>-Please choose-</option>
                  <option value="position">By Position</option>
                  <option value="training">By Training Type</option>
            </select></th>
            </tr>
            <tr>
               <th scope="col">Input Query :</th>
               <th><input type="text" id="search" name="search" size="50" /></th>
               <th><input type="submit" name="button" id="button" value="Search" /></th>
            </tr></table>
            </form>
    
             <?php  $resultName=$_POST['search']; ?>   
    
    <?php
    
    $search =$_POST['search'];
    $method =$_POST['method'];
    
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("training", $con);
    
    if($method=="") 
        { 
         }
    
    else if($method=="position"){    
    $result = mysql_query("SELECT distinct p_name FROM test WHERE p_name LIKE '%$search%'");
    $num_rows = mysql_num_rows($result);
    }
    
    else if($method=="training"){    
    $result = mysql_query("SELECT distinct p_training_type FROM test WHERE p_training_type LIKE '%$search%'");
    $num_rows = mysql_num_rows($result);
    } 
    
    
    echo "<font  size=2>"." Total number of result found for "."<font color='#FF0000' size=2>".$resultName."<font color='#43423F' size=2>"." :"."<font color='#FF0000' size=2>"." $num_rows"."<font color='#43423F' size=2>";
    ?>
    <div
    style="border: 0px; width: 620px; height: 400px; overflow: auto;">     
    <?php
    echo "<table width='600' height='0' border='0' bordercolor='#000000' bgcolor='#FFFFFF'>
    <tr>
       <th bgcolor='#000000'><font color='#FFFFFF'>Result </th>
       <th bgcolor='#000000'><font color='#FFFFFF'>Action</th>
    </tr>";
    
    if($method!="") 
        {   
    
    while($row = mysql_fetch_array($result))
      {
    
      if($method=="position"){
       echo "<tr>
                <td height=0 bgcolor='#FFFFFF'>"."<font size=2>".$row['p_name']."</td>
            <td height=0 bgcolor='#FFFFFF'>"."<font size=2><a href='update2.php?p_name=$row[p_name]'><img src='edit.gif' title='Edit' /></a>
             <a href='delete2.php?valid=".$row['p_name']."'><img src='delete.gif' title='Delete' /></a></td>";}
    
     else if($method=="training"){
      echo "<tr><td height=0 bgcolor='#FFFFFF'>"."<font size=2>"."<input name='p_training_type' width='25' type='text' value='".$row['p_training_type']."'/>"."</td>";}
      }
      }
    echo "</table>";
    
    mysql_close($con);
    
    ?>
    

    update2.php

    <table width="600" border="0">
      <tr>
        <th scope="col">Designation</th>
        <th width="17" scope="col">Action</th>
      </tr>
      <form name="form2"  method="POST" action="update3.php"  enctype="multipart/form-data">
      <tr>
        <td><input name="p_name" type="text" id="p_name" value="<?php echo $row_RecordsetZ['p_name']; ?>" size="20" /></td>
        <td><input type="submit" value="Update" id="update" name="update"/></td>
      </tr>
      </form>
    

    update3.php

    <?php
    $p_name=$_POST['p_name'];
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("training", $con);
    {
    
    $sql=mysql_query("UPDATE POSITION SET p_name='$p_name' WHERE p_name='".$p_name."'") or die("Error: " . mysql_error());
    
        print '<script type="text/javascript">'; 
        print 'alert("The details are successfully insert")'; 
        print '</script>';
    
        mysql_close($con);
    
    }
    
        echo "<script language=\"JavaScript\">{                                                                                         
        location.href=\"upate1.php\";                                                                                               
        }</script>";
        header('Content-Type: application/pdf');
    ?>
    

1 个答案:

答案 0 :(得分:0)

您需要通过提交表单

上的隐藏字段来隐藏PID
<form name="form2"  method="POST" action="update3.php"  enctype="multipart/form-data">
  <tr>
     <td><input name="p_id" type="hidden" id="p_id" value="<?php echo $row_RecordsetZ['p_id']; ?>" size="20" /></td>
     <td><input name="p_name" type="text" id="p_name" value="<?php echo $row_RecordsetZ['p_name']; ?>" size="20" /></td>
     <td><input type="submit" value="Update" id="update" name="update"/></td>
  </tr>
</form>

您的提交查询将是

$sql=mysql_query("UPDATE POSITION 
                  SET p_name='$p_name' 
                  WHERE p_name='".$p_name."' AND p_id = '".$_POST['p_id']."'");