cyberangel Posted January 5, 2011 Share Posted January 5, 2011 hi everybody, every time i asked a question here, i got quick and perfect answers. now i am back again with one i have a simple form which updates some fields in a mysql table. everything works fine, am just trying to convert numbers posted through the form into words on the update page and it is not working :'( for example: the form sends score to the update page, it is either 1, 2 or 3 all i want is that if the score on the form page was 1 then it should update the field in the table with the value "Low" if 2 then "Average" if 3 then "High" the reason is that i dont want the user to select 1,2 or 3 and also low, average and high. so i can simply recieve 1,2 or 3 and update 2 fields at the same time. i tried it with elseif, it doesn't work my code is as follows: <? if($_POST['Submit']){ $host="mysql"; $db_user="me"; $db_password="mypassword"; $database="mydb"; mysql_connect($host,$db_user,$db_password); mysql_select_db($database); $result=mysql_query("SELECT id FROM mytable ORDER BY id ASC"); while($row=mysql_fetch_assoc($result)){ $name=$_POST["name_".$row[id]]; $lastname=$_POST["lastname_".$row[id]]; $email=$_POST["email_".$row[id]]; $score=$_POST["score_".$row[id]]; // this is where i don't simply want to update the record // with the $_POST, rather convert the number into aplhabets, i.e. 1=Low, 2=Average and 3=High mysql_query("UPDATE mytable SET name='$name', lastname='$lastname', email='$email', score='$score' where id='$row[id]'"); } echo "--- Update Complete ---"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/223432-how-can-i-elseif-here/ Share on other sites More sharing options...
trq Posted January 5, 2011 Share Posted January 5, 2011 switch ($_POST["score_" . $row['id']]) { case 1: $score = 'Low'; break; case 2: $score = 'Average'; break; case 3: $score = 'High'; break; } The bigger questions are why are you appending a users id onto the end of all your form elements? And why is this all happening within a while loop? In fact why does the SELECT exist at all? Quote Link to comment https://forums.phpfreaks.com/topic/223432-how-can-i-elseif-here/#findComment-1155002 Share on other sites More sharing options...
MMDE Posted January 5, 2011 Share Posted January 5, 2011 switch($score){ case 1: $scoreresult='low'; break; case 2: $scoreresult='average'; break; case 1: $scoreresult='high'; break; } echo $scoreresult; is the same as if($score==1){ $scoreresult='low'; }elseif($score==2){ $scoreresult='average'; }elseif($score==3){ $scoreresult='high'; } echo $scoreresult; is this what you wanted? Quote Link to comment https://forums.phpfreaks.com/topic/223432-how-can-i-elseif-here/#findComment-1155003 Share on other sites More sharing options...
cyberangel Posted January 5, 2011 Author Share Posted January 5, 2011 perfect you guys are great Quote Link to comment https://forums.phpfreaks.com/topic/223432-how-can-i-elseif-here/#findComment-1155006 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.