1

I have a PHP script that is supposed to save data from a select function in a database. I did the whole thing with the switch case function. The data should actually be saved, but for an incomprehensible reason, this does not happen. My script looks like this:

 if(isset($_POST['acc_type'])){
  $select = $_POST['acc_type'];
  switch ($select) {
   case '2':
    $sql = "INSERT INTO userdata (accounttype) VALUES (2)";
    break;
   case '3':
    $sql = "INSERT INTO userdata (accounttype) VALUES (3)";
    break;
   default:
    $sql = "INSERT INTO userdata (accounttype) VALUES (1)";
    echo "nothing";
    break;
   }
 }

I have already tested whether it is due to the input (by setting an echo). But this was done. Therefore it must be due to the transfer to the database.

There should be 3 choices that are important for my login. The data should be saved as 1, 2 or 3 (depending on what is specified).

5
  • 4
    I don't see where the query $sql is executed. Do you? Commented Jun 6, 2020 at 20:48
  • The whole thing lies in an If statement. It is related to many other unimportant things. It is executed as soon as a submit button is clicked. I don't want to add the full script now because it is irrelevant. Commented Jun 6, 2020 at 20:51
  • try the query in your databse direkt and post the complete error message and also show us the create table of userdata Commented Jun 6, 2020 at 20:55
  • The command works weirdly in the database. Commented Jun 6, 2020 at 21:05
  • 1
    Debug print the value of $sql, right before you call the mysqli_query() function. Check the return value from the mysqli_query() function. What did you find out? Commented Jun 7, 2020 at 9:26

1 Answer 1

1

First of all, you don't need this repetitive stuff.

$select = ($_POST['acc_type'] == 2 || $_POST['acc_type'] == 3) ? $_POST['acc_type'] : 1;
$sql = sprintf("INSERT INTO userdata (accounttype) VALUES (%d)", $select);

A better solution would be to prepare the statement properly, but I don't know how you plan to execute your query (you didn't provide any code). If that's all, you actually don't insert anything in your database, you just set the query to a variable.

Sign up to request clarification or add additional context in comments.

3 Comments

Okay thanks I will change it. Nevertheless, it should actually work if I didn't do something wrong myself. I have another INSERT INTO which is marked with the same variable. It is directly under this function. This works perfectly. Maybe I did something wrong there. If so, please clarify. Do not know me well yet: D
I have now removed the variable but it still does not work. Maybe someone has an answer?
Then provide please some more code, there is no more context to answer with... Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.