...

View Full Version : Can't add new pages



RDLyTN
01-14-2012, 03:37 PM
Hi,

When I try to add a new page by clicking on a button, it won't work. The new page is added in the database but not in my menu. What's the problem?

----> CREATE_PAGE.PHP <----


<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>

<?php
$errors = array();

$required_fields = array('menu_name', 'position', 'visible', 'content');
foreach($required_fields as $fieldname) {
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
$errors[] = $fieldname;
}
}

$fields_with_lengths = array('menu_name' =>30);
foreach($fields_with_lengths as $fieldname => $maxlength) {
if(strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
$errors[] = $fieldname;
}
}

if(!empty($errors)) {
redirect_to('new_page.php');
}
?>

<?php
$menu_name = mysql_prep($_POST['menu_name']);
$position = mysql_prep($_POST['position']);
$visible = mysql_prep($_POST['visible']);
$content = mysql_prep($_POST['content']);
?>

<?php
$query = "INSERT INTO pages (
menu_name, position, visible, content
) VALUES (
'{$menu_name}', {$position}, {$visible}, '{$content}'
)";
$result = mysql_query($query, $connection);
if($result) {
redirect_to('content.php');
} else {
echo "<p>Page creation failed.</p>";
echo "<p>" . mysql_error() . "</p>";
}
?>

<?php mysql_close($connection); ?>


----> NEW_PAGE.PHP <----


<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/connection.php"); ?>
<?php find_selected_page(); ?>
<?php include("includes/header.php"); ?>
<?php if(!isset($new_page)) { $new_page = false; } ?>
<table id="structure">
<tr>
<td id="navigation">&nbsp;
<?php echo navigation($sel_subject, $sel_page); ?>
</td>
<td id="page">
<h2>Add new page</h2>
<?php if(!empty($message)) { echo "<p class=\"message\">" . $message .
"</p>"; } ?>
<?php if(!empty($errors)) { display_errros($errors); } ?>
<form action="create_page.php?subj=<?php echo $sel_page['id']; ?>" method="post">
<?php $new_page = true; ?>
<p>Page name:
<input type="text" name="menu_name" value="" id="menu_name" />
</p>
<p>Position:
<select name="position">
<?php
if(!$new_page) {
$page_set = get_pages_for_subject($sel_page['subject_id']);
$page_count = mysql_num_rows($page_set);
} else {
$page_set = get_pages_for_subject($sel_subject['id']);
$page_count = mysql_num_rows($page_set) + 1;
}
for($count=1; $count <= $page_count; $count++) {
echo "<option value=\"{$count}\"";
if($sel_page['position'] == $count) { echo " selected"; }
echo ">{$count}</option>";
}
?>
</select>
</p>
<p>Visible:
<input type="radio" name="visible" value="0"<?php if($sel_page['visible'] == 0) { echo " checked"; }
?> /> No
&nbsp;
<input type="radio" name="visible" value="1"<?php
if($sel_page['visible'] == 1) { echo " checked"; }
?> /> Yes
</p>
<p>Content:<br />
<textarea name="content" rows="20" cols="80"><?php echo $sel_page['content'];
?></textarea>
</p>
<input type="submit" name="submit" value"Add Page" />
</form>
<br />
<a href="edit_subject.php?subj=<?php echo $sel_subject['id'];
?>">Cancel</a><br />
</td>
</tr>
</table>
<?php require("includes/footer.php"); ?>

12k
01-14-2012, 04:25 PM
Any chance you can post the code where its pulling from the database to fetch the menu?

RDLyTN
01-14-2012, 04:28 PM
Any chance you can post the code where its pulling from the database to fetch the menu?




function get_pages_for_subject($subject_id) {
global $connection;
$query = "SELECT *
FROM pages
WHERE subject_id = {$subject_id}
ORDER BY position ASC";
$page_set = mysql_query($query, $connection);
confirm_query($page_set);
return $page_set;
}

function navigation($sel_subject, $sel_page) {
$output = "<ul class=\"subjects\">";
$subject_set = get_all_subjects();

while($subject = mysql_fetch_array($subject_set)) {
$output .= "<li";
if ($subject["id"] == $sel_subject['id']) { $output .= "
class=\"selected\""; }
$output .= "><a href=\"edit_subject.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";
$page_set = get_pages_for_subject($subject["id"]);
$output .= "<ul class=\"pages\">";

while($page = mysql_fetch_array($page_set)) {
$output .= "<li";
if ($page["id"] == $sel_page['id']) { $output .= "
class=\"selected\""; }
$output .= "><a href=\"edit_page.php?page=" . urlencode($page["id"]) .
"\">{$page["menu_name"]}</a></li>";
}
$output .= "</ul>";
}
$output .= "</ul>";
return $output;
}

12k
01-14-2012, 05:00 PM
Hmm, it appears when your creating your page, your not setting the "subject_id" on the mysql insertion here:



$query = "INSERT INTO pages (
menu_name, position, visible, content
) VALUES (
'{$menu_name}', {$position}, {$visible}, '{$content}'
)";


In your create_page.php

Then you are trying to access it here:



function get_pages_for_subject($subject_id) {
global $connection;
$query = "SELECT *
FROM pages
WHERE subject_id = {$subject_id}
ORDER BY position ASC";
$page_set = mysql_query($query, $connection);
confirm_query($page_set);
return $page_set;
}

RDLyTN
01-14-2012, 05:21 PM
Hmm, it appears when your creating your page, your not setting the "subject_id" on the mysql insertion here:



$query = "INSERT INTO pages (
menu_name, position, visible, content
) VALUES (
'{$menu_name}', {$position}, {$visible}, '{$content}'
)";


In your create_page.php

Then you are trying to access it here:



function get_pages_for_subject($subject_id) {
global $connection;
$query = "SELECT *
FROM pages
WHERE subject_id = {$subject_id}
ORDER BY position ASC";
$page_set = mysql_query($query, $connection);
confirm_query($page_set);
return $page_set;
}


How can I solve this? I just started with php :)

12k
01-14-2012, 05:25 PM
You will have to change your insertion query.

$query = "INSERT INTO pages (
menu_name, position, visible, content, subject_id
) VALUES (
'{$menu_name}', {$position}, {$visible}, '{$content}', '{VALUE OF SUBJECT ID}'
)";

I highlighted in red what need to be put there. That should be all you need. The old page won't work because it was inserting into mysql incorrectly. So you will have to create a new one.

RDLyTN
01-14-2012, 05:54 PM
You will have to change your insertion query.

$query = "INSERT INTO pages (
menu_name, position, visible, content, subject_id
) VALUES (
'{$menu_name}', {$position}, {$visible}, '{$content}', '{VALUE OF SUBJECT ID}'
)";

I highlighted in red what need to be put there. That should be all you need. The old page won't work because it was inserting into mysql incorrectly. So you will have to create a new one.

I tried that, but it is still not working.

12k
01-15-2012, 02:47 AM
sorry i dont get on here much. Feel free to add me to msn: twelve_k@live.com



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum