Jump to content

Recommended Posts

i have gained a little knowledge in php and mySQL now the problem i have is i am trying to develop a code to edit articles now almost the whole code works apart from the submit part of the code i think? when i log-in to edit the Articles it displays the data allows me to edit the data when i click submit it should post the new querys to mySQL insted it just takes me back to the editArticle.php page and does not edit the article, Can anybody help please

 

Here is the code

 

<?php

require_once("../includes/Sentry.php");

 

$sentry = new Sentry();

if ($HTTP_POST_VARS['user'] != ''){

$sentry->checkLogin($HTTP_POST_VARS['user'],$HTTP_POST_VARS['pass'],4,'welcome.php','failed.php');

}

 

if ($HTTP_GET_VARS['action'] == 'logout'){

if ($sentry->logout()){

echo '<center>You have been logged out</center><br>';

}

}

?>

<?

// Require the classes

require_once('../includes/DbConnector.php');

require_once('../includes/Validator.php');

 

// Create an object (instance) of the DbConnector and Validator

$connector = new DbConnector();

$validator = new Validator();

 

//select which database you want to edit

mysql_select_db("web176-contentms");

 

//If cmd has not been initialized

if(!isset($cmd))

{

//display all the thearticle

$result = mysql_query("select * from cmsarticles order by id");

 

//run the while loop that grabs all the thearticle scripts

while($r=mysql_fetch_array($result))

{

//grab the title and the ID of the thearticle

$title=$r["title"];//take out the title

$id=$r["ID"];//take out the id

 

//make the title a link

echo "<a href='editArticle.php?cmd=edit&id=$id'>$title - Edit</a>";

 

echo "<br>";

}

}

?>

<?

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")

{

if (!isset($_POST["Submit"]))

{

$id = $_GET["id"];

$sql = "SELECT * FROM cmsarticles WHERE id=$id";

$result = mysql_query($sql);

$myrow = mysql_fetch_array($result);

?>

 

<form action="editArticle.php" method="post">

<input type=hidden name="ID" value="<?php echo $myrow["id"] ?>">

 

Title:<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["title"] ?>" SIZE=30><br>

Message:<TEXTAREA NAME="thearticle" ROWS=10 COLS=30><? echo $myrow["thearticle"] ?></TEXTAREA>

<br>

 

<input type="hidden" name="cmd" value="edit">

 

<input type="Submit" name="Submit" value="Submit">

 

</form>

 

<? } ?>

<?

if ($_POST["$Submit"])

{

$title = $_POST["title"];

$thearticle = $_POST["thearticle"];

 

$sql = "UPDATE cmsarticles SET title='$title',thearticle='$thearticle' WHERE id=$id";

//replace theArticles with your table name above

$result = mysql_query($sql);

echo "Thank you! Information updated.";

}

}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/
Share on other sites

I would take this form and make it post to another page like this:

 

<form action="article_edited.php" method="post">
<input type=hidden name="ID" value="<?php echo $myrow["id"] ?>">

Title:<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["title"] ?>" SIZE=30><br>
Message:<TEXTAREA NAME="thearticle" ROWS=10 COLS=30><? echo $myrow["thearticle"] ?></TEXTAREA>
<br>

<input type="hidden" name="cmd" value="edit">

<input type="Submit" name="Submit" value="Submit">

</form>

 

then on the article_edited.php page you would then put this:

 

<?
if ($_POST["$Submit"])
{
$id = $_POST["id"];
$title = $_POST["title"];
$thearticle = $_POST["thearticle"];

$sql = "UPDATE cmsarticles SET title='$title',thearticle='$thearticle' WHERE id=$id";
//replace theArticles with your table name above
$result = mysql_query($sql);
echo "Thank you! Information updated.";
}
}

 

of course this is just a sample of what you would do and you would have to do the mysql connectiosn again on this page. hopefully that helps.

i have noticed that in the last bit of code

<?
if ($_POST["[b]$[/b]Submit"]) <-------Here should only be  
if ($_POST["Submit"]) 

 

now i am having a problem with this part i beleve

 

 $sql="UPDATE cmsarticles SET thearticle='$thearticle' WHERE ID =$id";
$result = mysql_query($sql);
echo "Thank you! Information updated.";

as i get the message Thank you! Information updated. but does not update the data

any help would be great

the code looks like

 

      <? 
// Require the classes
require_once('../includes/DbConnector.php');
require_once('../includes/Validator.php');

// Create an object (instance) of the DbConnector and Validator
$connector = new DbConnector();
$validator = new Validator();

//select which database you want to edit
mysql_select_db("web176-contentms"); 

//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the thearticle
   $result = mysql_query("select * from cmsarticles order by id"); 
   
   //run the while loop that grabs all the thearticle scripts
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the thearticle
      $title=$r["title"];//take out the title
      $id=$r["ID"];//take out the id
       
      echo "<br>";
    }
}
?>
      <?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["Submit"]))
   {
      $id = $_GET["id"];
      $sql = "SELECT * FROM cmsarticles WHERE id=$id";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
      </span></span>
      <form action="editArticle.php" method="POST">
  <input type=hidden name="ID" value="<?php echo $myrow["id"] ?>">
      <TEXTAREA NAME="thearticle" COLS=40 ROWS=10 class="quoteEdit"><? echo $myrow["thearticle"] ?></TEXTAREA>
      <br>
   
      <input type="hidden" name="cmd" value="edit">
   
      <input name="Submit" type="Submit" id="Submit" value="Edit">
      </form>
   
   <? } ?>
   <?
   if ($_POST["Submit"])
   {
 $thearticle = $_POST["thearticle"];

 $sql = "UPDATE cmsarticles SET thearticle='$thearticle' WHERE ID =$id";
//replace thearticle with your table name above	
  
 $result = mysql_query($sql);
 $result = mysql_query($sql) or die(mysql_error());
     
      echo "Thank you! Information updated.";
  
   }
}
?>

i was unable to place it onto 2 pages just gives loads of errors

Try this:

 

<?php
// Require the classes
require_once('../includes/DbConnector.php');
require_once('../includes/Validator.php');

//get the id
$id = $_GET["id"];

// Create an object (instance) of the DbConnector and Validator
$connector = new DbConnector();
$validator = new Validator();

//select which database you want to edit
mysql_select_db("web176-contentms"); 

//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the thearticle
   $result = mysql_query("select * from cmsarticles order by id"); 
   
   //run the while loop that grabs all the thearticle scripts
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the thearticle
      $title=$r["title"];//take out the title
      $id=$r["ID"];//take out the id
       
      echo "<br>";
    }
}

if($_GET["cmd"]=="edit")
{
   if (!isset($_POST["Submit"]))
   {
      $sql = "SELECT * FROM cmsarticles WHERE id='$id'";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
      </span></span>
      <form action="editArticle.php" method="POST">
  <input type=hidden name="ID" value="<?php echo $id; ?>">
      <TEXTAREA NAME="thearticle" COLS=40 ROWS=10 class="quoteEdit"><? echo $myrow["thearticle"] ?></TEXTAREA>
      <br>
   
      <input type="hidden" name="cmd" value="edit">
   
      <input name="Submit" type="Submit" id="Submit" value="Edit">
      </form>
   
   <?php } 
   if ($_POST["Submit"])
   {
$thearticle = $_POST["thearticle"];

$sql = "UPDATE cmsarticles SET thearticle='$thearticle' WHERE ID ='$id'";
//replace thearticle with your table name above	

$result = mysql_query($sql) or die(mysql_error());
     
      echo "Thank you! Information updated.";
  
   }
}
?>

 

I cleaned up the code a little bit. You should use <?php instead of <? because it makes it more universal. Secondly you only need to end php ?> when you are switching to html. Also I changed it to get the id without requiring the cmd to be edit. it does not necessarily need to be set to get the id and set it to a variable. if you get any errors please let me know.

ok without the

$_POST["cmd"]=="edit")

i was just getting returned to the editArticle.php with no change so i put that back in and now when i submit an update it changes all the "thrarticle" to my update ? so instead of it updating id 3 it changes id 1,2,3 to all the same article getting closer to the fix tho

The other problem i see is in the last query (sorry just noticed it):

 

if ($_POST["Submit"])
   {
$thearticle = $_POST["thearticle"];

$sql = "UPDATE cmsarticles SET thearticle='$thearticle' WHERE ID ='$id'";
//replace thearticle with your table name above	

$result = mysql_query($sql) or die(mysql_error());
     
      echo "Thank you! Information updated.";
  
   }

 

should be:

 

if ($_POST["Submit"])
   {
$thearticle = $_POST["thearticle"];

$sql = "UPDATE cmsarticles SET thearticle='$thearticle' WHERE id ='$id'";
//replace thearticle with your table name above	

$result = mysql_query($sql) or die(mysql_error());
     
      echo "Thank you! Information updated.";
  
   }

 

the only thing I did was change the ID to id in the query. the fields are case sensitive.

try this:

 

<?php
// Require the classes
require_once('../includes/DbConnector.php');
require_once('../includes/Validator.php');

//get the id
$id = $_GET["id"];
$cmd = $_GET["cmd"];


// Create an object (instance) of the DbConnector and Validator
$connector = new DbConnector();
$validator = new Validator();

//select which database you want to edit
mysql_select_db("web176-contentms"); 

//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the thearticle
   $result = mysql_query("select * from cmsarticles order by id"); 
   
   //run the while loop that grabs all the thearticle scripts
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the thearticle
      $title=$r["title"];//take out the title
      $id=$r["ID"];//take out the id
       
      echo "<br>";
    }
}

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["Submit"]))
   {
      $sql = "SELECT * FROM cmsarticles WHERE id='$id'";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
      </span></span>
      <form action="editArticle.php" method="POST">
  <input type=hidden name="ID" value="<?php echo $id; ?>">
      <TEXTAREA NAME="thearticle" COLS=40 ROWS=10 class="quoteEdit"><? echo $myrow["thearticle"] ?></TEXTAREA>
      <br>
   
      <input type="hidden" name="cmd" value="edit">
   
      <input name="Submit" type="Submit" id="Submit" value="Edit">
      </form>
   
   <?php } 
   if ($_POST["Submit"])
   {
$thearticle = $_POST["thearticle"];

$sql = "UPDATE cmsarticles SET thearticle='$thearticle' WHERE id='$id'";
//replace thearticle with your table name above	

$result = mysql_query($sql) or die(mysql_error());
     
      echo "Thank you! Information updated.";
  
   }
}
?>



 

let meknow if you get any errors also let me know what the url looks like when you first visit the page.

dont get any errors when i go to my editing page the url looks like this http://mydomain.co.uk/cmsadmin/editArticle.php?cmd=edit&id=1

when i click submit/edit it sends me to

http://mydomain.co.uk/cmsadmin/editArticle.php with a message of Thank you! Information updated. no errors  ???

the thing is it only updates id=3 or the highest id i should say as 3 is the highest id in my table

 

ok I think I have got it now try this:

 

<?php
// Require the classes
require_once('../includes/DbConnector.php');
require_once('../includes/Validator.php');

//get the id
$id = $_GET["id"];
$cmd = $_GET["cmd"];


// Create an object (instance) of the DbConnector and Validator
$connector = new DbConnector();
$validator = new Validator();

//select which database you want to edit
mysql_select_db("web176-contentms"); 

//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the thearticle
   $result = mysql_query("select * from cmsarticles order by id"); 
   
   //run the while loop that grabs all the thearticle scripts
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the thearticle
      $title=$r["title"];//take out the title
      $id=$r["ID"];//take out the id
       
      echo "<br>";
    }
}

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["Submit"]))
   {
      $sql = "SELECT * FROM cmsarticles WHERE id='$id'";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
      </span></span>
      <form action="editArticle.php?cmd=<?php echo $cmd; ?>&ID=<?php echo $id; ?>" method="POST">
        <TEXTAREA NAME="thearticle" COLS=40 ROWS=10 class="quoteEdit"><? echo $myrow["thearticle"] ?></TEXTAREA>
      <br>
   
      <input type="hidden" name="cmd" value="edit">
   
      <input name="Submit" type="Submit" id="Submit" value="Edit">
      </form>
   
   <?php } 
   if ($_POST["Submit"])
   {
$thearticle = $_POST["thearticle"];

$sql = "UPDATE cmsarticles SET thearticle='$thearticle' WHERE id='$id'";
//replace thearticle with your table name above	

$result = mysql_query($sql) or die(mysql_error());
     
      echo "Thank you! Information updated.";
  
   }
}
?>

 

the code was not being passed to the second page. well it was but by a post and you were not getting if from the post so I changed it so that when it posts it posts the url.

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.