samedi 28 janvier 2017

Can't get checkboxes to update correctly (php, mysql)

Sorry if this has already been addressed before but I have searched for a solution here and elsewhere and cannot seem to find out why this isn't working.

So I have a form that pulls data from a DB. The fields are input text boxes with check boxes along the side so that a user can update the title, URL and select whether each one is to be active or inactive.

The problem is a bit hard to explain but it only seems to update one row. What am I missing or doing wrong here?

Thanks for any help! Siafu

So, rssfeeds.php:

echo "<form name='rssfeedupdate' action='rssfeeds_update.php' method='POST'>
<table width='100%' border='0'>";

// SELECT DATA FROM DB
$result_rss = @mysql_query("SELECT * FROM rss_feeds ORDER BY status DESC, title") or die(mysql_error());
$count = mysql_num_rows($result_rss);
while ($row = mysql_fetch_array($result_rss)) {

$id = $row['id'];
$title = $row['title'];
$feedurl = $row['feed'];
$status = $row['status'];

    echo "<tr>
    <td valign='top'>
        $id <input type='text' name='title[]' value='$title' size='20%'></input>
    </td>

    <td valign='top'>
        <input type='text' name='feedurl[]' value='$feedurl' size='60%'></input>
    </td>

    <td valign='top' align='center'>";
        if($status == 'on') {
            $checked = "checked";
        } else { 
            $checked = "";
        }
        echo "
        <input type='checkbox' name='status[]' $checked></input>";

    echo "</td>
    </tr>

    <tr>
    <td valign='top' align='center' colspan='4'>
        <br><hr><br>
    </td>
    </tr>";

<input type='hidden' name='id[]' value='$id'></input>   
}

echo "</table>
<input type='hidden' name='count' value='$count'></input>
<table width='100%' border='0'>
<tr>
<td valign='top' align='right' colspan='4'>
<input type='submit' name='submit' value='Update Feeds'></input>
</td>
</tr>
</table>
</form>";

rssfeeds_update.php:

        // LETS GET INFO FROM rssfeeds.php
        if(isset($_POST['id'])) {
            $id = $_POST['id'];
        }
        if(isset($_POST['count'])) {
            $count = $_POST['count'];
        }
        if(isset($_POST['title'])) {
            $title = $_POST['title'];
        }
        if(isset($_POST['feedurl'])) {
            $feedurl = $_POST['feedurl'];
        }
        if(isset($_POST['status'])) {
            $status = $_POST['status'];
        }

        for($i=0; $i<$count; $i++) {
            echo "<table width='100%' border='0'>
            <tr>
            <td valign='top' width='5%'>
                $id[$i]
            </td>
            <td valign='top' width='20%'>
                $title[$i]
            </td>
            <td valign='top' width='60%'>
                $feedurl[$i]
            </td>
            <td valign='top' width='10%'>
                $status[$i]
            </td>
            </tr>
            </table>";


            $Update_Feeds = "UPDATE rss_feeds SET title='$title[$i]', feed='$feedurl[$i]', status='$status[$i]' WHERE id='$id[$i]'";
        }


        if (!mysql_query($Update_Feeds, $link)) {
            die('<br><br>Error: ' . mysql_error());
        } else {
            echo "<br><br>RSS feeds updated successfully! <a href='rssfeeds.php'>Go Back</a>";
        }




Aucun commentaire:

Enregistrer un commentaire