Current location: Hot Scripts Forums » Programming Languages » PHP » can't edit multiple rows at the same time


can't edit multiple rows at the same time

Reply
  #1 (permalink)  
Old 03-30-04, 05:19 PM
conundrum conundrum is offline
New Member
 
Join Date: Mar 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
can't edit multiple rows at the same time

Hello

The script that i've written displays all the data in rows with input fields and check boxes for each row.
I can tick a box or more than to boxes and press "delete" button and it will delete the selected rows.
My problem is that i also want to be able to edit any input field(s) in a row or multiple rows and press "edit" button to update the database. At the moment thought will only let me edit one row when it is the only row in the database.
If i have more than one row,than it will only let me edit the bottom row.

So, i want to be able to edit any row and as many(multiple) rows as i wish at a time by checking (it)them with a chechbox. I'm really struggeling with this and will apraciate if someone helps me. I've posted the code below.
PHP Code:

<?php

    $connection 
mysql_connect("localhost""root""");
    
    
mysql_select_db("whitg22");

    function 
deletenames()
    {
        
$result mysql_query("SELECT * FROM hits");

        if ((
$row mysql_fetch_array($result)) != 0)
        {    
            echo 
"<table border=0>";

            echo 
"<tr bgcolor=#cccccc>
                <td align=center>Title</td>
                <td align=center>Artist</td>
                <td align=center>Year</td>
                <td align=center>Genreid</td>
                <td align=center>Chart Position</td>
                <td align=center>Type</td>
                <td align=center>Price</td>
                <td align=center>Quantity</td>
                <td align=center>Choose a Hit</td>
                </tr>"
;
            
            do
            {            
                echo 
"<form method='post' action='change.php'>
                    <tr>
                    <td align='center'>
                    <input type='text' name='title' value='" 
$row['title'] . "' size='30' maxlength='30'></td>
                    <td align='center'>
                    <input type='text' name='artist' value='" 
$row['artist'] . "'></td>
                    <td align='center'>
                    <input type='text' name='year' value='" 
$row['year'] . "' size='4' maxlength='4'></td>
                    <td align='center'>
                    <input type='text' name='genreid' value='" 
$row['genreid'] . "' size='5' maxlength='5'></td>
                    <td align='center'>
                    <input type='text' name='chartposition' value='" 
$row['chartposition'] . "' size='10'         

                                                    maxlength='10'></td>
                    <td align='center'>
                    <input type='text' name='type' value='" 
$row['type'] . "' size='5' maxlength='5'></td>
                    <td align='center'>
                    <input type='text' name='price' value='" 
$row['price'] . "' size='4' maxlength='4'></td>
                    <td align='center'>
                    <input type='text' name='quantity' value='" 
$row['quantity'] . "' size='5' maxlength='5'></td>
                    <td align='center'>
                    <input type='checkbox' name='id[]' value='" 
$row['id'] . "'></td></tr>";
            } 
            while (
$row mysql_fetch_array($result));

              
            echo 
"<tr><td><input type='submit' name='delete' value='Delete'>";
            
            echo 
"<input type='submit' name='edit' value='Edit'></td></tr></table></form>";    
        }
        else        
        {    
        echo 
"No records found!";
        }
        
    }

    
deletenames();

    if(isset(
$delete))
    {
        foreach(
$_POST['id'] as $id)
        {
            
mysql_query("DELETE FROM hits WHERE id = '$id'");
        }
    }

    if(isset(
$edit))
    {
    
mysql_query("UPDATE hits SET 
            title = '
$title', artist = '$artist', year = '$year', genreid = '$genreid', 
            chartposition = '
$chartposition', type = '$type', price = '$price', quantity = '$quantity'
             WHERE    
            id = '
$id'");
    }
    
mysql_close($connection);
?>
Reply With Quote
  #2 (permalink)  
Old 03-31-04, 06:08 PM
simone's Avatar
simone simone is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
i also need some help with this ... anyone?
Reply With Quote
  #3 (permalink)  
Old 04-01-04, 12:50 AM
simone's Avatar
simone simone is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
I worked it out

With your actual input form the "name=" will have to be something like.. newchartpos_$i ..different for each input thing of course, aslong as it has
PHP Code:

_$i 

. Then also at the end add:

<form type="hidden" name="id_$i" value="$id">

Where $id being the value name you have associated with the id's of your rows.

Now for the php.

PHP Code:

if(isset($edit)) {

            
$query mysql_query("SELECT * FROM hits");
            
$i=1;
         while(
$hits $db->fetch_array($query)) {
            
$theid "id_{$i}";
             
$theid = $$theid;
              if(
$theid $hits[id]){
            
$charpos2 "newchartpos_{$i}";
            
$charpos2 = $$charpos2;
            if(
$charpos2 != $hits[chartposition]){
                
mysql_query("UPDATE hits SET chartposition='$charpos2' WHERE chartposition='$hits[chartposition]' AND id='$theid'");
            }
            
        }    
        
$i++;
    }
    echo 
"Whatever you want here, this will show after the form is submitted.";
 } 
Okay thats just the basic ..you'll need to add more to suit yours.

Hopefully it'll work for you, i created it for mine, it only has a delete function and updating only one column of X amount of rows.

Last edited by simone; 04-01-04 at 12:52 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to validate mutiple rows using the same input text Han84 JavaScript 1 08-02-07 09:14 AM
Deleting multiple rows using radio buttons and text ciggie PHP 2 03-19-04 07:46 PM
Reduce the time intensive phases of .Net application development using TierDeveloper3 smars General Advertisements 0 12-02-03 05:15 AM
Showing time is users time zone? Nasimov PHP 1 11-20-03 07:14 AM
edit php in Squirrelmail jrcortrightiii PHP 1 07-10-03 02:08 PM


All times are GMT -5. The time now is 10:22 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.