www.webdeveloper.com
Results 1 to 3 of 3

Thread: Retrieve desired row from mysql table

  1. #1
    Join Date
    Oct 2011
    Posts
    32

    Question Retrieve desired row from mysql table

    Hi
    I have a PHP page to update and delete row data retrieved from a MySQL database & when i click on the update link(at the end column) beside the particular row in the browser,it should open up a form with only the particular row showing input boxes for each field and a edit button at the last column. And when I will edit the input boxes of the particular row on the browser and then click on 'submit' (here the edit button), it will fetch & show only my updated row from the database(i.e. not all the rows from the table).
    Here the table is named as `activities` table with the fields as `id`,`sports_category`,`year` and `winner`.
    But my code is not showing the desired row,instead it is retrieving all the rows from the table.
    Here is my last tried code where none of the data from the table is showing!!
    Here is the code -
    PHP Code:
    <?php
                          $c
    =1;
                          
    $sel2="select * from `activities` where `id`='$_GET[upd]' ";
                          
    $res2=mysql_query($sel2);
                          while(
    $row=mysql_fetch_array($res2))
                          {
                        if(
    $_GET['upd']=="$row[id]")
                                 {
        echo 
    "<TABLE border=0 height=200 width=400>
                    <THEAD>
                    <TR>
                    <TH>SERIAL NO.</TH>
                    <TH>SPORT NAME</TH>
                    <TH>YEAR</TH>
                    <TH>WINNER</TH>
                    <TH COLSPAN=2 ALIGN=CENTER>ACTION</TH>
                    </TR>
                    </THEAD>
                           <TBODY>
    <FORM METHOD='POST' ACTION='' name='f2'>
    <tr>
     <td class=bb >
    $c</td>
    <td class=bb ><INPUT TYPE='text' value='
    $row[sports_category]' name='spocat'></td>
    <td class=bb ><INPUT TYPE='text' value='
    $row[year]' name='uyr'></td>
    <td class=bb ><INPUT TYPE='text' value='
    $row[winner]' name='win'></td>
    <INPUT TYPE='hidden' NAME='hd' value='
    $row[id]'>
    <td colspan=2 align=center class=bb><INPUT TYPE='submit' name='ed' value='Edit'  class=bb><td>
    </tr></FORM>
        </TBODY>
                </TABLE>"

     }
          }
    if(
    $_GET['page']=="show")
     {
        echo 
    "<TABLE border=0 height=200 width=400>
            <THEAD>
                    <TR>
                    <TH>SERIAL NO.</TH>
                    <TH>SPORT NAME</TH>
                    <TH>YEAR</TH>
                    <TH>WINNER</TH>
                    </TR>
                    </THEAD>
        <TBODY>"
    ;
                    
    $u=$_GET['ed'];
            
    $sel3="select * from `kris`.`activities` where id='$h' ";
            
    $res3=mysql_query($sel3);
                       while(
    $row3=mysql_fetch_array($res3))
                        {
                         echo 
    "<tr>
                        <td class=bb>
    $row3[id]</td>
                        <td class=bb>
    $row3[sports_category]</td>
                            <td class=bb>
    $row3[year]</td>
                        <td class=bb>
    $row3[winner]</td>
                                </tr>"
    ;
                                                    
    $c++;    
                            }
                        } 
                        
    ?>
    In the abode code I have used a 'upd' variable which is being passed through a update link in another page.These part of the code starting from
    PHP Code:
    if($_GET['upd']=="$row[id]"
    to
    PHP Code:
    <td colspan=2 align=center class=bb><INPUT TYPE='submit' name='ed' value='Edit'  class=bb><td>
    </
    tr></FORM>
        </
    TBODY>
                </
    TABLE>"; 
    is working fine. But then when I click on the "Edit" button ,the value is getting updated in the database but the desired row is not showing on the browser!!
    Please tell me what tweak in the MySQL code is to be done in the $_GET[show] part of the above code??
    And here is the php code for the edit part-(it should be at the start before the above code)
    PHP Code:
    if($_POST['ed']=="Edit")
      {
          
    $spt=$_POST['spocat'];
          
    $yar=$_POST['uyr'];
          
    $wnr=$_POST['win'];
          
    $h=$_POST['hd'];
         
    $a="UPDATE `k`.`activities` SET `sports_category` = '$spt', `year` = '$yar', `winner` = '$wnr' WHERE `activities`.`id` ='$h' ";
         
    mysql_query($a);
         
    header("location:upd_a.php?page=show&msg=updated successfully");
      } 
    Will be greatly obliged for any genuine help.
    Last edited by jitc; 07-13-2012 at 12:49 PM.

  2. #2
    Join Date
    Oct 2011
    Posts
    32

    Unhappy

    I am heartbroken.No one is replying
    Plzz somebody help me.

  3. #3
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    Would really like to see more code before I try answering, but what you want to achieve is pretty simple. I'll only going to sample it, I'm not going to try giving precise code here without seeing more of yours:

    PHP Code:
    // Check the $_GET is set AND has a value.
    if(isset($_GET['some_value']) && $_GET['some_value'])
    {
        
    $connect mysql_connect("localhost""username""password");
        
    $connected mysql_select_db("some_database"$connect);

        if(
    $connected or die(mysql_error()))
        {
            
    // Set ID to local var.
            
    $your_id $_GET['some_value'];
     
            
    // Fetch the single row that matches the given ID.
            
    $fetch_row mysql_query("SELECT * FROM some_table WHERE id = '$your_id'");

            
    // Store results in associative array.
            
    $result[0] = mysql_fetch_assoc($fetch_row);


            echo 
    "
                  <table>
                      <tr>
                          <td>SERIAL NO.</td>
                          <td>SPORT NAME</td>
                          <td>YEAR</td>
                          <td>WINNER</td>
                      </tr>

                     <tr> 
                         <td>" 
    $results[0]['id'] . "</td>
                         <td>" 
    $results[0]['sports_category'] . "</td>
                         <td>" 
    $results[0]['year'] . "</td>
                         <td>" 
    $results[0]['winner'] . "</td>
                     </tr>
                  </table>
                   
                    "
    ;
          
        }

    I can't test this code without my own database, so I'm sorry if it has bugs. This is really the easiest way to print out a single row. There's absolutely no need for any kind of loop since you've got no need to iterate through the records.

    Post if you need more.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles