Jump to content

problem with adding multiple data in mysql


joliocesar

Recommended Posts

hi every body . i'm new member ...

 

i want to add multiple data to my mysql database but i cant by two foreach ...

 

 

must 5 items added to database but 25 items added !

 

please help me ..

 

this is my code

 

if($mode=="cart_all")
{

$id = $_POST['check'];
$cat = $_POST['cat'];
?>




Items Selected for adding to shopping cart






$nums = count($id);
foreach($id as $lists)
{
$query1 = "select * from sublink_2 where id='$lists' and cat='$cat'";
$query1 = mysql_query($query1);

if($nums == "0")
{
echo "nothing for display";
}

while($query2 = mysql_fetch_array($query1)){
$id = $query2['id'];
$title_s = $query2['title'];
$pre = $query2['pre'];
$price = $query2['price'];
$photo = $query2['link_photo'];
?>








?>















?>






Price




MoreMore }
}

?>






}

?>

if($mode=="add_all_cart")
{

$cat = htmlspecialchars($_POST['cat']);
$id = $_POST['id'];
$qua = $_POST['qua'];


mklog($username_2,"Add items to shopping cart -- selecting",$_SESSION['permission']);

foreach($id as $list)
{

$query1 = "select * from sublink_2 where id='$list' and cat='$cat' ";
$query1 = mysql_query($query1);

while($query2 = mysql_fetch_array($query1))
{
$id = $query2['id'];
$title_s = $query2['title'];
$pre = $query2['pre'];
$photo = $query2['link_photo'];
$price = $query2['price'];
}
$select = "select * from cart where item_id = '$list' and username = '$username_2'";
$select = mysql_query($select);
$select_num = mysql_num_rows($select);
foreach($qua as $quant)
{
if($select_num == "0")
{

$insert = "insert into cart (title,item_id,quantity,username,price,total,read_r,payed,date,approve) values
('$title_s','$list','$quant','$username_2','$price','$total_ps','0','0','$date','0')";

if(!mysql_query($insert))
{
error(mysql_error());
}

}
}



}

?>




noerror("$title_linkS Added Successfully To your Shopping Cart");
redirection("?mode=cart_show","4500");

?>



}?>

 

thanks

Link to comment
Share on other sites

please help me for this problem...

 

i try another ways but my methods is not good enough for this ...

 

 

i want to add many items with check boxes and already i have input type = text that has values ... i wanna get this values but in second loop .. my loop repeat again ... and item added more than i want ...

 

 

please help me .. how i can get out my second loop (foreach) that dont repeat that loop for times ... ??

 

with what condition i can break my loop ?

 

what's your solutions ?!

 

thank you so much ... :D this is very important for me ...

Link to comment
Share on other sites

You should never, never use loops for doing queries like that. It is a big performance hit and there are better ways to do it.

 

I can't really make sense of the first block of code you posted as tehre are multiple instances of PHP closing tags in succession - it looks like you left out the code for displaying the results. Instead of looping through the IDs, just create a single query:

 

$query = "SELECT *
          FROM sublink_2
          WHERE id IN (" . implode(',', $id) . ") and cat='$cat'
          ORDER BY id";

$result = mysql_query($query) or die(mysql_error());
if (!mysql_num_rows($result))
{
    echo "nothing for display";
}
else
{
    //display the results
}

 

The second block of code makes little sense to me as well. It looks like you created looping structures that are not properly nested. The Loop using "foreach($qua as $quant)" is used to make the inserts into the database, but the values it uses are created in the "while($query2 = mysql_fetch_array($query1))". That loop ends before the $qua loop begins, so the same values will be inserted into the database based upon how many records there are in the first query results. (properly nesting your code will help prevent this type of error).

 

I took a shot at rewriting it more efficiently, but I'm not 100% sure of the result/logic you want. (I'm sure there are typos as well as I could not test it)

 

if($mode=="add_all_$listcart")
{

    $cat = htmlspecialchars($_POST['cat']);
    $id = $_POST['id'];
    $qua = $_POST['qua'];


    mklog($username_2,"Add items to shopping cart -- selecting",$_SESSION['permission']);

    //Run query to get records in sublink_2 for list of IDs
    //where there are no matching records in cart
    $query = "select * 
              FROM sublink_2
              RIGHT JOIN cart ON sublink_2.id = cart.item_id
              WHERE sublink_2.id IN (" . implode(',', $id) . ")
                AND sublink_2.cat='$cat'
                AND cart.item_id IS NULL
              ORDER BY id";
    $result = mysql_query($query) or die (mysql_error());


    if (mysql_num_rows($result))
    {
        //Create insert records from the results
        while($record = mysql_fetch_array($result))
        {
            //Get the corresponding quantity for the id
            if(array_search($record['id'], $id) && count($qua) > array_search($record['id'], $id))
            {
                $quant = $qua[array_search($record['id'], $id)];
                $insert_records[] = "('{$record['title']}', '{$record['id']}', ";
                                  . "'{$quant}', '{$username_2}', '{$record['price']}', "
                                  . "' $total_ps', '0', '0', '$date', '0')\n";
            }
        }

        //Creat the combined insert query and run
        $query = "INSERT INTO cart (title,item_id,quantity,username,price,total,read_r,payed,date,approve)\n "
               . "VALUES (" . implode(',', $insert_records) . ")";
        $result = mysql_query($query) or die (mysql_error());

    }
}


noerror("$title_linkS Added Successfully To your Shopping Cart");
redirection("?mode=cart_show","4500");

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.