Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

How to insert Distinct Records OR Avoid repetition when insering values into DB

i have some trouble when i insert values into database the record repeats himself how can i avoid this problem guys
and thank alot
ps:i spend one week thinking and searching how to avoid this trouble without result

[Image: Okfi57g.png]
PHP Code

<?php 
$sql
="SELECT name FROM ingredient";
     
$result=mysqli_query($conn,$sql);
   
/* if (!$result) {
    printf("Error: %s\n", mysqli_error($conn));
    exit();
}*/
echo"<select name='cars[]' id='cars' multiple >";
    
    while(
$row=mysqli_fetch_assoc($result))
        {
            foreach (
$row as $key => $okhrej) {              
               
                 echo
"<option value=".$okhrej.">".$okhrej."</option>"; 

                 
            }
            
        }   

 echo
"</select>";


  
$sql1="SELECT name FROM ingredient";
     
$result1=mysqli_query($conn,$sql1);
       while(
$row1=mysqli_fetch_assoc($result1))
        {
            foreach (
$row1 as $key1 => $okhrej1) {              
               
                 echo 
"<input name='ingsave[]' >";

                 
            }
            
        }   
    
mysqli_close($conn); 
[Image: GBiqNLg.png]
PHP Code

<?php 
$selectop 
= $_POST['cars'];
$name = $_POST['nom'];
$prix = $_POST['prix'];
$prixn = $_POST['prix-neut'];
$savesplease = $_POST['ingsave'];
if(!empty(
$_POST['cars'])){
foreach (
$selectop as $key ) {
    foreach (
$savesplease as $key1 ) {
         
mysqli_query($conn,"INSERT INTO coffe VALUES ('$key','$name','$prix','$prixn','$key1')");
        }
 }


echo 
"New record created successfully";
}
 else {
  echo 
"_error_: " . $sql . "<br>" . mysqli_error($conn);
} 
Writeln('Give me a Target >');
[Image: giphy.gif]
(Jul 22, 2020 04:53 PM)blackcoder00 Wrote:  i have some trouble when i insert values into database the record repeats himself how can i avoid this problem guys
and thank alot
ps:i spend one week thinking and searching how to avoid this trouble without result

[Image: 4TzRd3G]
PHP Code

<?php 
  $sql
="SELECT name FROM ingredient";
 
    $result=mysqli_query($conn,$sql);
 
  /* if (!$result) {
    printf("_error_: %s\n", mysqli_error($conn));
    exit();
}*/
echo"<select name='cars[]' id='cars' multiple >";
 
 
   while($row=mysqli_fetch_assoc($result))
 
       {
 
           foreach ($row as $key => $okhrej) {           
               
                 
echo"<option value=".$okhrej.">".$okhrej."</option>"; 

 
                
            
}
 
           
        
}   

 
echo"</select>";


 
 $sql1="SELECT name FROM ingredient";
 
    $result1=mysqli_query($conn,$sql1);
 
      while($row1=mysqli_fetch_assoc($result1))
 
       {
 
           foreach ($row1 as $key1 => $okhrej1) {           
               
                 
echo "<input name='ingsave[]' >";

 
                
            
}
 
           
        
}   
    mysqli_close
($conn); 
[Image: S62W4rc]
PHP Code

<?php 
$selectop 
= $_POST['cars'];
$name = $_POST['nom'];
$prix = $_POST['prix'];
$prixn = $_POST['prix-neut'];
$savesplease = $_POST['ingsave'];
if(!empty(
$_POST['cars'])){
foreach (
$selectop as $key ) {
 foreach (
$savesplease as $key1 ) {
 
mysqli_query($conn,"INSERT INTO coffe VALUES ('$key','$name','$prix','$prixn','$key1')");
 }
 }


echo 
"New record created successfully";
}
 else {
 
 echo "_error_: " . $sql . "<br>" . mysqli_error($conn);
} 

google works nice

https://www.tutorialspoint.com/mysql/mys...icates.htm

try it
(Jul 22, 2020 04:55 PM)BadLandChugs Wrote:  google works nice

https://www.tutorialspoint.com/mysql/mys...icates.htm

try it

i didn't find anything that could help me
Writeln('Give me a Target >');
[Image: giphy.gif]
Why is your insert query in 2 loops? Post the full code, it’s hard to tell what’s even happening here.
Je t'aime
[Image: fpZ5a5g.png]
(Apr 22, 2022 04:39 PM)-Scrub Wrote:  Jokes on you, I'm not smart enough to know what he's talking about.
delete foreach https://www.w3schools.com/php/php_mysql_select.asp
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Jul 23, 2020 06:05 AM)HFDone Wrote:  delete foreach https://www.w3schools.com/php/php_mysql_select.asp

thank you bro but i solved the problem from the last night
the correct code
PHP Code

<?php 
if(!empty($_POST['cars'])){
$stmt = mysqli_prepare($conn,'INSERT INTO coffe VALUES(?, ?, ?, ?, ?)');
mysqli_stmt_bind_param($stmt, 'ssiii', $v, $_POST['nom'], $_POST['prix'], $_POST['prix-neut'], $save);
foreach (
$_POST['cars'] as $k => $v) {
    
$save = $_POST['ingsave'][$k];
    
mysqli_stmt_execute($stmt);
}


echo 
"New record created successfully";
}
 else {
  echo 
"_error_: " . $sql . "<br>" . mysqli_error($conn);
} 

(Jul 23, 2020 04:54 AM)Nachfolger Wrote:  Why is your insert query in 2 loops? Post the full code, it’s hard to tell what’s even happening here.

yeah that's why i got that problem (insert the record 4*2 ) because i have put it into two foreach loop
Writeln('Give me a Target >');
[Image: giphy.gif]


Possibly Related Threads...
Thread / Author Replies Views Last Post
by bigfatjoeApr 3, 2024 02:33 AM 5 188 Apr 3, 2024 02:33 AM
Last Post: siliconvalley
[DBMS] by SIM-3343347Feb 9, 2019 06:12 PM 3 296 Feb 9, 2019 06:12 PM
Last Post: +Haze
by AurianaJan 5, 2019 12:35 PM 7 222 Jan 5, 2019 12:35 PM
Last Post: Auriana