Insert value of a radio button into the database

Asked

Viewed 1,789 times

0

I have:

<label class="">
      <input name="user_perfil" type="radio" value="administrador">  Administrador
    </label>
    <label class="">
      <input name="user_perfil" type="radio" value="cliente">  Cliente
    </label>

And in my database I have a column:
enum('administrador', 'cliente', 'suporte_tecnico')

I’m picking up the POST that contains the value of the radio button:
$perfil = $_POST['user_perfil'];

And I’m calling a function:

function novoUsuario($conexao, $user, $password, $email, $nome, $perfil)
{
   $query = "INSERT INTO usuarios (user_name, user_password_hash, user_email,     user_fullname, rank)
            VALUES ('{$user}', '{$user_password_hash}', '{$email}', '{$nome}', '{$perfil}')";
            return mysqli_query($conexao, $query);
}

But in my bank the rank value is not being saved. I will use this rank to open specific pages for each value (Adm, support, etc).

What I’m doing wrong my friends?

  • Checked if the value is getting to php? type: die($profile), and changing the selected option?

  • It really looks all right, debug the value that is coming with the 'print_r($_POST)', if you are going the right values, debug the query with the 'or die(mysqli_error()', try to post here the results of this.

1 answer

0


Caesar, it seems that you are doing everything right. I found no mistake. I am understanding that only the value of the variable $perfil is not being inserted into mysql. I suggest doing a debug.

  1. Let’s see if you’re getting the variable value correctly.
  2. Let’s test the entry in the database with some value.

After these results we can determine where the error is.

Use echo "variável=>".$_POST['user_perfil']; If you see the value in the browser, the radio value recovery is ok. If not, the value is not being recovered correctly.

Right after on the next line use $perfil = "teste"; If you do not see the test value in your database table, it is pq the problem if you find the insertion. If you see test in your database table the problem is in the previous phase.

Another thing. Review the size of sweep(?) in creating the table.

for vc check the error in your insertion advise using mo Mysqli error manager Adapt this code to your function if you haven’t already done so:

if (!mysqli_query($con,"INSERT INTO .... "))
  {
     echo("Erro: " . mysqli_error($con));
  }
  • It is using Enum, it has no size, it only accepts the terms that are in the list

  • Right, I didn’t pay attention to that detail.

  • Zwitterion, good afternoon, little man! A few minutes ago I did two tests. The first was to register a new user without making any changes to the code, result: rank did not save in the database. The second, I added a var_dump to take a look at the post like you asked me to, and _$POST recovery is ok and insertion ok too. I looked at the bank, and everything worked right. I took the var_dump and everything is working correctly. Now the question... Why didn’t it work before when I didn’t change anything in the code? O_O

  • Good afternoon.. happens to me tbm. Sometimes there is a small detail, a comma or quotation marks that we can’t see. But then everything works. Glad it all worked out.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.