Jump to content

Looping


june_c21

Recommended Posts

WHy my code can't loop when name = empty

. what should i put in the while statement?

 

 

<?php
session_start();
$host = 'localhost';
$user = 'root';
$password = '';
$dbase = 'project';

$dblink = mysql_connect($host,$user,$password);
mysql_select_db($dbase,$dblink);

$sname    = $_POST['name'];
$age = $_POST['age'];

While ($name=NULL){ 
     
$query= "INSERT INTO list(name,age) VALUES ('$name', '$age')";
$result = mysql_query($query,$dblink);
}


?>

Link to comment
Share on other sites

This code is all wrong ... i think you need to read up on php better mate  if you want to loop somthing you need to use a counter variable so you can stop it when you want somthing like this

 

 

 

While ($counter <=10)

{

dotihis

dothis

dothis

$counter++;//adds one to counter

}

 

also since your getting $sname from the get you will only have one set of data so the same data will be entered into teh database over and over is this what you want ?

 

Link to comment
Share on other sites

you don't have to only use a counter, you can use boolean.

 

<?php
$isGood = TRUE;
while($isGood){
     $num = rand(3);
     if($num == 0){
          $isGood = TRUE;
     }
     if(num == 1){
          $isGood = TRUE;
     }
     if(num == 2){
          $isGood = TRUE;
     }
     if(num == 3){
          $isGood = FALSE;
     }
}
?>

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.