Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts

    Retrieving and Updating Records in Tables

    I have a script called addtitle located at kansasoutlawwrestling.com/addtitle.com and I have it inserting the information into the table but I have another script called edittitle which is located at kansasoutlawwrestling.com/edittitle which is the same script but what happens with edit title is that it will retrieve the values from the table and you can edit the fields and then you'll click a submit button to save changes to the title but don't know how to do that. Because in this book I own it talks about retrieving and updating and don't know how I need to do that.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Are you referring to SQL mechanisms? If so, this is what you do.
    You have a record you want to edit. The page that redirects you to the edit page would contain a link that has the primary (or candidate) key identifier as a part of the querystring, so that edittitle.php receives id=4 for example. Fetching a records is a simple select statement where the primary key (or candidate) is equal to the provided value. With this you construct a form passing the key value in as a hidden field. If the hidden field exists, run an update command, otherwise an insert. This is kinda hard to explain, so let me try to demonstrate it with some simple code:
    listtitles.php <-- Simply lists all of the available titles
    PHP Code:
    <?php
    include 'yourconnection.php';
    $aTitles = array();
    $sTitlesListSQL "SELECT * FROM `titlesTable`";
    $sTitlesList mysql_query($sTitlesListSQL) || die (mysql_error());
    if (
    mysql_num_rows($sTitlesList) <= 0)
    {
        die (
    'No records to display.');
    }
    while (
    $row mysql_fetch_assoc($sTitlesList))
    {
        
    printf("<a href=\"modifytitle.php?id=%d\">%s</a>\n", (int)$row['id'], $row['title']);
    }
    ?>
    Modifytitle.php
    PHP Code:
    <?php

    if (isset($_POST['submit']))
    {
        
    $sUpdateSQL '';
        if (isset(
    $_POST['iID']) && !empty($_POST['iID'])
        {
            
    // Update
            
    $sUpdateSQL "UPDATE `titleTable` SET `title` = '" mysql_real_escape_string($_POST['txtTitle']) . "' WHERE `id` = " . (int)$_POST['iID'];
        }
        else
        {
            
    $sUpdateSQL "INSERT INTO `titleTable` (`title`) VALUES ('" mysql_real_escape_string($_POST['txtTitle']) . "')";
        }
        
    mysql_query($sUpdateSQL) || die(sprintf("Cannot update database (%d): %s"mysql_errno(), mysql_error()));
    }

    $sTitle '';
    $iID null;
    $sIDField '';

    if (isset(
    $_GET['id']) && !empty($_GET['id']))
    {
        
    $sGetTitle "SELECT * FROM `titlesTable` WHERE `id`=" . (int)$_GET['id'];
        if (
    mysql_num_rows($sGetTitle) == 1)
        {
            
    $row mysql_fetch_assoc($sGetTitle);
            
    $sTitle $row['title'];
            
    $iID = (int)$row['id'];
            
    $sIDField '<input type="hidden" name="iID" value="' $iID " />
        }
    }

    $form = <<<FORM
    <form action="
    modifytitle.php" method="post">
        <fieldset>
            $sIDField
            <input type="
    text" name="txtTitle" value="$sTitle" />
        </fieldset>
        <div>
            <input type="
    submit" name="submit" value="Update" />
            <input type="
    reset" name="reset" value="Reset" />
        </div>
    </form>
    FORM;

    ?>
    No idea if that actually works, but it should give you an idea. It constructs a self posting form that if the hidden iID field exists it tries to update record, and if it doesn't it simply inserts it.

    Hope that answers what you are looking for.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    CoolAsCarlito (07-30-2008)

  • #3
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    Well what I think I want to happen is have when you click on the link a link called Edit title it will bring up the edittitle and edittitle will have a drop down select field that when you select the title you want to edit it will post that titles respected data into the form fields below. Hope that makes sense
    Last edited by CoolAsCarlito; 07-30-2008 at 09:11 PM.

  • #4
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    But right now I'm getting this error:

    Parse error: parse error, unexpected $ in /home/content/y/a/n/yankeefaninkc/html/edittitle.php on line 105

    PHP Code:
    <?php
    // Connects to your Database
    $link mysql_connect("?""?""?") or die(mysql_error());
    mysql_select_db("?",$link) or die(mysql_error());
     
    if (!
    mysql_select_db("?"$link)) {
    echo 
    'Could not select database';
    exit;
    }
     
    //This code runs if the form has been submitted
    if (isset($_POST['edittitle'])) {
     
    //This makes sure they did not leave any fields blank
    if (!$_POST['titlename'] || !$_POST['type'] || !$_POST['status'] || !$_POST['champion'] || !$_POST['contender1'] || !$_POST['contender2'] || !$_POST['contender3'] ) {
    die(
    'You did not complete all of the required fields');
    }

    // This is where we call all the values from the fields
    $query 'SELECT * FROM titles';
    if (
    $r mysql_query ($query)) { //Run the query.


    // now we insert it into the database
    $insert "INSERT INTO titles (titlename, type, status, champion, contender1, contender2, contender3)
    VALUES ('"
    .$_POST['titlename']."','".$_POST['type']."','".$_POST['status']."','".$_POST['champion']."','".$_POST['contender1']."','".$_POST['contender2']."','".$_POST['contender3']."')";
    $add_title mysql_query($insert,$link) or die(mysql_error());
    ?>
     

    <h1>Title is been changed!</h1>

    <?php
    }
    else
    {
    ?>
    <center>
    <table border=1 cellpadding=5 cellspacing=0 width=350>
    <tr><td>Contender #1:</td><td>
    <?php
    echo '<select>';
    $data mysql_query("SELECT wrestlingname FROM singles");
    while(
    $row mysql_fetch_assoc($data)) {
        echo 
    '<option value="'.$row['wrestlingname'].'">'.$row['wrestlingname'].'</option>';
    }
    echo 
    '</select>'
    ?>
    </td></tr></table>
    <center><table border=1 cellpadding=5 cellspacing=0 width=350>
    <font color="#CC0000"><h2><center>Create a Title</h2></center></font>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <tr><td>Title Name:</td><td>
    <input type="text" name="titlename" maxlength="60">
    </td></tr>
    <tr><td>Type:</td><td>
    <select name="type">
    <option>Singles</option><option>Tag Team</option><option>Stable</option></select>
    </td></tr>
    <tr><td>Status:</td><td>
    <select name="status">
    <option>Active</option><option>Inactive</option></select>
    </td></tr>
    <tr><td>Champion:</td><td>
    <?php
    echo '<select>';
    $data mysql_query("SELECT wrestlingname FROM singles");
    while(
    $row mysql_fetch_assoc($data)) {
        echo 
    '<option value="'.$row['wrestlingname'].'">'.$row['wrestlingname'].'</option>';
    }
    echo 
    '</select>'
    ?>
    </td></tr>
    <tr><td>Contender #1:</td><td>
    <?php
    echo '<select>';
    $data mysql_query("SELECT wrestlingname FROM singles");
    while(
    $row mysql_fetch_assoc($data)) {
        echo 
    '<option value="'.$row['wrestlingname'].'">'.$row['wrestlingname'].'</option>';
    }
    echo 
    '</select>'
    ?></td></tr>
    <tr><td>Contender #2:</td><td>
    <?php
    echo '<select>';
    $data mysql_query("SELECT wrestlingname FROM singles");
    while(
    $row mysql_fetch_assoc($data)) {
        echo 
    '<option value="'.$row['wrestlingname'].'">'.$row['wrestlingname'].'</option>';
    }
    echo 
    '</select>'
    ?>
    <tr><td>Contender #3:</td><td>
    <?php
    echo '<select>';
    $data mysql_query("SELECT wrestlingname FROM singles");
    while(
    $row mysql_fetch_assoc($data)) {
        echo 
    '<option value="'.$row['wrestlingname'].'">'.$row['wrestlingname'].'</option>';
    }
    echo 
    '</select>'
    ?>
    <tr><th colspan=2><input type="submit" name="createtitle" value="Create Title"></th></tr></table>
    </form>
    <?php
    ?>

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You have an open } from an else clause. Thats not the error I would expect for it to trigger, but that does appear to be the cause, I would expect something more along the lines of an unexpected end of file, not an unexpected $.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    CoolAsCarlito (07-31-2008)

  • #6
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    I'm confused by that. Are you not able to pinpoint where my error is?

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You're error is right here:
    PHP Code:
    <h1>Title is been changed!</h1>

    <?php
    }
    else
    {
    ?>
    There is no close for this else statement. It likely belongs at the bottom of the page.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    CoolAsCarlito (07-31-2008)

  • #8
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    Now I'm getting this:

    Parse error: parse error, unexpected $ in /home/content/y/a/n/yankeefaninkc/html/edittitle.php on line 101

  • #9
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    Okay so I deleted all but the form and restarted putting in the database and all the rest of the actual important php code and then I started getting errors. Anyone see the problem in my code:

    PHP Code:
    <?php
    // Connects to your Database
    $link mysql_connect("?""?""?") or die(mysql_error());
    mysql_select_db("?",$link) or die(mysql_error());
     
    if (!
    mysql_select_db("?"$link)) {
    echo 
    'Could not select database';
    exit;
    }
     
    //This code runs if the form has been submitted
    if (isset($_POST['edittitle'])) {
     
    //This makes sure they did not leave any fields blank
    if (!$_POST['titlename'] || !$_POST['type'] || !$_POST['status'] || !$_POST['champion'] || !$_POST['contender1'] || !$_POST['contender2'] || !$_POST['contender3'] ) {
    die(
    'You did not complete all of the required fields');
    }

    // This is where we call all the values from the fields
    $query 'SELECT * FROM titles';
    if (
    $r mysql_query ($query)) { //Run the query.


    // now we insert it into the database
    $insert "INSERT INTO titles (titlename, type, status, champion, contender1, contender2, contender3)
    VALUES ('"
    .$_POST['titlename']."','".$_POST['type']."','".$_POST['status']."','".$_POST['champion']."','".$_POST['contender1']."','".$_POST['contender2']."','".$_POST['contender3']."')";
    $add_title mysql_query($insert,$link) or die(mysql_error());
    ?>
     

    <h1>Title is been changed!</h1>

  • #10
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    Here's what I have now:

    http://kansasoutlawwrestling.com/edittitle.php

    So now I just have to have it paste the respective info into the form fields when you select a title.


  •  

    Posting Permissions

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