Search code examples
phpsqlrecordset

Update certain column of recordset from textbox value


room.php

enter image description here

<input name = "room" type = "text" size="70"/>    

<a href="updateroom.php?roomid=<?=$row_Recordsetroomid['roomid'] ?>">Update</a>

updateroom.php

<?php
  mysql_connect('localhost','athirahhazira','1234');
  mysql_select_db("dbcollege");
  session_start();

  $sql = "UPDATE studentsroom set room='$strroom' WHERE roomid='$_GET[roomid]'";
  mysql_query($sql) or die('Error updating room status');

  header('Location:staff/room-staff.php');
?>

i can update if there is a default value such as :

  $sql = "UPDATE studentsroom set room='A206' WHERE roomid='$_GET[roomid]'";

but not the value from a textbox. could u help me with what i am missing here?


Solution

  • try this

    $sql = "UPDATE studentsroom set room='A206' WHERE roomid='".$_REQUEST['roomid']."'";
    

    Note: your code can be sql injection. also mysql_* is deprecated use mysqli_* or PDO

    Update2:

    add a form and submit button instead of hyperlink

    <form method="post" action="updateroom.php" >
    <input name = "room" type = "text" size="70"/>    
    <input type="hidden" name="roomid" value="<?php echo $row_Recordsetroomid['roomid'];?>" />
    <input type="submit" name="submit" value="Update" />
    </form>
    

    AND update.php

    <?php
      mysql_connect('localhost','athirahhazira','1234');
      mysql_select_db("dbcollege");
      session_start();
    
      $room = $_REQUEST['room']; 
      $roomid = $_REQUEST['roomid']; 
    
      $room = mysql_real_escape_string($room);
      $roomid = mysql_real_escape_string($roomid);
    
      $sql = "UPDATE studentsroom set room='$room' WHERE roomid='$roomid'";
      mysql_query($sql) or die('Error updating room status');
    
      header('Location:staff/room-staff.php');
    ?>