php/MySQL form

Discussion in 'PHP' started by SiteExpress, Nov 16, 2006.

  1. #1
    I am sorry for being such a noob in advance. I am trying to learn all this, and although I am making good headway, I have come to a stopping point. Hopefully, some of you php/MySQL gurus can help out!!

    I am working on a backend to a site of mine. I have created a database, and managed to add data and remove data from it, using the backend I have made.

    One page displays organized data such as below.

    [​IMG]

    I would like to enable it so that I can edit data by clicking the "Edit" link. It then takes me to a form that should auto fill in fields according to the ID.

    I cannot seem to be able to get the data from the db to pass to the form, where I can then edit it.

    here is some code that I am using.

    The "Edit" link
    echo '<a href="edit-school.php?edit='.$row['id'].'">Edit</a>';
    Code (markup):
    Edit-data.php
    <form method="get" action="config/edit_data.php">
        <input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
    <table width="550" border="0" align="center" cellpadding="0" cellspacing="0" id="schoolform">
      <tr>
        <td width="150" class="formtitle">School Name:</td>
        <td class="formfield">
        <input name="name" value="<?php echo $row['name']; ?>" type="text" size="45" /></td>
      </tr>
      <tr>
        <td width="150" class="formtitle">State:</td>
        <td class="formfield">
           <select name="state" value="<?php echo $row['state']; ?>">
            <option selected="selected">Select One</option>
            <option>ALL</option>
            <option>Alabama - AL</option>
            <option>Alaska - AK</option>
            </select>      </td>
      </tr>
      <tr>
        <td width="150" class="formtitle">Logo URL:</td>
        <td class="formfield"><input name="logourl" value="<?php echo $row['logourl']; ?>" type="text" size="45" /></td>
      </tr>
        <tr>
        <td width="150" class="formtitle">Page URL:</td>
        <td class="formfield"><input name="pageurl" type="text" value="<?php echo $row['pageurl']; ?>" size="45" /></td>
      </tr>
        <tr>
        <td width="150" class="formtitle">Campus?:</td>
        <td class="formfield">
        <select name="campus" value="<?php echo $row['campus']; ?>">
          <option>Select One</option>
        </select></td>
      </tr>
         <tr>
        <td width="150" class="formtitle">Description:</td>
        <td class="formfield"><textarea name="descr" value="<?php echo $row['descr']; ?>" cols="42" rows="7"></textarea></td>
      </tr>
    </table>
    <input name="addschool" type="submit" value="Add School" />
        </form>
    Code (markup):
    edit_data.php
    <?php
    require_once('config.php');
    
    // Edit from table
    if($_GET['edit']){
    $name = $_GET['name'];
    $state = $_GET['state'];
    $logourl = $_GET['logourl'];
    $pageurl = $_GET['pageurl'];
    $campus = $_GET['campusl'];
    $descr = $_GET['descr'];
    $id = $_GET['edit'];
    
    $result = mysql_query("UPDATE schools SET name='name' WHERE id='id'") or die(mysql_error());
    }
    ?>
    Code (markup):
    Any help is appreciated!!
     
    SiteExpress, Nov 16, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Variables start with a dollar sign $, which is not present in your UPDATE query.
     
    nico_swd, Nov 16, 2006 IP
  3. SiteExpress

    SiteExpress Well-Known Member

    Messages:
    1,355
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    155
    #3
    Changed the UPDATE query to
    $result = mysql_query("UPDATE schools SET name='$name' WHERE id='$id'") or die(mysql_error());
    Code (markup):
    Still not passing the info to the page with the form.

    ANy other advice?
     
    SiteExpress, Nov 16, 2006 IP
  4. an0n

    an0n Prominent Member

    Messages:
    5,688
    Likes Received:
    915
    Best Answers:
    0
    Trophy Points:
    360
    #4
    $result = mysql_query("UPDATE schools SET name='" . $name . "' WHERE id='" . $id . "'") or die(mysql_error());


    try that'

    edit: btw
    try echoing your data to make sure you are pulling it correctly prior to updating and/or even using it anywhere.

    e.g.
    echo $name;
    echo $id;
     
    an0n, Nov 16, 2006 IP
  5. SiteExpress

    SiteExpress Well-Known Member

    Messages:
    1,355
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    155
    #5
    Thanks for the help guys, but not working yet. WHen I view source of the form page, the Hidden field for the ID is still showing a blank value. So, the ID is still not being sent to the form.
     
    SiteExpress, Nov 16, 2006 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    No reason why the above shouldn't work. The variables are between double quotes.

    Try changing your form to this.
    
    <form method="get" action="config/edit_data.php?edit=1">
    
    Code (markup):
     
    nico_swd, Nov 16, 2006 IP
    SiteExpress likes this.
  7. an0n

    an0n Prominent Member

    Messages:
    5,688
    Likes Received:
    915
    Best Answers:
    0
    Trophy Points:
    360
    #7
    Are you calling a SELECT * or are you calling specifics `id`, `title`, etc etc on your initial call?

    ok nevermind that. looks like you are just populating values based on the fetched row.
     
    an0n, Nov 16, 2006 IP
  8. SiteExpress

    SiteExpress Well-Known Member

    Messages:
    1,355
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    155
    #8
    Still didnt pass it. When i line of data from the image above for ID 3, this is the url it send me to.
    www[dot]mydomain[dot].com/edit-school.php?edit=3

    So, the correct id is being sent to the page, but the form is not pulling the data from the db.

    I know I have probably just left out something silly, but dammed if I can figure it out. LOL
     
    SiteExpress, Nov 16, 2006 IP
  9. ajscottsr

    ajscottsr Peon

    Messages:
    388
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Okay, lesson 1 in debugging... echo the MySQL query to see what it contains:

    echo ("UPDATE schools SET name='" . $name . "' WHERE id='" . $id . "'");
    // $result = mysql_query("UPDATE schools SET name='" . $name . "' WHERE id='" . $id . "'") or die(mysql_error());
    Code (markup):
    That way you can see what MySQL query is actually being sent to MySQL. That at least lets you know if its the update query or something else failing.

    Let's say you echo that query and it doesn't have the values for a few steps, back up to earlier in the program and do some echos:

    
    echo $_GET['name'] . "<BR>";
    
    $name = $_GET['name'];
    
    echo $name . "<BR>";
    
    $state = $_GET['state'];
    $logourl = $_GET['logourl'];
    $pageurl = $_GET['pageurl'];
    $campus = $_GET['campusl'];
    $descr = $_GET['descr'];
    
    echo $_GET['edit'] . "<BR>";
    
    $id = $_GET['edit'];
    
    echo $id . "<BR>";
    Code (markup):
    I see a possible culprit here. $id = $_GET['edit'] ... shouldn't that be $id = $_GET['id']?

    Debugging 101, when you get an unintended result, pick where you think the problem might be and start echoing what you have. If it all looks correct, proceed down the chain of events and echo subsequent items. If its wrong there, start moving to prior items in the chain of events.

    This will help you isolate the problem most of the time...
     
    ajscottsr, Nov 16, 2006 IP
    SiteExpress likes this.
  10. an0n

    an0n Prominent Member

    Messages:
    5,688
    Likes Received:
    915
    Best Answers:
    0
    Trophy Points:
    360
    #10
    hahah i told him the same EXACT thing :p
    but apparently it didn't resolve it

    Rob C: uh oh
    Rob C: $id = $_GET['edit'];
    Rob C: ^^shouldnt that be id
    Rob C: not edit
    Rob C: if($_GET['edit']){
    $name = $_GET['name'];
    $state = $_GET['state'];
    $logourl = $_GET['logourl'];
    $pageurl = $_GET['pageurl'];
    $campus = $_GET['campus'];
    $descr = $_GET['descr'];
    $id = $_GET['edit'];
     
    an0n, Nov 16, 2006 IP
  11. SiteExpress

    SiteExpress Well-Known Member

    Messages:
    1,355
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    155
    #11
    Thanks for the pointers. I only started learning this stuff a week ago, so I am VERY new at it.


    I can Echo edit, and get the result I want. So, the id(Which is edit) is being sent to the form page. However, no other data is being pulled.

    Nothing else gets pulled when trying an echo with it. it seems like I am connected to the db, but cannot pull any info from the db to the correct values of the form. The only reason I can get the id (or edit) variable, is because it is being sent from the other page that has the "Edit" link
     
    SiteExpress, Nov 16, 2006 IP
  12. SiteExpress

    SiteExpress Well-Known Member

    Messages:
    1,355
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    155
    #12
    Thanks for the help guys, I finally got this all worked out. I ended up having to use a Fetch_Array to get the job done. Was actually less code than I was originally trying to use.


    Thanks again!!
     
    SiteExpress, Nov 17, 2006 IP