klye_g
klye_g

Reputation: 1242

If post submit statement not getting past for each loop

Thanks in advance, here is what is going on:

if(isset($_POST['submit']))
{
$var = $_GET['var'];

foreach((array)$_POST['content'] as $area => $contents)
    {

        $result = 'UPDATE $table SET '.$area.' = "'.mysql_real_escape_string(stripslashes($contents)).'" WHERE var = "'.$_GET['var'].'";';
        mysql_query ($result);
    }


$query = 'UPDATE $table SET '.
            'title = "'.mysql_real_escape_string(stripslashes($_POST["title"])).'", '.
            'template = "'.mysql_real_escape_string($_POST["templateid"]).'", '.
            'description = "'.mysql_real_escape_string(stripslashes($_POST["description"])).'", '.
            'keywords = "'.mysql_real_escape_string(stripslashes($_POST["keywords"])).'" '.
            ' WHERE var = "'.$_GET['var'].'";';

mysql_query ($query);// or die(mysql_error());

}

I am unable to get the if statement to go beyond the for each loop. It will not update the database with the $query mysql statement. It simple updates the table for everything inside the for each loop and then exists. Do I need to do an If while or else if? And I tried looking up php conditional statements, is there such a thing as and statements? Such as: If post submit do this AND this? That is essentially what I am trying to do, If post submit, do for each loop AND do $query.

Let me know if you need more information.

Upvotes: 0

Views: 150

Answers (3)

Sander Mangel
Sander Mangel

Reputation: 305

try and add *ini_set('display_errors',1);* add the start of the script to ensure error ouput. And use *var_dump(mysql_error());* to display the mysql error of the second query.

And a small suggestion. do a switch on $area to ensure the valid fields are passed:

switch ($area) {
  case 'field1': $area='field1'; break;
  case 'field2': $area='field2'; break;
  default: echo 'No valid field'; continue; break;
}

good luck!

Upvotes: 0

Luke Cousins
Luke Cousins

Reputation: 2156

The table name wont be showing. Also add something to show the mysql error message to help debug:

$result = 'UPDATE '.$table.' SET '.$area.' = "'.mysql_real_escape_string(stripslashes($contents)).'" WHERE var = "'.$_GET['var'].'";';
mysql_query ($result) or die(mysql_error());

Upvotes: 0

Pascal
Pascal

Reputation: 16941

Well, is $_POST['content'] defined at all? In cases like this you may want to start debugging with print_r($_POST) to see all values that you get.

What you probably intended to do is:

foreach ($_POST as $key => $val)

Edit:
And you should really escape that $_GET['var'] as well as the $area in the loop!!! Escape ALL user input, always!!!

Upvotes: 1

Related Questions