Jump to content

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]
Link to comment
https://forums.phpfreaks.com/topic/23562-code-never-works/
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.
Link to comment
https://forums.phpfreaks.com/topic/23562-code-never-works/#findComment-106947
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
Link to comment
https://forums.phpfreaks.com/topic/23562-code-never-works/#findComment-107346
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.