4 Replies - 387 Views - Last Post: 20 March 2017 - 02:03 PM

#1 hiya1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 21-August 16

Can't edit rows in my table

Posted 20 March 2017 - 01:30 PM

Hello all so i'm trying to allow the user to edit their data after they've submitted it. I have a staff table set up with a auto incrementing primary key. It's this bit of my code that's giving me bother "//getting id from url
$StaffID = $_GET['StaffID'];" I cannot for the life of me figure out what is wrong with it because the syntax seems correct. It's telling me the indexed is unidentified

<?php
// including the database connection file
include_once("connect.php");
 
if(isset($_POST['update']))
{    
    $StaffID = $_POST['StaffID'];


  
    // checking empty fields
    if(empty($Name) || empty($Address) || empty($Telephone) || empty($BusinessID)) {            
        if(empty($Name)) {
            echo "<font color='red'>Name field is empty.</font><br/>";
        }
        
        if(empty($Address)) {
            echo "<font color='red'>Age field is empty.</font><br/>";
        }
        
        if(empty($Telephone)) {
            echo "<font color='red'>Email field is empty.</font><br/>";
        }   
        
        if(empty($BusinessID)){
            echo "<font color='red'>Email field is empty.</font><br>/";
        }
    } else {    
        //updating the table
        $result = mysqli_query($conn, "UPDATE staff SET Name='$Name',Address='$Address',Telephone='$Telephone', BusinessID='$BusinessID' WHERE StaffID = $StaffID");
        
        //redirectig to the display page. In our case, it is index.php
        header("Location: HomePHP.php");
    }
}
?>
<?php
//getting id from url
$StaffID = $_GET['StaffID'];
 
//selecting data associated with this particular id
$result = mysqli_query($conn, "SELECT * FROM staff WHERE StaffID=$StaffID");
 
while($res = mysqli_fetch_array($result))
{
    
    
    $Name = $res['Name'];
    $Address = $res['Address'];
    $Telephone = $res['Telephone'];
    $BusinessID = $res['BusinessID'];
}
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Can't edit rows in my table

#2 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2343
  • View blog
  • Posts: 9,400
  • Joined: 03-December 12

Re: Can't edit rows in my table

Posted 20 March 2017 - 01:41 PM

Don't do this:
"UPDATE staff SET Name='$Name',Address='$Address',Telephone='$Telephone', BusinessID='$BusinessID' WHERE StaffID = $StaffID"


$StaffID = $_POST['StaffID'];

You are assigning the value regardless of whether it exists.
Was This Post Helpful? 0
  • +
  • -

#3 hiya1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 21-August 16

Re: Can't edit rows in my table

Posted 20 March 2017 - 01:44 PM

View Postastonecipher, on 20 March 2017 - 01:41 PM, said:

Don't do this:
"UPDATE staff SET Name='$Name',Address='$Address',Telephone='$Telephone', BusinessID='$BusinessID' WHERE StaffID = $StaffID"


$StaffID = $_POST['StaffID'];

You are assigning the value regardless of whether it exists.


Thankyou for the reply. What would you suggest?

I got rid of the

$StaffID = $_POST['StaffID'];


But it's still giving me an error on StaffID.
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2343
  • View blog
  • Posts: 9,400
  • Joined: 03-December 12

Re: Can't edit rows in my table

Posted 20 March 2017 - 01:55 PM

Well, what is the exact error?
Was This Post Helpful? 0
  • +
  • -

#5 hiya1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 21-August 16

Re: Can't edit rows in my table

Posted 20 March 2017 - 02:03 PM

View Postastonecipher, on 20 March 2017 - 01:55 PM, said:

Well, what is the exact error?


Undefined index: StaffID in C:\Users\Greg\Documents\UAMP\UwAmp\www\php-project\update.php on line 42

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1