0

Working on a configuration page for a website I am getting an error while trying to update user information. Let me show you the part of the code that does not work and then I explain what is the problem:

HTML code - config_view.php:

<div class="container" <?php if ($lock_company_or_user == "0") {echo "";} else {echo "style='display: none'";} ?>>
    <fieldset>
        <legend>¿Eres una empresa o un particular?</legend>
        <p>Necesitamos esta información para adaptar la página de configuración a tu caso.</p>
        <b>Nota: una vez guardada, esta configuración no se puede cambiar. Por favor, asegúrate de escoger la correcta.</b>
        <br><br>
        <form name="select_company_or_user" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
            <input type="radio" name="company_or_user_selector" value="0" /> Empresa -
            <input type="radio" name="company_or_user_selector" value="1" checked /> Particular <br><br>
            <input type="submit" value="Guardar" /><br>
        </form>
    </fieldset>
</div>

PHP code - config_model.php:

// Check if the user sent data
if(!empty($_POST)) {
    // Obtain the data
    $selector = $_POST['company_or_user_selector'];

    // Prepare to update data in the database
    $sql_update = "UPDATE users_extendedprofile SET company_or_user='$selector' WHERE id='$display_name'";

    // Chech if query has been successful
    if(mysqli_query($conn, $sql_update)) {
        mysqli_close($conn);
        $successes[] = lang("SUCCESFULLY_UPDATED_TYPE_OF_USER");
    } else {
        $errors[] = lang("ERROR_SAVING_TYPE_OF_USER");
    }
}

How does it (should) work: I created a very simple registration page in which the users do not give all the information needed to use all parts of the website but at least they can see it and check whether they like it or not. If they decide to continue using it, there is a page in which they input some extra information (mainly for billing purposes). The first "question" I ask to them is to select if they are a company or not - form that only appears if this information was not given previously.

Then, in the model (in the part of it that manages this form) I wrote a code to obtain data from the form, store it as a variable and proceed to store it in the database. The thing here is that page loads without problem and when I click to a radio button and send the form, the result is an error message telling me that information has not been saved.

My first assumption was that the query had any mistake, so I checked it (and compared it to other codes that I prepared to update information in a DB) and I did not found any difference so I decided to perform the query manually in phpMyAdmin (exactly as is written in the PHP form) and it worked perfectly, but then back to the config page the same error appeared again and again.

Also notice that some of the variables that appear in the code are stored before this part of the code (is quite long so I decided to post only the part affecting this form)

So my question is: do you detect any mistake that could affect the process of updating the information in the database?

Thank you very much, and sorry if I made any huge mistake (I am learning by myself so it could probably happen)

-Jordi

  • `$display_name` is defined; where? – Funk Forty Niner Oct 12 '15 at 19:48
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) You should learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) . – Jay Blanchard Oct 12 '15 at 19:49
  • @fred-ii It is defined previously in the code, taking it from the session, as this: $userLoggedIn = $_SESSION['userCakeUser']; $display_name = $userLoggedIn->displayname; – Jordi Planas Oct 12 '15 at 19:52
  • @JayBlanchard Yes I know, I am just preparing a "base" and then I will improve the code, but thank you for both references I will take a look! – Jordi Planas Oct 12 '15 at 19:55
  • Use `mysqli_error($conn)` to get the reason for the query failure. – Barmar Oct 12 '15 at 19:58
  • Solved! The problem was that I closed the connection to the database after obtaining all the information needed to show users config. Thank you to all of you by answering to me! – Jordi Planas Oct 12 '15 at 20:22

0 Answers0