Page 1 of 1

Mysql update issue!

Posted: Wed May 18, 2011 6:45 pm
by cap2cap10
Here I am again, members of the php technorati, with another strange issue. This time it is my update script. Here is the code:

Code: Select all

if (!empty($_POST['submit_1']))
{
$candidateID = $_POST['candidateID'];
require 'open_db.php';
for ($i = 1; $i <= 16; $i++) {
    $word = 'resp_';
    $value = $word.$i;
    $response = filter_input(INPUT_POST, $value, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);

    if (!empty($response)) {
        mysql_query("UPDATE prelim_db SET $value = '" . mysql_real_escape_string($response) . "'
WHERE candidateID = '". mysql_real_escape_string($candidateID) . "' ") or die('Query failed: ' . mysql_error());
    }
}
mysql_query("UPDATE prelim_db SET q_count = q_count + 1 WHERE candidateID = '$candidateID'")or die('Query failed: ' . mysql_error());

$prelim_db = mysql_query("SELECT * FROM prelim_db WHERE candidateID = '$candidateID'")
or die(mysql_error());
$row4 = mysql_fetch_array( $prelim_db );
}
mysql_close();
Now, what seems to be occurring is this:
1- it skips the 2nd response update!??
2-it takes the third response and places it in the 2nd response field in the database? :banghead:
***Really screws up my questionaire form!***
What the on earth is going on?!!
Does !empty cause this phenomenon ? Some one please enlighten me as to the error of my ways.

Thanks in advance,

Batoe

Re: Mysql update issue!

Posted: Thu May 19, 2011 8:37 am
by mikosiko
what do you get if you echo $response immediately after its creation?

Re: Mysql update issue!

Posted: Thu May 19, 2011 3:10 pm
by cap2cap10
Ok, I stuck echo $response with a time delay in this scrip and I do not see anything on the page for 10 second delay!????
I still does the above though even though the echo shows nothing!???

Perplexed Batoe

Re: Mysql update issue!

Posted: Fri May 20, 2011 9:48 am
by mikosiko
if your echo $response came empty then the UPDATE in this part of the code that you posted:

Code: Select all

for ($i = 1; $i <= 16; $i++) {
    $word = 'resp_';
    $value = $word.$i;
    $response = filter_input(INPUT_POST, $value, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);

    if (!empty($response)) {
        mysql_query("UPDATE prelim_db SET $value = '" . mysql_real_escape_string($response) . "'
WHERE candidateID = '". mysql_real_escape_string($candidateID) . "' ") or die('Query failed: ' . mysql_error());
    }
}
is never executing, therefore your problem(s) are in some other place in your code... also I recommend you to read again what the filter_input() does and how to use it. In your code $value is NOT a POSTED variable.
from the filter_input() definition:
filter_input — Gets a specific external variable by name and optionally filters it

Re: Mysql update issue!

Posted: Fri May 20, 2011 10:38 am
by cap2cap10
Ok, I see where you are coming from, but remember this code works for 14 updates. It seems to discriminate against the # 2 variable. why would it behave this way? It leaves the query open until the next update comes and places it into the resp_2 field. Value still represents the name of the variable that corresponds to the name in the database. Resp_1 works perfectly. There has to be a logical reason why it is skipping # 2 variable($i=2).
I need a vacation. I am going to the gym.

Batoe

Re: Mysql update issue!

Posted: Fri May 20, 2011 12:59 pm
by Jade
It's checking to make sure your $response variable isn't empty. It looks like whatever you're using for your second update is failing the filter_input function because there is no response returned and therefore no update.

Re: Mysql update issue!

Posted: Sat May 21, 2011 7:16 am
by cap2cap10
Thanks guys, the error was in the form. An array that provided the field name was being generated too early before it was supposed to be modified to the current question #.Thanks for the heads up.

Batoe