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
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>';
    }

Link to comment
Share on other sites

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>";
    }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.