使用文本框使用php表单更新MySQL数据库

时间:2016-04-07 03:51:51

标签: javascript php html mysql sql-update

您好我在使用Netbeans中的PHP表单更新MySQL时遇到的问题是: “
注意:未定义的变量: C:\ Xampp \ htdocs \ Resume_DB \ PHP \ Edication_Edit.php 中的fetched_row 75

在我的所有4个文本框中。我的代码是:

    <?php

    mysql_connect("localhost", "root", "");
    mysql_select_db("cs266db_db1");


    if(isset($_GET['edit_id']))
 {
  $sql_query="SELECT * FROM education Where Edit_Education=".$_GET['edit_id'];
$result_set=  mysql_query($sql_query);
$fetched_row= mysql_fetch_array($result_set);
}

 if(isset($_POST['btn_update']))
 {
//variables for input data
$Class = $_POST['Class'];
$Discipline = $_POST['Discipline'];
$Description = $_POST['Description'];
$Term = $_POST['Term'];


//sql query for update data 
   $sql_query = "UPDATE Education SET Class='$Class',  Discipline='$Discipline', Description='$Description', Term='$Term' WHERE               Education_Edit=".$_GET['edit_id'];



  //sql query execution function

  if(mysql_query($sql_query))
 {
   ?>
<script type='text/javascript'>
   alert('Data Has Been Updated');
   window.location.href='Eduaction.php';
   </script>
    <?php
 }
 else 
{
?>
<script type='text/javascript'>
    alert('Error Occured while Updating Data');
    </script>
    <?php
 }

  //sql query execution function 
}

  if(isset($_POST['btn-cancel']))
 {
header("Location: Education.php");
 }
  ?>

       <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Edit Education</title>

  </head>
  <body>
 <center>

   <div id="header">
    <div id="content">
    <label>Edit Education</label>
    </div>
  </div>
  <div id="body">
  <div id="content">
    <form method="post">
     <table align="center">
    <tr>
   **<td><input type="text" name="Class" placeholder="Class" value="<?php echo $fetched_row['Class']; ?>" required /></td>
   </tr>
    <tr>
    <td><input type="text" name="Discipline" placeholder="Discipline" value="<?php echo $fetched_row['Discipline']; ?>" required /></td>
    </tr>
    <tr>
    <td><input type="text" name="Description" placeholder="Description" value="<?php echo $fetched_row['Description']; ?>" required /></td>
</tr>
    <tr>
        <td><input type="Text" name="Term" placeholder="Term" value="<?php echo $fetched_row['Term']; ?>" required</td>**
    </tr>
<tr>
<td>
<button type="submit" name="btn-update"><strong>UPDATE</strong></button>
<button type="submit" name="btn-cancel"><strong>Cancel</strong></button>
</td>
</tr>
</table>
</form>
</div>

  <div id="footer">
  <div id="content">

  </div>
 </div>

 </center>
 </body>
 </html>

有什么想法?谢谢你的帮助

3 个答案:

答案 0 :(得分:1)

问题是只有在设置$ _GET [&#39; edit_id&#39;]时才会分配fetched_row。否则它不存在,但即使未设置$ _GET [&#39; edit_id&#39;],您仍然会尝试使用它。

更改

<td><input type="Text" name="Term" placeholder="Term" value="<?php echo $fetched_row['Term']; ?>" required</td>**

要:

<td><input type="Text" name="Term" placeholder="Term" value="<?php echo (isset($fetched_row['Term']))? $fetched_row['Term'] : ''; ?>" required</td>**

答案 1 :(得分:1)

在更新查询中使用“Education_Edit”代替“Edit_Education”而不是“Education_Edit”,而您在选择查询中使用“Edit_Education”。

<form name='serviceForm'>
      <select ng-change='ValidateInput()' id='colorId1' ng-model = "color1" class = "form-control"
          ng-options = "color as color for color in colors" required>

          <option value="">Choose an option</option> 
      </select><br><br>

      <select ng-change='ValidateInput()' id='colorId2' ng-model = "color2" class = "form-control"
          ng-options = "color as color for color in colors" required>

          <option value="">Choose an option</option> 
      </select><br><br>

      <input type='submit' ng-click='ValidateInput()' value='Submit'>
   </form>

答案 2 :(得分:0)

更改if(isset($ _ POST [&#39; btn_update&#39;]))if(isset($ _ POST [&#39; btn-update&#39;]))因为在html按钮标记中你命名按钮作为btn-update。 在更新查询更改列名称Education_Edit更改为Edit_Education