2012-09-29 101 views
0

更新/編輯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發送,而不是價值觀..我有空白字段。從那裏,我想讓我的領域能夠編輯,但是再次,空白的領域...我有一個問題的「身份證」,而不是「價值」和格式正確。

對此提出建議?

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(); 
    ?> 
+0

您需要選擇在編輯頁面查詢和使用ID U可以獲取價值,在你的領域顯示選擇查詢數據 – Jalpesh

+0

我還在學習,所以我不太確定我是否掌握了那個...但你可以給我一個例子嗎?謝謝! – Mike

回答

0

一些修改是made..try這

<?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(); 
    ?> 
+0

我加了我的index.php,因爲你的更正似乎不起作用:( – Mike

+0

什麼是當時的錯誤... –

+0

在答案中做出了一些改變...嘗試併發布結果... –