Editing a PHP Page

Hi All

I’ve just finished creating and adding content to my pages, I now want to be able to edit them, I’m using the script shown below. The pages need to be edited through a form, I have included this script on here as well.

<?php
if (!isset($_POST['publish'])) {
	
	require_once ('../Connections/ilikeyours.php'); 
	
	
	$errors = array();
	
	if (empty($_POST['page_title'])) {
		$errors[] = 'You forgot to enter a page title';
		
	} else {
		$e = mysql_real_escape_string($dbc, trim($_POST['page_title']));
	}
	
	if (empty($_POST['page_content'])) {
		$errors[] = 'You forgot to add content to yor page';
		
	} else {
		$p = mysql_real_escape_string($dbc, trim($_POST['page_content']));
	}
		
	$q = "SELECT page_id FROM co_pages WHERE 
	(page_title='$e')";
	
	$r = mysql_query($dbc, $q);
	
	$t = mysql_num_rows($r);

	if ($t == 1) { 
		
		$q = "UPDATE co_pages SET page_title, page_content WHERE page_id=$row[0], page_content=$e AND page_content = $p ";
		
		$r = mysql_query($dbc, $q);
		
		if (mysql_affected_rows($dbc) == 1) {
			
			echo "Thank you";
			
		} else {
			
			echo "System error";
		}
		
		echo mysql_error($dbc);
		
	}
}
			?>
<!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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="edit_page.php" method="post">
      <p>Page Title:
        <span id="sprytextfield1">
        <input name="page_title" type="text" style="width:80%" id="page_title"  />
        <span class="textfieldRequiredMsg">A value is required.</span></span> </p>
      <p>
        <textarea class="content" name="page_content" style="width:90%" id="page_content">
	    </textarea>
        <br />
        <input type="submit" name="publish" value="Publish" />
        </p>
</form>
</body>
</html>

Please help :o)

What is the problem?

When I submit the form, it doesn’t do anything it just shows me a white page?

Add the following to the top of your php script…


&lt;?php
error_reporting(-1);
ini_set('display_errors', true);

Hi

Have just added this and I still have a white page, with nothing on it and the values in the MYSQL database have not change :o(.

Strange, I do notice however you’re passing the connection handle to the mysql_* functions, this isn’t needed, and certainly not in the wrong order. :wink:

Your initial ‘if’, you should be asking if ‘publish’ IS set, not NOT set.

Well that’s unravelled a few bits:

I’m now getting this:


Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in C:\\Program Files\\xampp\\htdocs\\admin\\edit_page.php on line 18

Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in C:\\Program Files\\xampp\\htdocs\\admin\\edit_page.php on line 25

Warning: mysql_query() expects parameter 2 to be resource, string given in C:\\Program Files\\xampp\\htdocs\\admin\\edit_page.php on line 31

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\\Program Files\\xampp\\htdocs\\admin\\edit_page.php on line 33

That’ll be because your passing the query string where you could be passing the database connection handle.

Your SQL is a bit off too fella, you want to be looking for something similar to this I believe.


<?php
error_reporting(-1);
ini_set('display_errors', true);

$errors = array();

foreach(array('page_title', 'page_content') as $parameter){
    if(false === isset($_POST[$parameter]) || true === empty($_POST[$parameter])){
        array_push($errors, sprintf('&#37;s is required but it is missing.', $parameter));
    }
}

if(0 === count($errors)){
    require_once ('../Connections/ilikeyours.php'); 
    $sql = sprintf(
        "UPDATE co_pages SET page_content = '%s' WHERE page_title = '%s' LIMIT 1",
        mysql_real_escape_string($_POST['page_content']),
        mysql_real_escape_string($_POST['page_title'])
    );
    if(false === ($result = mysql_query($sql))){
        array_push($errors, sprintf('Cannot update the database, no matching record found.'));
    }
}
?>

I’ve just tried using your code and I’m back at a white screen, lol

…and now?


<?php
error_reporting(-1);
ini_set('display_errors', true);

$errors = array();

foreach(array('page_title', 'page_content') as $parameter){
    if(false === isset($_POST[$parameter]) || true === empty($_POST[$parameter])){
        array_push($errors, sprintf('&#37;s is required but it is missing.', $parameter));
    }
}

if(0 === count($errors)){
    require_once ('../Connections/ilikeyours.php'); 
    $sql = sprintf(
        "UPDATE co_pages SET page_content = '%s' WHERE page_title = '%s' LIMIT 1",
        mysql_real_escape_string($_POST['page_content']),
        mysql_real_escape_string($_POST['page_title'])
    );
    if(false === ($result = mysql_query($sql))){
        array_push($errors, sprintf('Query Error: %s', mysql_error()));
    }elseif(1 !== mysql_affected_rows()){
        array_push($errors, 'No record found matching the title supplied.');
    }else{
        echo 'Page updated!';
        exit;
    }
}

if(0 < count($errors)){
    foreach($errors as $error){
        echo '<h4 style="color: red;">', $error, '</h4>';
    }
}
?>

Genius it works :o) thanks very much been working on that thing all afternoon. :D:D:D:D:D:D:D

Hey, no problem. Stay in touch. :wink: