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); } ?> Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment 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 ? Quote Link to comment 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; } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.