june_c21 Posted April 24, 2008 Share Posted April 24, 2008 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 More sharing options...
JasonLewis Posted April 24, 2008 Share Posted April 24, 2008 You don't even have a variable called 'name' in the first place. And you are setting it to NULL not checking if it is NULL. == means "is equal to". = means.. "equals". What are you trying to do? Link to comment https://forums.phpfreaks.com/topic/102677-looping/#findComment-526010 Share on other sites More sharing options...
The Little Guy Posted April 24, 2008 Share Posted April 24, 2008 Why do you even have a while loop? Link to comment https://forums.phpfreaks.com/topic/102677-looping/#findComment-526014 Share on other sites More sharing options...
mofm Posted April 24, 2008 Share Posted April 24, 2008 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 More sharing options...
The Little Guy Posted April 24, 2008 Share Posted April 24, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.