Solved

Loop to insert data into mysql table

Posted on 2010-08-22
20
295 Views
Last Modified: 2013-12-13
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
Comment
Question by:savsoft
  • 8
  • 6
  • 3
  • +2
20 Comments
 
LVL 9

Expert Comment

by:Snarfles
ID: 33497990

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
 
LVL 9

Expert Comment

by:Snarfles
ID: 33497993
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
 
LVL 9

Expert Comment

by:Snarfles
ID: 33497997
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
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33498012
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
 

Author Comment

by:savsoft
ID: 33498013
can you help me to make this code by using array?
0
 

Author Comment

by:savsoft
ID: 33498044
i have try above codes but it doesn't insert data...
0
 
LVL 8

Expert Comment

by:kumaranmca
ID: 33498047
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
 
LVL 8

Expert Comment

by:kumaranmca
ID: 33498053
   mysql_query("INSERT INTO squestions(obno, ucode) VALUES ".$values.");
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33498059
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
 
LVL 9

Expert Comment

by:Snarfles
ID: 33498061
The above 3 lines replaces the single line

mysql_query("INSERT INTO squestions(obno, ucode)
      VALUES ('$ob','$testcode')");
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:savsoft
ID: 33498073
yes it insert value of $testcode but leave empty value of $ob
0
 
LVL 8

Expert Comment

by:kumaranmca
ID: 33498127
to assign the value first

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

then execute my code...its work
0
 

Author Comment

by:savsoft
ID: 33498143
sorry kumaranmca,

i have also tried your code its not work
0
 
LVL 9

Accepted Solution

by:
Snarfles earned 500 total points
ID: 33498172
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 33498183
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
 

Author Comment

by:savsoft
ID: 33498205
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
 
LVL 9

Expert Comment

by:Snarfles
ID: 33498228
Hmm that should work...

Your previous page might not be passing nob correctly.
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33498233
echo $nob=$_POST['nob']; just to make sure it returns a number.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33498253
$nob=(int)$_POST['nob'];
0
 

Author Closing Comment

by:savsoft
ID: 33498352
its work 100%
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now