SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Assistance with Updating a single record of a list

    My Code so far:
    PHP Code:
    <body>
    <?php
    $con 
    mysql_connect("localhost","root","");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("test2"$con);

    $result mysql_query("SELECT * FROM cars WHERE color='' ORDER BY id DESC");
    $rownumbers=0;

    while (
    $row mysql_fetch_array($result))
    {
        
    $rownumbers++;
    echo 
    "(".$rownumbers.")<br />";
    echo 
    "<div><font color='green'>Record# -  </font><a href='../editrecord.php' id='" $row['id'] . "' />".$row['id']."</a>:  <br />Make is: " $row['make'] . "<br />  Model Type: ".$row['model']."</div>";
    echo 
    "<div><font color='red'>What color is it: </font>" $row['color'] . "</div>";

    echo 
    "<div><hr></hr></div>";

    mysql_close($con);?>
    </body>
    </html>
    My goal is to be able to click on the Record # which i have as a href and that it will update the record thereby removing it from the list and populate another page where I can edit the record completly. Im not sure about how to go about doing that though. Anyone out there with some ideas and an example or two?

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    that it will update the record thereby removing it from the list and populate another page where I can edit the record completly
    Can you re-explain this part?

  3. #3
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    (1)

    Record# - 7:
    Make is: plymouth
    Model Type: SUV
    What color is it:

    edit this record? {yes} This is a checkbox

    --------------------------------------------------------------------------------
    (2)

    Record# - 5:
    Make is: chevy
    Model Type: truck
    What color is it:

    edit this record? {yes}

    --------------------------------------------------------------------------------
    (3)

    Record# - 4:
    Make is: ford
    Model Type: car
    What color is it:

    edit this record? {yes}

    --------------------------------------------------------------------------------
    (4)

    Record# - 3:
    Make is:
    Model Type:
    What color is it:

    edit this record? {yes}

    --------------------------------------------------------------------------------
    (5)

    Record# - 2:
    Make is: dodge
    Model Type: truck
    What color is it:

    edit this record? {yes}

    --------------------------------------------------------------------------------
    [SUBMIT BUTTON]



    The above list is the output of the code from the opening post.
    The code generates a list from the table where the field for color is empty.

    So what i am trying to do is to select a record from the list, pass the data from the selected record to a new page that will allow me to update the record.
    When a record has a color then it does not get included in the list.
    I am not sure how to go about doing that.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Your form:
    Code:
    <form action=update.php method=post>
    
    
    // pseudocode ish
    foreach vehicle with no color{
    
    <p>Make is: plymouth<br />
    Model Type: SUV<br />
    What color is it? <input type=text name="7" value=""></p>
    
    }
    
    // close off your single form
    <input type=submit>
    </form>
    Your postback form handler

    update.php

    PHP Code:
    <?php

    // check what is passed back to this handler
    var_dump($_POST);

    unset(
    $_POST['submit']);  // get rid of submit button

    var_dump($_POST); // check that worked, is submit gone?

    // now update your db

    foreach($_POST as $k=>$val){

    // escape the string to avoid sql injection, and turn a numerical id into an integer (or set to 0 on fail)

    $sql "update cars set color = '"mysql_real_escape_string($_POST[$k]). "' where id=" . (int)$k;

    // send query to dbase here

    }
    Not tested, but in theory this is one way you could do it.

    It is possible to update all cars with one query but that should get you going.


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
  •