Jump to content

how can i elseif here?


cyberangel

Recommended Posts

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 ---";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/223432-how-can-i-elseif-here/
Share on other sites

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?

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?

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.