-1

I'm at a complete loss here. I've written a relatively simple PHP script which updates a database record based on user input from a HTML form. The script contains an 'if' statement which executes based a hidden input. I know that the statement executes because the SQL query executes without a problem. The problem I'm having is that there there is another if statement within which should execute if the query object is set, but apparently it doesn't because the $message variable within is not assigned a value. I know that the query object is set because when I echo it it shows up as '1'. Below is the code block in question:

<?php  
if(isset($_POST['submitted']) == 1) {
$name = mysqli_real_escape_string($dbc, $_POST['name']);
    $q = "UPDATE ".$_POST['table']." SET name = '".$name."' WHERE id = ".$_POST['id'];
    $r = mysqli_query($dbc, $q);
    echo $r;
    print_r($_POST);
    echo mysqli_error($dbc);
    if ($r) {
        $message = '<p>Operation executed successfuly</p>';
    } else {
        $message = '<p>Operation did not execute because: '.mysqli_error($dbc);
        $message .= '<p>'.$q.'</p>';
    } 
}
?>

The echoes and print_r() below the query are for debugging purposes. The code that should echo $message is above the aforementioned code block (in my script) and looks like this:

<?php if(isset($message)) {echo $message;} ?>

Also, I tried using isset() for the $r variable and also changed the condition to $r !== false but that did not make a difference. When I just echo out $message without the isset() i get the obvious " Undefined variable: message in C:\xampp\htdocs\IMS\modify.php on line 47" error. My apologies if I'm missing something glaringly obvious. I did search beforehand but all the answers were too different from my situation and my knowledge of PHP is too small for me to be able to connect dots that are that far away, if you know what I mean.

EDIT: alright, I might as well put in the entire script. It's a bit all over the place, my apologies. The $id and $table variables do show as undefined after the submit button is pressed, could that have something to do with it?

<?php
error_reporting(E_ALL);
include('config/setup.php');

$id = $_GET['id'];
$table = $_GET['table'];

if ($table == "users") {
    header('Location: index.php');
    exit;
}

?>
<html>
<head>
    <title>Update</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>

    <div class="back">
        <a href="category_list.php">Back</a>
    </div>
    <div class="panel">
        <?php
        if(!isset($_POST['submitted'])) {
            $q = "SELECT name FROM $table WHERE id = $id";
                $r = mysqli_query($dbc, $q);
                $row = mysqli_fetch_assoc($r);
                if($table == "categories"){
                    $type = "category";
                } else if ($table == "products") {
                    $type = "product";
                }

            echo "<p>You are changing the properties of this ".$type.": ".$row['name']."</p>";
        }
        ?>
        <?php if(isset($message)) {echo $message;} ?>
        <form action="modify.php" method="POST">
            <label for="name">New name</label>
            <input type="text" class="form-control" id="name" name="name">
            <button type="submit">Submit</button>
            <input type="hidden" name="submitted" value="1">
            <input type="hidden" name="id" value="<?php echo $id; ?>">
            <input type="hidden" name="table" value="<?php echo $table; ?>">
        </form>
            <?php  
            if(isset($_POST['submitted'])) {
                $name = mysqli_real_escape_string($dbc, $_POST['name']);
                $q = "UPDATE ".$_POST['table']." SET name = '".$name."' WHERE id = ".$_POST['id'];
                $r = mysqli_query($dbc, $q);
                echo $r;

                print_r($_POST);
                echo mysqli_error($dbc);
                if ($r !== false) {
                    $message = '<p>Operation executed successfuly</p>';
                    } else {
                    $message = '<p>Operation did not execute because: '.mysqli_error($dbc);
                    $message .= '<p>'.$q.'</p>';
                    }
                }
            ?>
    </div>
</body>

EDIT2: Alright, I came up with a "fix" that kind of solves the problem, namely, I moved the if condition up before the echo of $message and changed the condition to isset($_POST['submitted']. This will have to do, I suppose. I guess I should read up more about the order of operations when processing submitted data and parsing PHP files in general, because I am quite confused as to why this "fix" even works...

iprit1915
  • 23
  • 4
  • 3
    `if(isset($_POST['submitted']) == 1)` that for one thing, isn't the way to use that method. – Funk Forty Niner Sep 28 '17 at 12:58
  • 2
    btw, `$q = "UPDATE ".$_POST['table']."` are you sure you really want to do that? You are asking for trouble here. – Funk Forty Niner Sep 28 '17 at 12:59
  • 2
    [Little Bobby](http://bobby-tables.com/) says **[you may be at risk for SQL Injection Attacks](https://stackoverflow.com/q/60174/)**. Learn about [Prepared Statements](https://en.wikipedia.org/wiki/Prepared_statement) with [parameterized queries](https://stackoverflow.com/a/4712113/5827005). I recommend `PDO`, which I [wrote a function for](http://paragoncds.com/grumpy/pdoquery/#function) to make it extremely easy, clean, and more secure than using non-parameterized queries. Also, [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) may help you choose between `MySQLi` and `PDO` – GrumpyCrouton Sep 28 '17 at 12:59
  • okay i removed the ==1. it executes, no problem there. i'll look into prepared statement with parametrized queries then. – iprit1915 Sep 28 '17 at 13:03
  • 1
    we need to see more code. Your question is starting to look unclear as to the POST arrays' values and their origins. Seems to me that you're probably getting that `$_POST['id']` from a previous query/in a populated form/hidden element and may need to be a GET for the id. – Funk Forty Niner Sep 28 '17 at 13:24

1 Answers1

1

This (conditional) statement is a false positive:

if(isset($_POST['submitted']) == 1)

What you need to do is either break them up into two separate statements:

if(isset($_POST['submitted']) && $_POST['submitted']== 1)

or just remove the ==1.

Your code is also open to a serious SQL injection. Updating a table and setting columns from user input is not safe at all.

At best, use a prepared statement.

However, please note that you cannot bind a table and/or a column should you want to convert that to a prepared statement method.

Therefore the following will fail (when using a PDO prepared statement as an example):

$q = "UPDATE :table SET :name = :name WHERE id = :id;

or

$q = "UPDATE ? SET name = :name WHERE id = :id;

Read the following about this method, where that cannot be used:

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • okay thanks i'll look into it, but that doesn't really help with the main problem... – iprit1915 Sep 28 '17 at 13:14
  • @iprit1915 what does error reporting show then? http://php.net/manual/en/function.error-reporting.php there may be errors you're not seeing. We don't know what values are going in the POST arrays. and did you not connect with the mysqli api? – Funk Forty Niner Sep 28 '17 at 13:17
  • @iprit1915 also, use `mysqli_affected_rows()` when querying with UPDATE http://php.net/manual/en/mysqli.affected-rows.php otherwise, you may be getting another false positive. – Funk Forty Niner Sep 28 '17 at 13:20
  • okay i set error_reporting(E_ALL) but that didn't make a difference in output. i am passing GET variables over to POST with the hidden inputs but that couldn't be the cause, surely... i mean the GET variables are gone after the submit button is pressed but i am passing POST variables in the query. i'm not sure what you mean connecting with the api, do you mean like is there a db connection? there is it is stored in the $dbc variable. – iprit1915 Sep 28 '17 at 13:33