Jump to content

php function help


bigdog704

Recommended Posts

i cant seem to find where my problem is with this function.  Any help is greatly appreicated.

 

function update_reservation($room_id, $reg_id, $reserve_date) {
    global $db;
    $query = 'UPDATE reservations
              SET room_id = :room_id,
                  reserve_date = :reserve_date,
              WHERE reg_id =:reg_id';
    try {
        $statement = $db->prepare($query);
        $statement->bindValue(':room_id', $room_id);
        $statement->bindValue(':reserve_date', $reserve_date);
        $statement->bindValue(':reg_id', $reg_id);
        $row_count = $statement->execute();
        $statement->closeCursor();
        return $row_count;
    } catch (PDOException $e) {
        $error_message = $e->getMessage();
        display_db_error($error_message);
    }
}

<form action="." method="post" id="add_edit_reservation_form">
    <?php if (isset($reg_id)) : ?>
            <input type="hidden" name="action" value="update_reservation" />
            <label>Reservation ID:</label>
            <input type="text" name="reg_id" value="<?php echo $reg_id ?>" />
    <?php else: ?>
            <input type="hidden" name="action" value="add_reservation" />
            <label>User_ID</label>
            <input type="text" name="ms_user_id" value="<?php echo $user_info['ms_user_id']; ?>" />
    <?php endif; ?>

    <label>Which room would you like?</label>
        <select name="room_id">
            <?php foreach ($room_ids as $room_id) :
                            if ($room_id['room_id'] == $reservation['room_id']) {
                                $selected = 'selected';
                            } else {
                                $selected = '';
                            } ?>
                 <option value="<?php echo $room_id['room_id']; ?>" <?php
                      echo $selected ?>>
                <?php echo $room_id['room_name']; ?>
            </option>
            <?php endforeach; ?>
        </select><br>

        <label>What date would you like to reserve your room?</label>
        <input type="date" name="reserve_date" 
               value="<?php echo ($reservation['reserve_date']); ?>"><br>

        <label>Please submit form to reserve your room.</label>
        <input type="submit" value="Submit">
    </form>

Link to comment
Share on other sites

Pleas explain what you expect that code to do and then describe what it is doing (or not doing) that is contrary to your expectations. Are there errors?

FYI: there is nothing in that code that ever calls that function. So, kinda hard to say it isn't working, when it is not used.

Edited by Psycho
Link to comment
Share on other sites

1 hour ago, chhorn said:

At least there are PHP-tags missing.

"Unexpected end of file" errors occur when the php parser expects something but cannot find it. Usual causes are an opening { without a corresponding closing } or quots at the start of a string and then no closing quotes.

You'll have to find it yourself as it's impossible from just the couple of isolated snippets that we can see.

Edited by Barand
Link to comment
Share on other sites

Took an attempt to clean up your code.  See if this reads better for you.  Note the subtle changes I have made here to help you understand what you are doing better.

echo "<form action='' method='post' id='add_edit_reservation_form'>";
if (isset($reg_id)) 
{
    echo "<input type='hidden' name='action' value='update_reservation' />
            <label>Reservation ID:</label>
        <input type='text' name='reg_id' value='$reg_id'/>";
}
else
{
    echo "<input type='hidden' name='action' value='add_reservation'/>
        <label>User_ID</label>
        <input type='text' name='ms_user_id' value='" . $user_info['ms_user_id'] . "'>";
}
echo "<label>Which room would you like?</label>
        <select name='room_id'>";
foreach ($room_ids as $room_id)
{
    if ($room_id['room_id'] == $reservation['room_id']) 
        $selected = 'selected';
    else 
        $selected = '';
    echo "<option $selected value='" .$room_id['room_id'] . "'>" . 
            $room_id['room_name'] . 
            "</option>";
}
echo "
    </select><br>
    <label>What date would you like to reserve your room?</label>
    <input type='date' name='reserve_date' value='" . $reservation['reserve_date'] . "'>
    <br>
    <label>Please submit form to reserve your room.</label>
    <input type='submit' value='Submit'>
    </form>
    ";
//**********************************
//  Functions below
//**********************************
function update_reservation($room_id, $reg_id, $reserve_date) 
{
    global $db;
    $query = 'UPDATE reservations
                SET room_id = :room_id,
                    reserve_date = :reserve_date,
                WHERE reg_id =:reg_id';
    try 
    {
        $statement = $db->prepare($query);
        $statement->bindValue(':room_id', $room_id);
        $statement->bindValue(':reserve_date', $reserve_date);
        $statement->bindValue(':reg_id', $reg_id);
        $row_count = $statement->execute();
        $statement->closeCursor();
        return $row_count;
    } 
    catch (PDOException $e) 
    {
        $error_message = $e->getMessage();
        display_db_error($error_message);
    }
}

Now - what doesn't work?  

Edited by ginerjm
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.