Solved

attaching number to variable in while statement

Posted on 2002-06-05
5
192 Views
Last Modified: 2008-02-20


hmm, I don't seem to be doing this right.

I want to insert rows of data into mysql.

The data coming from the form is incremented.  The values are like this:

edtitle1=a&edurl1=b&edtitle2=c&edurl2=d&edtitle3=f&edurl3=g...ect...

So I figured I could do a while statement and increment it like below, but it does not work (I end up with the numbers I intended to be part of the variable actually in the database, but not the variables value.)

The while statement is intended to check to make sure there is a value (there might be a better way to do that, I don't know), and if there is a value should enter it into the db.  Then go to and check the next intereration, up to 10 (starting with 0).

How do I fix this?

Thanks,    Chris



/*insert the new links*/

$i=0;

WHILE ($i < 10):

if ($edtitle.$i != "" && $edurl.$i  != ""){
mysql_query("INSERT INTO ky_edlinks (edtitle,edurl) VALUES ('$edtitle.$i','$edurl.$i')");
}
$i++;  
ENDWHILE;
0
Comment
Question by:ChrisAndrews
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 5

Expert Comment

by:Hamlet081299
ID: 7058653
In answer to you question...

$i=0;

WHILE ($i < 10):

$edtitle = 'edtitle' . $i;
$edurl = 'edurl' . $i;
if ($$edtitle != "" && $$edurl != ""){
mysql_query("INSERT INTO ky_edlinks (edtitle,edurl) VALUES ('{$$edtitle}','{$$edurl}')");
}
$i++;  
ENDWHILE;

0
 
LVL 5

Accepted Solution

by:
Hamlet081299 earned 100 total points
ID: 7058654
And maybe a little tidier with a for loop...

for ($i=0; $i < 10; $i++) {
  $edtitle = 'edtitle' . $i;
  $edurl = 'edurl' . $i;
  if ($$edtitle != "" && $$edurl != ""){
    mysql_query("INSERT INTO ky_edlinks (edtitle,edurl) VALUES ('{$$edtitle}','{$$edurl}')");
  }
}
0
 
LVL 5

Expert Comment

by:Hamlet081299
ID: 7058659
I guess a little bit of explanation may be in order.

If you have variables..

$var = 123;
$varname = 'var';

echo $$varname; // outputs 123

... and if you want to embed that in a string you need to use curly syntax...

echo "$varname is {$$varname}"; // outputs "var is 123"

0
 
LVL 4

Expert Comment

by:lokeshv
ID: 7058757
How abt changing ur form ?? a better option i think

some thing like this

$num // total edurl

<? for($i=0;$i<$num;$i++){?>
<tr>

<td><input type=text name=edtitle[$i]> </td>
<td>< input type=text name=edurl[$i]> </td>

</tr>

<?}?>


and here is ur code for form handling

$info=$HTTP_POST_VARS;

for($i=0;$i<count($info[edtitle]);$i++){
   
   mysql_query("insert into ur_table(edtitle,edurl) values('$info[edtitle][$i]','$info[edurl][$i]');
}


Hope this helps

Lk
0
 

Author Comment

by:ChrisAndrews
ID: 7059344

That works well, thank you Hamlet.

Lk - thank you your comment also, Hamlets just works out easier for what I already have set up,

Chris
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Link failure 16 64
Simple function not working 7 44
AJAX, If No returns don't let the AJAX Call run again 2 36
how to use jquery variable in mysql query 20 35
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 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