Jump to content

[SOLVED] SQL duplicate entries


krv

Recommended Posts

Hello,

I have a script that allows you to select line items(checkbox) that hold a product id (hidden field, cs2[]) and $ amount (text field, cs3[]).

 

I try to pass the checked items with a foreach() function to then create an array of queries.

All i get is the first text box 2x in the database.

 

Any input is appreciated. Thanks.

 

Example of script:

mult.jpg

 

    if (isset($_POST['scl']) && !empty($_POST['cs1']))
    {
        if ($_POST['checkbox'] == 0)
        {
            $nc = "<span style=\"background-color: #fff1a8;\">No services selected</span>";
        } else
        {
            foreach ($_POST['checkbox'] as $id)
            {
                $queries = array();
                for ($i = 0; $i < count($id); $i++)
                {
                    $cs1 = $_POST['cs1'];
                    $cs2 = $_POST['cs2'];
                    $cs3 = $_POST['cs3'];
                    if (!get_magic_quotes_gpc())
                    {
                        $cs2[$i] = addslashes($cs2[$i]);
                        $cs3[$i] = addslashes($cs3[$i]);
                    }
                    $queries[] = "('$cs1 ','$cs2[$i]','$cs3[$i]')";
                }
                $piece = implode(", ", $queries);
                $query = "INSERT INTO cust_services (cs1,cs2,cs3) VALUES $piece";
                $result = mysql_query($query) or die(mysql_error());
                // header("Refresh:0;url=");
                $nc = "<span style=\"background-color: #fff1a8;\">Service Added</span>";
            } // end checkbox loop
        }
    } else
    {
        $nc = '<span style="background-color: #fff1a8;">Please select a facility</span>';
    }

Link to comment
https://forums.phpfreaks.com/topic/149852-solved-sql-duplicate-entries/
Share on other sites

My error was in my for() loop.

I was trying to have a list of items that pulled from a database display in a table. Within each row there would be a hidden field with an item id and a text field to enter in a value that associates with the item id. Then i wanted to be able to check which items i wanted to save and also store the value.

 

I was having an issue inserting the Correct value with the item id.

I fixed this by subtracting 1 or $i -1 due to an issue where the items and values would insert into the database but would be off by one.

    if (isset($_POST['scl']) && !empty($_POST['cs1']))
    {
        if ($_POST['checkbox'] == 0)
        {
            $nc = "  <span style=\"background-color: #fff1a8;\">No services selected</span>";
        } else
        {
            foreach ($_POST['checkbox'] as $id)
            {
                $queries = array();
                for ($i = 0; $i < count($id); $i++)
                {
                    $cs1 = $_POST['cs1'];
                    $cs2 = $_POST['cs2'];
                    $cs3 = $_POST['cs3'];
                    if (!get_magic_quotes_gpc())
                    {
                        $cs2[$i] = addslashes($cs2[$i]);
                        $cs3[$i] = addslashes($cs3[$i]);
                    }
                    $ls = $id -1;
                    $queries[] = "('$cs1 ','$id','$cs3[$ls]')";
                }
                $piece = implode(", ", $queries);
                $query = "INSERT INTO cust_services (cs1,cs2,cs3) VALUES $piece";
                $result = mysql_query($query) or die(mysql_error());
                // header("Refresh:0;url=");
                $nc = "<span style=\"background-color: #fff1a8;\">Service Added</span>";
            } // end checkbox loop
        }
    } else
    {
        $nc = '<span style="background-color: #fff1a8;">Please select a facility</span>';
    }

Solution

    if (isset($_POST['scl']) && !empty($_POST['cs1']))
    {
        if ($_POST['checkbox'] == 0)
        {
            $nc = "  <span style=\"background-color: #fff1a8;\">No services selected</span>";
        } else
        {
            foreach ($_POST['checkbox'] as $id)
            {
                $queries = array();
                //for ($i = 0; $i < count($id); $i++)
                //{
                    
                   // $cs2 = $_POST['cs2'];
                   // $cs3 = $_POST['cs3'];
                   // if (!get_magic_quotes_gpc())
                    //{
                       // $cs2[$i] = addslashes($cs2[$i]);
                     //   $cs3[$i][$id] = addslashes($cs3[$i]);
                    //}
                    //$ls = $id - 1;
                    $itemId = $_POST['cs2'][$id];
            		$itemValue = $_POST['cs3'][$id];
                    $queries[] = "('$cs1 ','$itemId','$itemValue')";
                //}
                $piece = implode(", ", $queries);
                $query = "INSERT INTO cust_services (cs1,cs2,cs3) VALUES $piece";
                $result = mysql_query($query) or die(mysql_error());
                $nc = "<span style=\"background-color: #fff1a8;\">Service Added</span>";
                header("Refresh:0;URL=services?c=1");
            } // end checkbox loop
        }
    }
<?php
    $query = "SELECT id,s1,s2 FROM services";
    $result = mysql_query($query) or die("Can't create query: " . mysql_error());
    while ($row = mysql_fetch_array($result))
    {
        echo "
		<tr style=\" background-color: #ffffff\" onMouseover=\"this.style.backgroundColor='lightblue';\" onMouseout=\"this.style.backgroundColor='white';\">
			<td class='ul'>$row[s1]</td>
			<td class='ul'><input type=\"hidden\" name=\"cs2[$row[id]]\" value=\"$row[id]\"><input type=\"text\" name=\"cs3[$row[id]]\" value=\"\" /></td>
			<td><input type='checkbox' name='checkbox[]' value='$row[id]'></td>
		</tr>";
    }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.