-1

This is what i'm trying to do, I have a form in one page:

<form action="update.php" method="post" value="">
<textarea name="update">
<?php $page_name = "home"; $tablename = "pages"; $columnname = "content"; $redirect="formulier_home.php"; ?>
<?php $result = mysql_query("SELECT $columnname FROM $tablename WHERE page_name = '$page_name'"); ?>
<?php include("../includes/content_in_textarea.php"); ?>
</textarea>
<input type="submit" value="Publiceer" >        
</form>

and I update in another page:

<?php require_once("../includes/connection.php"); ?>
<?php $opvangen = $_POST['update'];?>
<?php $content = nl2br($opvangen); ?>
<?php mysql_query("UPDATE $tablename SET $columnname='$content' WHERE page_name='$page_name'") or die (mysql_error());
header("Location:$redirect"); ?>

It works fine if I don't use variables in the query like this:

<?php mysql_query("UPDATE $tablename SET $columnname='$content' WHERE page_name='$page_name'")or die (mysql_error());
header("Location:$redirect"); ?>

But I'm a little confused on how I will transfer the variables to the update page, I want to use the update page for all my columns and pages that I want to adjust.

SomeKittens
  • 38,868
  • 19
  • 114
  • 143

2 Answers2

1

If you want push variables from one site to other you need to use $_SESSION as @SomeKittens wrote or use a hidden form field. $_SESSION is better becouse the user can't overwrite it.

Put this in your form container file's first line:

<?php session_start(); ?>

The form:

<form action="update.php" method="post">
<textarea name="update">
<?php
  $_SESSION['page_name'] = "home";
  $_SESSION['tablename'] = "pages";
  $_SESSION['columnname'] = "content";
  $_SESSION['redirect'] ="formulier_home.php";
  $result = mysql_query("SELECT {$_SESSION['columnname']} FROM {$_SESSION['tablename']} WHERE page_name = '{$_SESSION['$page_name']}'");
  include("../includes/content_in_textarea.php");
?>
</textarea>
<input type="submit" value="Publiceer" >        
</form>

Your upload.php:

<?php
session_start(); // You need to put this in first line all the time when you use the $_SESSION global variable!
require_once("../includes/connection.php");
if (isset($_POST['update'])) {
 $opvangen = mysql_real_escape_string($_POST['update']);
 $content = nl2br($opvangen);
 $result = mysql_query("UPDATE {$_SESSION['tablename']} SET {$_SESSION['columnname']}='$content' WHERE page_name='{$_SESSION['page_name']}'") or die (mysql_error());
 header("Location:{$_SESSION['redirect']}");
}
?>

And that's all what you need.

  • Hey thanks you guys, I'm sorry but I prob did something stupid so half over my story was not in the post. the whole idea i had was having one page to update all the forms i have on my website, but the problem was I could not find a way to push the variable values from a for – peterpdekker Nov 03 '12 at 21:45
0

If you want to "transfer" variables from one page to another, you should use the $_SESSION superglobal.

As a side note, please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
SomeKittens
  • 38,868
  • 19
  • 114
  • 143
  • I couldn't understand the question very well, but there's also query strings: `header('Location: /some/place?var1='.$value);` etc.. – Wesley Murch Nov 03 '12 at 14:43