Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i select an item in search.php and try to update the selected record to update.php, dropdown item cant select the right value of that particular record that i want to update.

my Db,
----------------------
tbldepartment
----------------------
deptcode varchar(50) PK
deptname
----------------------

----------------------
tblcourse
----------------------
crscode varchar(50) PK
crsname varchar(50)
duration varchar(50
_deptcode varchar(50) FK
----------------------

What I have tried:

PHP
?php 
    //get and pass value from 'searchForm.php'
    $id = ''; 
    if(isset( $_GET['id'])) {
        $id = $_GET['id']; 
    }

    //retrieve  dbrecord to the form    
    try{    
        //include db connection
        include "../php/dbconfig.php";
        // Prepare, bind and execute
        $sql = "SELECT crscode,crsname,duration,_deptcode FROM tblcourse WHERE crscode = ?";
        $stmt = $conn -> prepare ($sql);
        $stmt->bind_param("s", $id); 
        $stmt->execute(); 
        $stmt->bind_result($crscode, $crsname,$duration,$_deptcode);
        // Echo
        while ($stmt->fetch()) {
            echo "<pre>";
            echo "Deptcode: $crscode\n";
            echo "Deptname: $crsname\n";
            echo "Deptcode: $duration\n";
            echo "Deptname: $_deptcode\n";

        }
        // Close
        $stmt -> close();
        $conn -> close();    

    }catch(Exception $e){
        //echo "<p>Caught exception: " . $e->getMessage() . "</p>";
    }   

    // Update Function
    function update(){
        $ID = $GLOBALS['id']; // Store the value of varialble $id
        // include db connection
        include "../php/dbconfig.php";
        // Prepare and bind
        $sql = "UPDATE tblcourse SET crsname = ?, duration = ?, _deptcode = ? WHERE crscode = ?";
        $stmt = $conn -> prepare($sql);
        $stmt -> bind_param("ssss",$crsname,$duration,$_deptcode,$ID);
        // Set parameters
        $crsname = trim($_POST['txtcrsname']);
        $duration = trim($_POST['txtduration']);
        $_deptcode = trim($_POST['cbodept']);
        // Execute
        if($stmt -> execute()){
            echo "  
                        window.alert('Record Updated!');
                        window.location='seaCrs.php'; 

                "; 
        }else{
            echo "ERROR: Could not execute query:" . mysqli_error($conn);
        }
        // close 
        $stmt -> close();
        $conn -> close();
    }

    $mssg = "";
    include '../php/functions.php';
    //button update
    if(isset($_POST['btnupdate'])){
        if(empty($_POST['txtcrscode']) || empty($_POST['txtcrsname']) || empty($_POST['txtduration']) || empty($_POST['cbodept'])){
            $GLOBALS['mssg'] = "Please fill up all fields!";
        }else{
            update();   //calling update function
        }
    }

    //btn cancel
    if (isset($_POST['btncancel'])){
        redirect("seaCrs.php");
        echo " window.location='searchDept.php';  ";
    }
?>

<pre>
<title> Update Course 

    .form-con{
        background-color: ;
        width: 100%;
        padding: 10px;
        text-align: left;
    }
    .txtbtn{
        width: 100%;
    }

<br><br><br><br>
<div class="form-con">
    <h2 class="admin-headertitle">UPDATE COURSE</h2>        

        <h3>Course Code:</h3>

        <h3>Course Name:</h3>

        <h3>Duration:</h3>

        <h3>Department: </h3>

             prepare($sql);
                // Execute and fetch
                $stmt -> execute();
                $result = $stmt -> get_result();
                while($row = $result -> fetch_assoc()){
                    //echo "" .$row['deptcode'] ."";
                }
                // Close
                $stmt -> close();
                $conn -> close();*/

            ?> 
             prepare($sql);
                // Execute and fetch
                $stmt -> execute();
                $result = $stmt -> get_result();
                while($row = $result -> fetch_assoc())  { ?>
                 >

        <br><br>

        <br><br>

        <p style="color: rgba(255, 0, 0, 1); font-style: italic"> </p>      

</div>
Posted
Updated 3-Aug-23 4:05am
v4
Comments
Member 15627495 1-Aug-23 3:21am    
hello !

when you submit your choice, you're querying '#.php" , you may prefer "update.php" as 'action' attribute
akosisugar 3-Aug-23 9:32am    
plss help. i already update my post..
Andre Oosthuizen 1-Aug-23 4:52am    
Where is the code for update.php, we cannot see what you are trying to do with what you supplied here as the code and your question/explanation does not make sense at all.
akosisugar 3-Aug-23 9:32am    
plss help. i already update my post..
Andre Oosthuizen 3-Aug-23 10:13am    
With the amount of questions you have asked to date you should know by now how to post a proper question with the proper tags etc, I have cleaned up the post for you, please do not post like that again.

As far as your code is concerned -
1. I do not see the html for your form and the select element
2. Your above code is incomplete, it will never run, you need to add error checks to see where your code is incorrect and then post the line of code and the error
3. Use the following code at the top of your page which will catch any and all errors -
error_reporting(E_ALL);
ini_set('display_errors', 1);

4. Once you see the errors, post them here and we can then assist.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900