Jump to content

Archived

This topic is now archived and is closed to further replies.

Ninjakreborn

code never works

Recommended Posts

I have tried to replicate this a hundred times, but it never works, so I always leave it as is, it's always related to the cms.
Every time I try to replicate something like this, it's suppose to show an edit form, allow them to edit, submit, then say it was successfull but still reshow the form everytime with the new information, but it NEVER works, I have tried this in hundreds of ways, within the past few months, every other time I need to build a cms I try to get it to work right, but it never does, it is confusing, any advice would be appreciated.

[code]<?php
session_start();
// db connection info
mysql_connect("localhost", "#####", "#####");
mysql_select_db("hasbadse_hbservice");
if ($_SESSION['controller'] == true) {
if ($_POST['status'] == "edit") {
$id = mysql_real_escape_string($_POST['id']);
$header = mysql_real_escape_string($_POST['header']);
$text = mysql_real_escape_string($_POST['text']);
$edit = "UPDATE usercms SET header = '$header', text = '$text' WHERE id = '$id';";
if (mysql_query($edit)) {
echo "Edited Successfully.<br />";
}else {
echo "There was a problem editing this entry.<br />";
}

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Edit Your File Here</title>
</head>
<body>
<?php
if (isset($_GET['id']) || isset($id)) {
$id = mysql_real_escape_string($_GET['id']);
$select = "SELECT * FROM usercms WHERE id = '$id';";
$query = mysql_query($select);
if ($row = mysql_fetch_array($query)) {
?>
<form name="edit" id="edit" action="usercpaneledit.php" method="post">
<label for="header">Header:</label><br />
<input name="header" id="header" type="text" maxlength="100" value="<?php echo $row['header']; ?>" />
<br />
<label for="text">Text:</label><br />
<textarea name="text" id="text" rows="20" cols="20"><?php echo $row['text']; ?></textarea>
<br />
<input name="status" value="edit" type="hidden" />
<input name="id" value="<?php echo $row['id']; ?>" type="hidden" />
<input name="submit" id="submit" type="submit" value="Continue" />
</form>
<?php
}
?>
</body>
</html>
<?php
}else {
echo "You did not come from the proper page.<br />";
}

}else {
header("Location: index.php");
}
?>[/code]

Share this post


Link to post
Share on other sites
What parameters does this page get passed?

Please provide all methods... GET/POST, COOKIES or SESSION.

Regards
Huggie

Share this post


Link to post
Share on other sites
Ok, anytime I do this kind of page, there are always the session variables for login, in this case they are
$_SESSION['username'] = $username;
$_SESSION['status'] = "logged";
$_SESSION['userid'] = $row['id'];
$_SESSION['subdomainname'] = $row['subdomainname'];
$_SESSION['email'] = $row['email'];
$_SESSION['controller'] = true;
Then it also get's past get statements from the previous page, in this case it's
from a link, which Is the id
then when the post get's those variables are passed BACK to the same page, to start up the edit.

Everything works, every time I do this, everything alway's work's properly.
Except the bottom portion ALWAYS disappears when it does a successful one, this isn't the only way I have tried either, every time I build one I try to get the same functionality, it's like when it runs a database call php kills everything below it, it's aggravating.

Share this post


Link to post
Share on other sites
Interesting use of both POST and GET, but not a problem... We can sort this for you.

There's a few bits where I think you're over complicating it a bit, so we'll see if we can clear those up too.

I'll get on the case now.

Regards
Huggie

Share this post


Link to post
Share on other sites
Businessman,

Back up your old code and then give this a try.  There's no real enhancements to the code, but it should work.

[code]<?php
// Start the session
session_start();

// db connection info
mysql_connect("localhost", "#####", "#####");
mysql_select_db("hasbadse_hbservice");

// Redirect if they aren't allowed to be here
if ($_SESSION['controller'] != true){
  header("Location: index.php");
}

// If we don't have an ID then produce an error (I don't like this, but it's what you had it doing)
if (!isset($_REQUEST['id']){
  echo "You have arrived here from the wrong page\n";
  die();
}

// If the status is edit then update the table
if ($_POST['status'] == "edit") {
  $id = mysql_real_escape_string($_POST['id']);
  $header = mysql_real_escape_string($_POST['header']);
  $text = mysql_real_escape_string($_POST['text']);
  $edit = "UPDATE usercms SET header = '$header', text = '$text' WHERE id = '$id'";
  if (mysql_query($edit)){
      echo "Edited Successfully.<br />";
  }
  else {
      echo "There was a problem editing this entry.<br />";
  }
}

// Select the data to display in the form
$id = mysql_real_escape_string($_REQUEST['id']);
$select = "SELECT * FROM usercms WHERE id = '$id'";
$query = mysql_query($select);
$row = mysql_fetch_array($query);

// Echo the form
echo <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Edit Your File Here</title>
</head>
<body>
  <form name="edit" id="edit" action="{$_SERVER['PHP_SELF']}" method="POST">
  <label for="header">Header:</label><br />
  <input type="text" name="header" id="header" maxlength="100" value="{$row['header']}" /><br />
  <label for="text">Text:</label><br />
  <textarea name="text" id="text" rows="20" cols="20">{$row['text']}</textarea><br />
  <input type="hidden" name="status" value="edit" />
  <input type="hidden" name="id" value="{$row['id']}" />
  <input type="submit" name="submit" id="submit" value="Continue" />
  </form>
</body>
</html>
HTML;
?>[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.