0

hello i have a problem everytime i submitted a form . This is script i made

<?php include 'rumahtable.php';
    include 'connect.php';
    $result =  $conn -> query("select kodhomestay from rumah");
    ?><br><br>
    <form action="" method="POST">
    <label>Memadam data rumah : </label>
        <select name="rumah">
            <option>--Pilih Kod Rumah--</option>
            <?php while ($rows = $result->fetch_assoc()){
                $jrumah = $rows['kodhomestay'];
                echo "<option value='$jrumah'>$jrumah</option>";
             } ?>
        </select><br><br>
        <button type="submit" name="submit" >Delete</button>
        <button type="submit" name="update">Update</button>
    </form>
<?php if(isset($_POST["submit"])){
    include 'deletedata.php';
}
if(isset($_POST["update"])){
    ob_clean();
    $rumah = $_POST["rumah"];
    include 'updatedata.php';
}?>

and this is the deletedata.php:

<?php include 'connect.php';
$rumah=$_POST["rumah"];
$query = "delete from rumah where kodhomestay = '$rumah'"; 
$delete= mysqli_query($conn,$query);
if($delete){ ?>
    <script type="text/javascript">
            window.alert("Data Berjaya Dipadam.");
    </script>
    <?php header("Refresh:0"); 
}else{ ?>
    <script type="text/javascript">
        window.alert("Maaf data tidak dapat dipadamkan atau data tidak wujud.");
        </script>
        <?php header("Refresh:0");
}

and this is updatesenarai.php:

<?php $rumah = "RH007";
if(empty($_POST['submit'])){ ?>
    <h2>Kemaskini Data Rumah</h2>
    <?php include 'updaterumahtable.php'; ?>
    <form action="" method="POST">
        <label>Kod Rumah : </label><?php echo $rumah ?><br><br>
        <label>Nama Rumah : </label><input type="text" name="jrumah"><br><br>
        <label>Harga : </label><input type="number" name="price"><br><br>
        <input type="submit" name="submit" value="Kemaskini">
        <form>
<?php }else{
    include 'connect.php';
    $jr = $_POST['jrumah'];
    $price = $_POST['price'];
    $query = "update rumah set jenishomestay='$jr', harga=$price where kodhomestay='$rumah'";
    $update= mysqli_query($conn,$query);
    if($update){?>
        <script type="text/javascript">
            window.alert("Data Berjaya Dikemaskini.");
        </script>
        <?php header("Refresh:0");
    }else{ ?>
        <script type="text/javascript">
            window.alert("Maaf data tidak dapat dikemaskini atau data tidak wujud.");
        </script>
        <?php header("Refresh:0");
    }
    mysqli_close($conn);
}?>

everytime i click the update button. it uses the updatesenarai.php but when i want to update the data in sql. it wont update and keep saying the first window alert in delete data. But when i used only updatedata.php it works just fine. If i change the submit button name it the whole file refreshes even without the header(). I tried changing the $conn into something else but it still saying the first window alert from deletedata.php. Im still new.

1 Answers1

0

Check your updatesenarai.php. Your deletedata.php and updatesenarai.php are exactly same. Please write the code for updating in updatesenarai.php.

Mahmud hasan
  • 918
  • 10
  • 13