Mysql update issue!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Mysql update issue!

Post 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
Last edited by cap2cap10 on Thu May 19, 2011 8:32 pm, edited 1 time in total.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Mysql update issue!

Post by mikosiko »

what do you get if you echo $response immediately after its creation?
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: Mysql update issue!

Post 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
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Mysql update issue!

Post 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
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: Mysql update issue!

Post 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
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Mysql update issue!

Post 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.
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: Mysql update issue!

Post 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
Post Reply