• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

Loop to insert data into mysql table

I have some values in post like:

$ob1=$_POST['ob1'];
$ob2=$_POST['ob2'];
$ob3=$_POST['ob3'];
----------------------upto 30-----------
$ob50=$_POST['ob50'];

first i want to add loop to make code short.

now i use code to insert these value to table:


$query1= mysql_query("INSERT INTO squestions(obno, ucode)
VALUES
('$ob1','$testcode')");


$query2= mysql_query("INSERT INTO squestions(obno, ucode)
VALUES
('$ob2','$testcode')");


$query3= mysql_query("INSERT INTO squestions(obno, ucode)
VALUES
('$ob3','$testcode')");


-------------upto 50 queries----------------


$query50= mysql_query("INSERT INTO squestions(obno, ucode)
VALUES
('$ob50','$testcode')");


Please help me ,
how to add loop for this?





0
savsoft
Asked:
savsoft
  • 8
  • 6
  • 3
  • +2
1 Solution
 
SnarflesCommented:

for ( $counter = 1; $counter <= 50; $counter++) {
$ob=$_POST['ob$counter'];
mysql_query("INSERT INTO squestions(obno, ucode)
VALUES
('$ob','$testcode')");

}

Open in new window

0
 
SnarflesCommented:
Oh hang on... 'up to 50'... means a little change to check

Also I'm making the assumption $testcode is declared further up and is always the same?
for ( $counter = 1; $counter <= 50; $counter++) {
if (isset($_POST['ob$counter'])){
$ob=$_POST['ob$counter'];
mysql_query("INSERT INTO squestions(obno, ucode)
VALUES
('$ob','$testcode')");
}
}

Open in new window

0
 
SnarflesCommented:
Actually to be honest 50 post variables is a lot... you would probably be better off passing a string which you can then explode into an array... not sure if thats going to work for your code or not... but something to think about.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cyberkiwiCommented:
Snarfies, borrowing your code..

I tend to use the below. isset only tests if the var is set, as long as the POST var is in the form, and it is an empty string, isset still returns true.
for ( $counter = 1; $counter <= 50; $counter++)
{
  if ($_POST['ob$counter']>'')
  {
    $ob=$_POST['ob$counter'];
    mysql_query("INSERT INTO squestions(obno, ucode)
      VALUES ('$ob','$testcode')");
  }
}

Open in new window

0
 
savsoftAuthor Commented:
can you help me to make this code by using array?
0
 
savsoftAuthor Commented:
i have try above codes but it doesn't insert data...
0
 
kumaranmcaCommented:
I found the good solution of Mysql query...

Try my the below code...to solve your needs...


for($i=0;$i<=50;$i++){
      $values.="('".$ob.$i."','".$testcode."')";
}
echo $values;
INSERT INTO squestions(obno, ucode) VALUES $values
0
 
kumaranmcaCommented:
   mysql_query("INSERT INTO squestions(obno, ucode) VALUES ".$values.");
0
 
SnarflesCommented:
savsoft do this

echo $query = "INSERT INTO squestions(obno, ucode)
      VALUES ('$ob','$testcode')";
echo "<br />";
mysql_query($query);

and tell me if it spits out the correct queries.
0
 
SnarflesCommented:
The above 3 lines replaces the single line

mysql_query("INSERT INTO squestions(obno, ucode)
      VALUES ('$ob','$testcode')");
0
 
savsoftAuthor Commented:
yes it insert value of $testcode but leave empty value of $ob
0
 
kumaranmcaCommented:
to assign the value first

$ob='1';
$ob1='2';
$ob2='3';
....
$ob50='50';

then execute my code...its work
0
 
savsoftAuthor Commented:
sorry kumaranmca,

i have also tried your code its not work
0
 
SnarflesCommented:
Sorry I've given you a bit of a bum steer.

'ob'.$counter not 'ob$counter'
for ( $counter = 1; $counter <= 50; $counter++)
{
  if ($_POST['ob'.$counter]>'')
  {
    $ob=$_POST['ob'.$counter];
    mysql_query("INSERT INTO squestions(obno, ucode)
      VALUES ('$ob','$testcode')");
  }
}

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
Is the number of $_POST['ob_'] variables the same each time or different?  Do you already have the code to connect to the server and select the database?  How are you testing whether it works or not?
0
 
savsoftAuthor Commented:
yes, Snarfles
its work,
Thank you for your great help.
can you help me litle more?

if i changed above code to:

$nob=$_POST['nob'];

for ( $counter = 1; $counter <= $nob; $counter++)
{
  if ($_POST['ob'.$counter]>'')
  {
    $ob=$_POST['ob'.$counter];
    mysql_query("INSERT INTO squestions(obno, ucode)
      VALUES ('$ob','$testcode')");
  }
}

its not work if i replaced 50 $nob or '$nob'
0
 
SnarflesCommented:
Hmm that should work...

Your previous page might not be passing nob correctly.
0
 
SnarflesCommented:
echo $nob=$_POST['nob']; just to make sure it returns a number.
0
 
cyberkiwiCommented:
$nob=(int)$_POST['nob'];
0
 
savsoftAuthor Commented:
its work 100%
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now