Forum Moderators: coopster

Message Too Old, No Replies

PHP Error

Undefined index

         

onceumy

6:38 pm on Jul 13, 2010 (gmt 0)

10+ Year Member



hey peeps, i am a beginner to phh
i have been getting this error

Notice: Undefined index: subj in C:\wamp\www\widget_corp\edit_subject.php on line 6

Fatal error: Call to undefined function redirect_to() in C:\wamp\www\widget_corp\edit_subject.php on line 7

CODE:

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

<?php

if (intval($_GET['subj']) == 0) {
redirect_to("content.php");
}

if (isset($_POST['submit'])) {
$errors = array();

$required_fields = array('menu_name', 'position', 'visible');
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)) {
// Perform Update
$id = mysql_prep($_GET['subj']);
$menu_name = mysql_prep($_POST['menu_name']);
$position = mysql_prep($_POST['position']);
$visible = mysql_prep($_POST['visible']);

$query = "UPDATE subjects SET
menu_name = '{$menu_name}',
position = {$position},
visible = {$visible}
WHERE id = {$id}";

$result = mysql_query($query, $connection);
if (mysql_affected_rows() == 1) {
// Success
} else {
// Failed
}

} else {
// Errors occurred
}




} //
?>
<?php find_selected_page();?>
<?php include("includes/header.php"); ?>



<div id="navigation">

<?php echo navigation($sel_subject, $sel_page); ?>

</div>

<div id="content">
<h2>Edit Subject: <?php echo $sel_subject['menu_name']; ?></h2>
<form action="edit_subject.php?subj=<?php urlencode($sel_subject['id']); ?>" method="get">
<p>Subject name:
<input type="text" name="menu_name" value="
<?php echo $sel_subject['menu_name']; ?>" id="menu_name" />
</p>
<p>Position:
<select name="position">

<?php
$subject_set = get_all_subjects();
$subject_count = mysql_num_rows($subject_set);
echo $subject_count;

// $subject_count + 1 b/c we are adding a subject
for($count=1; $count <= $subject_count+1; $count++) {
echo "<option value=\"{$count}\"";

if ($sel_subject['position'] == $count){
echo " selected";

}
echo ">{$count}</option>";

}

?>
</select>
</p>

<p>Visible:
<input type="radio" name="visible" value="0" <?php
if($sel_subject['visible'] == 0) { echo " checked"; }
?> /> No
&nbsp;
<input type="radio" name="visible" value="1" <?php
if($sel_subject['visible'] == 1) {echo " checked"; } ?>
/> Yes
</p>
<input type="submit" name="submit" value="Edit Subject" />
</form>

<br />
<a href="content.php">Cancel</a>

</div>

<?php require("includes/footer.php"); ?>

i have tried changing the form method from post to get but it still doesn't work. Please help

Matthew1980

7:36 pm on Jul 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there onceumy,

Greetings and welcome to the forum: [webmasterworld.com ] :)

After skimming through your code I can see this:-

<form action="edit_subject.php?subj=<?php urlencode($sel_subject['id']); ?>" method="get">


Change the bolded text to POST, and then try again, but this time at the top of the code in the receiver file (the file you reference in the action="" attribute) put this to debug what's being sent & whats set:-

<?php
if(isset($_POST['submit']) && ($_POST['submit'] == "Edit Subject")){
echo "<pre>";
print_r($_GET);
print_r($_POST);
echo "</pre>";
exit;
}


One you do this, it will echo to screen everything that has been set when the submit, hopefully the 'get' array will have the querystring key set 'subj' if not there is a problem.

Other than that, this strikes me:-

if (intval($_GET['subj']) == 0) {
redirect_to("content.php");
}


redirect_to is not in the standard library of functions, unless you have created it? Alternatively use header("location: yourfile.php"); which will do the job.

personally though I would do this:-

if (isset($_GET['subj']) && is_numeric($_GET['subj']) && ($_GET['subj'] == 0)) {
header("location: content.php");//full url paths are favoured in this type of situation
exit; //always good to kill the script there
}


As I say, only skimmed the code and commented on what stood out.

Hope this helps a little :)

Cheers,
MRb

onceumy

1:12 pm on Jul 14, 2010 (gmt 0)

10+ Year Member



thanks ur debugging idea helped alot. I've fixed that error. I'm presently facing an error at the very bottom of the code i.e the last line. if i comment out the last line it jumps to the one before that. very funny parse error i just cant c what i left out...

onceumy

1:15 pm on Jul 14, 2010 (gmt 0)

10+ Year Member



i also changed replaced redirect_to with header().thanks again!

onceumy

1:34 pm on Jul 14, 2010 (gmt 0)

10+ Year Member



i fixed it - was a closing brace - } but now my database query is failing - can't wait for these errors to stop

Matthew1980

2:37 pm on Jul 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there onceumy,

Mysql query not working? try this then:-

$query = "UPDATE `subjects` SET `menu_name` = '".$menu_name."', `position` = '".$position."',
`visible` = '".$visible."' WHERE `id` = '".$id."' ";



you hadn't quoted the values being inserted (position = $position) therefore the syntax was slightly incorrect, what I have quoted there though is correct in format, so *should* function.

So long as the query is being populated correctly this will function as you want, other wise you may have blank values being inserted...

Cheers,
MRb

onceumy

3:43 pm on Jul 15, 2010 (gmt 0)

10+ Year Member



many cheers m8 - i've tried everything still getting this error when i submit my form.

Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1

rocknbil

4:27 pm on Jul 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, none of the code here has a limit clause so it's something you introduced, probably missing a space before limit?

A side note, if you're doing anything like this,

update . . . where id=$id

select . . . . where id=$id

adding " limit 1" may give you the impression it's safer, but it's not necessary and could potentially hide other problems. If you leave limit 1 off of the statement, it would alert you to any logic or programming problems you may have, "id" should always point to a unique record. If it doesn't, you have a problem.