PHP form to edit MYSQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PHPjunkie
Forum Newbie
Posts: 2
Joined: Sat Mar 14, 2009 5:19 pm

PHP form to edit MYSQL

Post by PHPjunkie »

I've recently began trying to edit MYSQL from PHP. Now I think I have got half way and was wondering if someone could give me the directions to the finish line.

Currently this code lets me import the chosen mysql row in these text boxes.

I assumed from there it would just be a mater of declaring variables and then using the SET command to update. However While this application says the rows are updating, in relality there staying the same. I could swear the SQL code is fine, so I'm not sure whats wrong here.

Code: Select all

 
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $bookingID = $_GET["bookingID"];
      $sql = "SELECT * FROM booking WHERE bookingID=$bookingID";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
      
      <form action="edit.php" method="post">
      <input type=hidden name="bookingID" value="<?php echo $myrow["bookingID"] ?>">
   
      Room Name:<INPUT TYPE="TEXT" NAME="roomName" VALUE="<?php echo $myrow["roomName"] ?>" SIZE=30><br>
      Date:<INPUT TYPE="TEXT" NAME="lunch" VALUE="<?php echo $myrow["bookingDate"] ?>" SIZE=30><br>
      Reason:<TEXTAREA NAME="reason" ROWS=10 COLS=30><?php echo $myrow["reason"] ?></TEXTAREA><br>
      Start Time:<INPUT TYPE="TEXT" NAME="lunch" VALUE="<?php echo $myrow["startTime"] ?>" SIZE=5><br>
      Finish Time:<INPUT TYPE="TEXT" NAME="lunch" VALUE="<?php echo $myrow["finishTime"] ?>" SIZE=5><br>
      Lunch:<INPUT TYPE="TEXT" NAME="lunch" VALUE="<?php echo $myrow["lunch"] ?>" SIZE=3><br>
      Tea:<INPUT TYPE="TEXT" NAME="lunch" VALUE="<?php echo $myrow["tea"] ?>" SIZE=3><br>
      Room Type:<INPUT TYPE="TEXT" NAME="lunch" VALUE="<?php echo $myrow["roomType"] ?>" SIZE=40><br>
      Campus:<INPUT TYPE="TEXT" NAME="lunch" VALUE="<?php echo $myrow["campus"] ?>" SIZE=11><br>
      Attending:<INPUT TYPE="TEXT" NAME="lunch" VALUE="<?php echo $myrow["attending"] ?>" SIZE=3><br>
     
      
      
      <input type="hidden" name="cmd" value="edit">
      <input type="submit" name="submit" value="submit">
   
      </form>
   
   <? } ?>
<?
   if ($_POST["submit"])
   {
      $bookingID = $_POST["bookingID"];
      $roomName = $_POST["roomName"];
      $bookingDate = $_POST["bookingDate"];
      $reason = $_POST["reason"];
      $startTime = $_POST["startTime"];
      $finishTime = $_POST["finishTime"];
      $lunch = $_POST["lunch"];
      $tea = $_POST["tea"];
      $roomType = $_POST["roomType"];
      $campus = $_POST["campus"];
      $attending = $_POST["attending"];
      
     
      $sql = "UPDATE booking SET roomName='$roomName' ,bookingDate='$bookingDate' ,reason='$reason'"
      . " ,startTime='$startTime' ,finishTime='$finishTime' ,lunch='$lunch' ,tea='$tea' ,roomType='$roomType'" 
      .",campus='$campus' ,attending='$attending' WHERE bookingID=$bookingID";
 
      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
   }
}
?>
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: PHP form to edit MYSQL

Post by socket1 »

After the $sql variable (the second one at the bottom) put echo $sql; and then copy the output and paste it into the command line of your mysql client (I use Navicat) and see if it did the job it was supposed to.
Could it be caused by the . between these two things ?

roomType='$roomType'" . ",campus='$campus'
reason='$reason'" . " ,startTime='$startTime'

Also read this: viewtopic.php?f=2&t=17096

And this line

Code: Select all

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
Can be replaced by

Code: Select all

if($_REQUEST["cmd"]=="edit")
PHPjunkie
Forum Newbie
Posts: 2
Joined: Sat Mar 14, 2009 5:19 pm

Re: PHP form to edit MYSQL

Post by PHPjunkie »

socket1 wrote:After the $sql variable (the second one at the bottom) put echo $sql; and then copy the output and paste it into the command line of your mysql client (I use Navicat) and see if it did the job it was supposed to.
Could it be caused by the . between these two things ?

roomType='$roomType'" . ",campus='$campus'
reason='$reason'" . " ,startTime='$startTime'
It looks like it was caused by it losing the value of bookingID Consequently it could not update the rows. I manged to fix this by adding a
<INPUT TYPE="HIDDEN" NAME="bookingID" VALUE="<?php echo $myrow["bookingID"] ?>" SIZE=8>

It's working fine now, which is great, also cheers for this.
socket1 wrote: And this line

Code: Select all

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
Can be replaced by

Code: Select all

if($_REQUEST["cmd"]=="edit")
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: PHP form to edit MYSQL

Post by Bill H »

Just for what it's worth, a "hidden" input doesn't need a "size" attribute.
Post Reply