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
https://forums.phpfreaks.com/topic/102677-looping/
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
https://forums.phpfreaks.com/topic/102677-looping/#findComment-526015
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
https://forums.phpfreaks.com/topic/102677-looping/#findComment-526022
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.