SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member sponna's Avatar
    Join Date
    May 2003
    Location
    Derby UK
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    First attempt at CMS - help appreciated....

    Hi,

    I'm trying to improve my limited understanding of php/mysql particularly as we are now undertaking oscomm projects (it helps!)

    My wife is designing a villa promotion site at the moment so as a learning exercise I decided to try and build a simple CMS whereby the client can update a calendar showing "free" versus "booked" days for his properties.

    I started out with a simple table in mysql with 3 columns (ID, day, status) representing the available days for a month. The idea is that the punter can change the status from "free" to "booked" using a form submission.

    The table creation went fine and I managed a simple php file to pull data from the table (days and status) and insert it into a table.

    Next bit I'm finding trickier, i.e. the php file to allow changes to the "status" column for particular days as defined in the form fields.

    I have this (don't laugh!)

    <html>
    <head>
    <title> Status Admin </title>
    </head>
    <body>
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    <input type="checkbox" name="checkbox1" value="booked">
    <input type="checkbox" name="checkbox2" value="booked">
    <input type="checkbox" name="checkbox3" value="booked">
    <input type="checkbox" name="checkbox4" value="booked">
    <input type="checkbox" name="checkbox5" value="booked">
    <input type="checkbox" name="checkbox6" value="booked">
    <input type="checkbox" name="checkbox7" value="booked">
    <input type="checkbox" name="checkbox8" value="booked">
    <input type="checkbox" name="checkbox9" value="booked">
    <input type="checkbox" name="checkbox10" value="booked">
    <input type="checkbox" name="checkbox11" value="booked">
    <input type="checkbox" name="checkbox12" value="booked">
    <input type="checkbox" name="checkbox13" value="booked">
    <input type="checkbox" name="checkbox14" value="booked">
    <input type="checkbox" name="checkbox15" value="booked">
    <input type="checkbox" name="checkbox16" value="booked">
    <input type="checkbox" name="checkbox17" value="booked">
    <input type="checkbox" name="checkbox18" value="booked">
    <input type="checkbox" name="checkbox19" value="booked">
    <input type="checkbox" name="checkbox20" value="booked">
    <input type="checkbox" name="checkbox21" value="booked">
    <input type="checkbox" name="checkbox22" value="booked">
    <input type="checkbox" name="checkbox23" value="booked">
    <input type="checkbox" name="checkbox24" value="booked">
    <input type="checkbox" name="checkbox25" value="booked">
    <input type="checkbox" name="checkbox26" value="booked">
    <input type="checkbox" name="checkbox27" value="booked">
    <input type="checkbox" name="checkbox28" value="booked">
    <input type="checkbox" name="checkbox29" value="booked">
    <input type="checkbox" name="checkbox30" value="booked">
    <input type="checkbox" name="checkbox31" value="booked">
    <input type="submit" name="Submit" value="submit">
    </form>
    <?php
    // Connect to the database server
    $dbcnx = @mysql_connect('localhost', 'root', 'smeg');
    if (!$dbcnx) {
    die( '<p>Unable to connect to the ' .
    'database server at this time.</p>' );
    }

    // Select the calendar database
    if (! @mysql_select_db('calendar') ) {
    die( '<p>Unable to locate the calendar ' .
    'database at this time.</p>' );
    }?>
    <?php $checkbox = $_POST['checkbox1'];
    $sql = "UPDATE months SET
    Status='$checkbox'
    WHERE ID=1";
    if (@mysql_query($sql)) {
    echo('<p>Status has been updated.</p>');
    } else {
    echo('<p>Error updating status: ' .
    mysql_error() . '</p>');
    }
    ?>
    </body>
    </html>

    I can see why this is only letting me update one row represented by "checkbox1" but I'm damned if I can work out how to update selected fields.

    This is probably a poor way of attempting it but hey - I have to start somewhere!

    Advice for a php "noob" welcomed please.

    cheers
    sponna

  2. #2
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    if i understood, you want to allow someone to check one or more checkboxes, which will update the appropiate days in the database (ie. set the status to booked).

    First of all, you have to change your html, like this

    Code:
    <input type="checkbox" name="status[]" value="1">
    <input type="checkbox" name="status[]" value="2">
    <input type="checkbox" name="status[]" value="3">
    etc...
    Than you do this in your php code

    PHP Code:
    //connnect to the database
    //select the database


    $status = isset($_POST['status']) ? $_POST['status'] : array();

    foreach (
    $status as $value)
    {
        
    $sql "UPDATE months SET Status='booked' WHERE ID=$value"
        
    //do the query etc...

    What this does is put all the checked checkboxes in a value, and loop through the array.

    I hope this helps to get you started, as i am pretty sure this is not the final version (ie. you want to have the selected ones checked beforehand, you want to change the status to free etc..).

  3. #3
    SitePoint Member sponna's Avatar
    Join Date
    May 2003
    Location
    Derby UK
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks datune!

    I'll give that a go. You are right in spotting that I will need more funtionality later and no doubt I'll be back for more advice.

    Really appreciate your help....

    thanks
    sponna


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •