Dillon Burke
Dillon Burke

Reputation: 49

Update MySQL database using php form using text boxes

Hello I have an issue with updating MySQL using PHP form in Netbeans the issue that occurs is: "
Notice: Undefined variable: fetched_row in C:\Xampp\htdocs\Resume_DB\PHP\Edication_Edit.php on line 75
"

In all 4 of my text boxes. The code I have is :

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

Any thoughts? Thanks for your help guys

Upvotes: 0

Views: 688

Answers (3)

Akshay Honnalli
Akshay Honnalli

Reputation: 136

change if(isset($_POST['btn_update'])) to if(isset($_POST['btn-update'])) because in html button tag you named button as btn-update . In update query change column name Education_Edit change to Edit_Education

Upvotes: 0

Nabeel
Nabeel

Reputation: 147

It seems instead of "Edit_Education" you are using "Education_Edit" in update query or instead of "Education_Edit" you are using "Edit_Education" in select query.

$sql_query="SELECT * FROM education Where Edit_Education=".$_GET['edit_id'];

$sql_query = "UPDATE Education SET Class='$Class',  Discipline='$Discipline', Description='$Description', Term='$Term' WHERE Education_Edit=".$_GET['edit_id'];

Upvotes: 1

Greg Borbonus
Greg Borbonus

Reputation: 1384

The problem is that fetched_row is only assigned when $_GET['edit_id'] is set. Otherwise it does not exist, but you're still trying to use it, even if $_GET['edit_id'] is not set.

Change

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

To:

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

Upvotes: 1

Related Questions