0

I have a form with pull down select. I would like to save to mysql the result of select like this:

If select = teacher then in Mysql add into account column value as teacher and in another column add account type as 1 else if select = student then, in Mysql add account value as student and in the other column add account type as 2.

$account = 'account';
if ($account = "Agency") 
$accounttype = "1";  //Agency
elseif ($account= "Landlord") 
$accounttype = "2";  //Landlord

$user = [
'name'              => $this->request->getPost('name'),
'account_no'        => rand(),
'email'             => $this->request->getPost('email'),
'account'           => $this->request->getpost('account'),
'account_type'      => $accounttype,

Am able to add the account as either teacher or student but unable to add account type 1 or 2 in the account type column.... help me format my code.

Yoshi
  • 54,081
  • 14
  • 89
  • 103
Madmann
  • 9
  • 3
  • An `=` assigns, 2 compares. You need `if ($account == "Agency")`. I don't know why you'd store an integer value and text version though. Just have the integer values in a different table and if you need it as text join it. – user3783243 Nov 30 '21 at 12:28
  • i dont understand, can you elaborate more please? Thank you. – Madmann Nov 30 '21 at 12:29
  • And, as your first line states (`$account = 'account';`) your `$account`variable is never Agency or Landlord, is always `account`, so your `$accountype`is allways null – nacho Nov 30 '21 at 12:30
  • 1
    `if ($account = "Agency") ` sets `$account` to `Agency`. Whereas `if ($account == "Agency")` checks if `$account` equals `Agency`. – user3783243 Nov 30 '21 at 12:30
  • Thank you i have redone code as if ($account == "Agency") but now it throws me -- ErrorException Undefined variable $accounttype – Madmann Nov 30 '21 at 12:39
  • 1
    If `$account` is `account`, then it won't match either of your if statements, and `$accounttype` won't be defined. Add an `else` statement as a catchall or define the variable before the if blocks – aynber Nov 30 '21 at 12:41
  • I have redone it this way.... if ('account' == "Agency") $accounttype = "1"; else $accounttype = "2"; account is the select value in select option in my form. It now assigns all account types to 2 – Madmann Nov 30 '21 at 12:49
  • 1
    `'account' == "Agency"` This is comparing two strings straight out, no variables. Of course they aren't going to match. – aynber Nov 30 '21 at 13:01
  • ok this is my form select : How should i take value of select and store as either 1 or 2 depending on choice? – Madmann Nov 30 '21 at 13:13
  • Well, you need to get the value from `$_POST`. At least, I hope your form is using post for the method. So you would do `$account = $_POST['account'];`. **Then** you can compare it. However, your choices are `Teacher` and `Student`, and you're comparing it to `Agency` and `Landlord`, so it won't match anyway – aynber Nov 30 '21 at 13:15
  • sorry for the confusion, its Teacher and Student @aynber – Madmann Nov 30 '21 at 13:16
  • THANK YOU guys for pointing me to the right direction. I used if == in the ($account == "Teacher") and $account = $_POST['account']; it worked. – Madmann Nov 30 '21 at 13:22
  • @aynber i have read your profile, i have php laravel issue. I have application for sending sms written in laravel. It has a limit where API requests are limited to around 60, I think its default, something to do with throttling, so when i send many sms they fail with error "TOO MANY REQUESTS"...any idea about this? – Madmann Nov 30 '21 at 13:43
  • This should really be a separate question. However, you cannot change the API limit for 3rd party services. You'll need to figure out how *not* to send so many SMS messages, or ask if they can change the limit. – aynber Nov 30 '21 at 13:45
  • let add it as a seperate question, thanks – Madmann Nov 30 '21 at 14:00
  • hey guys, check my other codeignitor question, see if you can help. I am unable to save a form to db and i dont knw why. Thanks in advance – Madmann Dec 04 '21 at 15:29

0 Answers0