使用查找表更新/编辑mysql数据

时间:2012-09-29 04:20:24

标签: php mysql

我有4张桌子:

Departments:

dept_id         dept_name

1       dept 1
2       dept 2
3       dept 3
4       dept 4

-------------

Hours:

hours_id    proj_hours
1       18
2       36

-------------

Project:

proj_id     proj_name
1       project1
2       project2

-------------

Summary:

Summary_id  proj_id     hours_id    dept_id
1       1       1       1
2       2       1       2
3       1       2       3
4       1       1       4

-----------------------------------------------------

SELECT *
FROM summary S
JOIN projects P ON S.proj_id = P.proj_id
JOIN departments D ON D.dept_id = S.dept_id
JOIN hours H ON H.hours_id = S.hours_id

我有一个index.php表,它整齐地显示了上面的连接数据和一个破碎的编辑功能.....我无法弄清楚如何从手动编辑功能更新查找表中的实际值。

<td align="center"><a href="edit2.php?id=<?php echo $rows['summary_id']; ?>">Edit</a></td>

用于将summary_id发送到编辑页面,该页面是defintely发送的,但不是值。我有空白字段。从那里,我想让我的字段准备好编辑,但是再次,空白字段......我遇到了“id”的问题,而不是“值”和格式正确。

建议?

edit.php代码:

<?php
        // Connect to server and select database.
        mysql_connect("$host", "$username", "$password")or die("cannot connect");
        mysql_select_db("$db_name")or die("cannot select DB");

        if(isset($_GET['summary_id']))  
        {
            $id = $_GET['summary_id'];
            $res = mysql_query("SELECT * FROM summary WHERE proj_id='$proj_id'");
            $rows = mysql_fetch_array($res);
        }
        if (isset($_POST['nproj_id']))  

        {
            $summary_id     = $_POST['summary_id'];     
            $nproj_id       = $_POST['nproj_id'];
            $nclarity_id    = $_POST['clarity_id'];
            $nhours_id      = $_POST['nhours_id'];
            $ndept_id       = $_POST['ndept_id'];
            $sql            = "UPDATE summary SET clarity_id='$nclarity_id' WHERE proj_id='$proj_id'" 
            or die ("couldn't update".mysql_error());

            $res            = mysql_query($sql) or die ("couldn't update".mysql_error());
                            echo "<meta http-equiv='refresh' content='0;url=index2.php'>";
        }

?>


<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="edit2.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>

</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Clarity ID</strong></td>
<td align="center"><strong>Hours</strong></td>
<td align="center"><strong>Department</strong></td>

</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input name="nclarity_id" type="text" id="nclarity_id" value="<?php echo $rows['clarity_id']; ?>">
</td>
<td align="center">
<input name="nproj_hours" type="text" id="nproj_hours" value="<?php echo $rows['proj_hours']; ?>" size="15">
</td>
<td>
<input name="ndept_name" type="text" id="ndept_name" value="<?php echo $rows['dept_name']; ?>" size="15">
</td>
</tr>
<tr>
<td>&nbsp;</td>

<td>
<input name="summary_id" type="hidden" id="summary_id" value="<?php echo $rows['summary_id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Update">
</td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?php
// close connection
mysql_close();
?>

这是我的index.php ....你所做的更正仍然无效。 :(

<?php   
 // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

        if($_POST['submitted'])
        {
            $proj_id            = $_POST['proj_id'];
            $summary_id         = $_POST['summary_id'];
            $clarity_id         = $_POST['clarity_id'];
            $hours_id           = $_POST['hours_id'];
            $proj_hours         = $_POST['proj_hours'];
            $dept_name          = $_POST['dept_name'];
            $dept_id            = $_POST['dept_id'];

            $sqlinsert      = "INSERT INTO summary (summary_id,proj_id,hours_id,dept_id) VALUES (NULL,
            '$proj_id','$hours_id','$dept_id'";


            if (!mysql_query($sqlinsert)) {
            die('error inserting new record'.mysql_error());
        } // end of the nested if statement


        }
            $result = mysql_query("SELECT *
                        FROM summary S
                        JOIN projects P ON S.proj_id = P.proj_id
                        JOIN departments D ON D.dept_id = S.dept_id
                        JOIN hours H ON H.hours_id = S.hours_id
                        LIMIT 0 , 30");


    ?>

    <form action="index.php" method="post">
    Clarity #: <input type="text" name="clarity_id"><br />
    Estimated Hours: <input type="text" name="proj_hours"><br /> 
    Department: <input type="text" name="dept_name"><br />

    <input type="hidden" name="submitted" value="true"/>
    <input type="submit" value="enter">
    </form>



    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td>
    <table width="400" border="1" cellspacing="0" cellpadding="3">
    <tr>
    <td colspan="4"><strong>List data from mysql </strong> </td>
    </tr>

    <tr>
    <td align="center"><strong>Clarity#</strong></td>
    <td align="center"><strong>Estimated Hours</strong></td>
    <td align="center"><strong>Department</strong></td>
    </tr>

    <?php
    while($rows=mysql_fetch_array($result)){
    ?>

    <tr>
    <td><?php echo $rows['clarity_id']; ?></td>
    <td><?php echo $rows['proj_hours']; ?></td>
    <td><?php echo $rows['dept_name']; ?></td>
    <td align="center"><a href="edit2.php?id=<?php echo $rows['summary_id']; ?>">Edit</a></td>
    </tr>

    <?php
    }
    ?>

    </table>
    </td>
    </tr>
    </table>

    <?php
    mysql_close();
    ?>

1 个答案:

答案 0 :(得分:0)

进行了一些更正......这个

    <?php
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    if(isset($_GET['summary_id']))  
    {
        $id = $_GET['summary_id'];
        $res = mysql_query("SELECT * FROM summary WHERE `summary_id`='$id'");//Correction is made here
        if($res){
        $rows = mysql_fetch_array($res);
        }
        else{
         echo "Fetch Error: ".mysql_error();
        }
    }
    if (isset($_POST['btnSubmit']))  //correction is here

    {
        $summary_id     = $_POST['summary_id'];     
        $nproj_id       = $_POST['nproj_id'];
        $nclarity_id    = $_POST['clarity_id'];
        $nhours_id      = $_POST['nhours_id'];
        $ndept_id       = $_POST['ndept_id'];
        $sql            = "UPDATE summary SET clarity_id='$nclarity_id' WHERE proj_id='$proj_id' AND `summary_id`='{$summary_id}'"; //correction is here

        if(mysql_query($sql)){echo "Update successful";}else{echo "couldn't update".mysql_error();}
                        echo "<meta http-equiv='refresh' content='0;url=index2.php'>";
    }

    ?>


   <table width="400" border="0" cellspacing="1" cellpadding="0">
   <tr>
   <form name="form1" method="post" action="edit2.php">
   <td>
   <table width="100%" border="0" cellspacing="1" cellpadding="0">
   <tr>
   <td>&nbsp;</td>
   <td colspan="3"><strong>Update data in mysql</strong> </td>
   </tr>
   <tr>
   <td align="center">&nbsp;</td>
   <td align="center">&nbsp;</td>
   <td align="center">&nbsp;</td>

   </tr>
   <tr>
   <td align="center">&nbsp;</td>
   <td align="center"><strong>Clarity ID</strong></td>
   <td align="center"><strong>Hours</strong></td>
   <td align="center"><strong>Department</strong></td>

   </tr>
   <tr>
   <td>&nbsp;</td>
   <td align="center">
   <input name="nclarity_id" type="text" id="nclarity_id" value="<?php echo $rows['clarity_id']; ?>">
   </td>
   <td align="center">
   <input name="nproj_hours" type="text" id="nproj_hours" value="<?php echo $rows['proj_hours']; ?>" size="15">
   </td>
   <td>
   <input name="ndept_name" type="text" id="ndept_name" value="<?php echo $rows['dept_name']; ?>" size="15">
   </td>
   </tr>
   <tr>
   <td>&nbsp;</td>

   <td>
   <input name="summary_id" type="hidden" id="summary_id" value="<?php echo $rows['summary_id']; ?>">
   </td>
   <td align="center">
   <input type="submit" name="btnSubmit" value="Update"> <!--correction is here-->
   </td>
   <td>&nbsp;</td>
   </tr>
   </table>
   </td>
   </form>
   </tr>
   </table>

   <?php
   // close connection
   mysql_close();
   ?>