2

I'm currently trying to get a database updated through a PHP form. Firstly I get all the information from the database within the form, and then trying to update details within that form to reflect on the database.

I could successfully managed to get all the details from the database within the form but unfortunately doesn't update for that reason.

Here is the code to retrieve the info from DB, within the form which works 100%:

include_once 'db_connect.php';

if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<form id=updateVehicle action='' method=post";
            echo "<div id='vehicleDescription'>";

                echo "<div>";
                    echo "<label for=vin>VIN</label>";
                    echo "<input type=text id=vin name=vin minlength=17 maxlength=17 placeholder=e.g. BMW1234567890ABCD value="  . $row['vin'] . " />";
                    echo "<br/>";
                echo "</div>";

                echo "<div>";
                    echo "<label for=make>Make</label>";
                    echo "<input type=text id=make name=make minlength=3 maxlength=13 placeholder=e.g. BMW value=" . $row['make'] . " />";
                    echo "<br/>";
                echo "</div>";

                echo "<div>";
                    echo "<label for=model>Model</label>";
                    echo "<input type=text id=model name=model minlength=2 maxlength=15 placeholder=e.g. 530D value=" . $row['model'] . " />";
                    echo "<br/>";
                echo "</div>";

               echo "<div>";
                    echo "<input type=text name=id value=" . $row['id'] . " />";
                echo "</div>";

                echo "<span><?php echo $errorMessage;?></span>";
            echo "</div> <!--End of vehicleDescription div-->";
            echo "<input type='submit' class='submit' value='Update Ads' />";
        echo "</form>";
    }
}

The action is empty because it submits to itself, I've tried to use "echo htmlspecialchars($_SERVER["PHP_SELF"]);" but it didn't work, so I left it out.

Here is the code to process the update which doesn't work:

if(isset($_POST['vin'], $_POST['make'], $_POST['model'])) {
    $vin = filter_input(INPUT_POST, 'vin', FILTER_SANITIZE_STRING);
    $make = filter_input(INPUT_POST, 'make', FILTER_SANITIZE_STRING);
    $model = filter_input(INPUT_POST, 'model', FILTER_SANITIZE_STRING);
    $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);

    if($insert_stmt = $mysqli->prepare("UPDATE vehicles SET (vin=?, make=?, model=? WHERE id=? ")) {
        $insert_stmt->bind_param("sssi", $vin, $make, $model, $id);

        if(!$insert_stmt->execute()) {
            header('Location: ../error.php?err=Registration failure: UPDATE');
            $mysqli->close();
        }
    }
    header('Location: ../includes/register_success.php');
    $mysqli->close();
}

For some unknown reasons to me, it doesn't update the database at all. The 'db_connect.php' works 100% as I'm using it for other database queries.

I'd really appreciate any help if you could point me in the right direction.

Thank you!

4
  • Do you get any errors in your error_log? And what happens if you try to echo $vin $make $model $id after sanitizing them? Commented Jul 12, 2015 at 17:10
  • Lose the parentheses? Commented Jul 12, 2015 at 17:25
  • Hi Qirel, Whenever I try to active the error log, I will end up with "Fatal error: Class 'mysqli' not found in...". However, if the error reporting is turned off the code executes as it should, apart the code which doesn't work as mentioned above. Also, when I echo those variables nothing happens. Presumably the variables aren't set? Thank you Commented Jul 12, 2015 at 18:12
  • Hi Drew Pierce, I tried to remove them but still no luck. Any other suggestions? Thank you Commented Jul 12, 2015 at 18:13

1 Answer 1

1

Aren't you getting an error when executing the update query? I can see an extra ( after SET in:

UPDATE vehicles SET (vin=?, make=?, model=? WHERE id=? 

This should cause the query to fail which is a good reason why things are not updated in the database.

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

15 Comments

He doesn't have error reporting turned on or care to look at log
Hi jedrzej.kurylo, I tried to remove the parentheses you mentioned above but still no luck. Any other suggestion? Thank you
Hi Drew Pierce, Whenever I configure the PHP Error Log I will end up with "Fatal error: Class 'mysqli' not found in...". If I comment out the error log from PHP.ini, the code works fine, well apart the one mentioned above. Do you have any idea why is that? Thank you
php.net/manual/en/mysqli.installation.php you are missing mysqli extension
Uh ... Sorted! Finally! I thought would be better if I will rewrite everything and worked! Thank you so much for your help. You're a star
|

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.