-1

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

hello everyone i am getting a problem while updating my record..when i click the edit button it doesnot update the record but give following errors to all post variables.. i would really appreciate any kind of comments or help...

this is courses-edit.php

<?php include("../includes/config.php"); ?> <?php if ($_SESSION["isteacher"]) { $con=mysql_connect($dbserver,$dbusername,$dbpassword); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname, $con); $courseid=$_GET["id"]; $result = mysql_query("SELECT * FROM courses WHERE (id='".$courseid."')"); while($row = mysql_fetch_array($result)) { $id=$row['id']; $title = $row['title']; $des = $row['description']; $subjectid = $row['subjects-id']; } mysql_close($con); ?> <script type="text/javascript"> $(function() { $("form").validity(function() { $("#title") .require("This Field Must Be Filled!!") }); }); </script> <?php include("../includes/header.php"); ?> <?php include("includes/nav.php"); ?> <div id="maincontent"> <div class="span-24 last"> <div id="breadcrumbs"> <a href="index.php">Home</a> > <a href="manage-courses.php">Manage Courses</a> > <a href="courses-list.php">List Courses</a> > Edit Course </div> </div> <?php include("../teacher/includes/manage-courses-aside.php"); ?> <div class="span-18 last"> <h2 class="alt">Edit Course</h2> <form id="form" method="post" action="courses-edit-action.php"> <input type="hidden" value="<?php echo $courseid; ?>" name="id" /> <label>Course Name:</label><input type="text" name="title" id="title" class="text" value="<?php echo $title; ?>" /><br /><br /> <label>Choose Subject:</label> <?php $con=mysql_connect($dbserver,$dbusername,$dbpassword); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname, $con); $result = mysql_query("SELECT * FROM subjects"); echo "<select name='subjects-id'>\n"; while($row = mysql_fetch_array($result)) { echo "<option value='".$row['id'] . "'"; if ($subjectid==$row['id']) echo 'selected="selected"'; echo " >" . $row['subjectname'] . "</option>\n"; } echo "</select>\n"; mysql_close($con); ?> <label>Description:</label><textarea name="description" id="description"><?php echo $des; ?></textarea><br /> <br /> <input type="submit" value="Edit Course" class="button" /> </form> <?php include("../includes/footer.php"); ?> <?php } else { header("Location: ".$fullpath."login/unauthorized.php"); } ?> 

and this is courses-edit-action.php

<?php include("../includes/config.php");?> <?php $id=$_POST["id"]; $title=$_POST["title"]; $des=$_POST["description"]; $subjectid=$_POST["subjects-id"]; $con=mysql_connect($dbserver,$dbusername,$dbpassword); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ombts", $con); $query=("UPDATE courses SET title='".$title."', description='".$des."', 'subjects- id'='".$subjectid."' WHERE (id='".$id."')"); $query=mysql_query($query); if($result){ echo header("Location:manage-courses.php?status=2"); } mysql_close($con); ?> 

and the warnings are

Notice: Undefined index: id in C:\xampp\htdocs\project\teacher\courses-edit-action.php on line 4 Notice: Undefined index: title in C:\xampp\htdocs\project\teacher\courses-edit-action.php on line 5 Notice: Undefined index: description in C:\xampp\htdocs\project\teacher\courses-edit-action.php on line 6 Notice: Undefined index: subjects-id in C:\xampp\htdocs\project\teacher\courses-edit-action.php on line 7 
8
  • 1
    Are you able to get any result if you add the following line into courses-edit-action.php print_r($_POST); ? Commented Sep 26, 2012 at 8:18
  • This line <input type="hidden" value="<?php echo $courseid; ?>" name="id" /> the quotes are wrong Commented Sep 26, 2012 at 8:19
  • also, use mysql_real_escape_string to stop the SQL injection Commented Sep 26, 2012 at 8:20
  • 1
    You're variables arent being passed along from the form. try print_r($_POST); as suggested above. to see what you're getting. Commented Sep 26, 2012 at 8:21
  • Please show us the HTML-output of the form. Commented Sep 26, 2012 at 8:35

3 Answers 3

2

You try to access

$id=$_POST["id"]; $title=$_POST["title"]; $des=$_POST["description"]; $subjectid=$_POST["subjects-id"]; 

this POST variables. But when you access the script for the first time this values are not set in the POST variable.

Chck the values with isset.

if(!isset($_POST["id"]) { $_POST["id"] = ''; } 

or safe the checked value in a new variable

$id = isset($_POST["id"]) ? $_POST["id"] : 0; 

for example.

Sign up to request clarification or add additional context in comments.

Comments

2

Modify your script like this....

 $id= isset($_POST["id"]) ?$_POST["id"] : 0 ; $title= isset($_POST["title"]) ? $_POST["title"] : '' ; $des=isset($_POST["description"]) ? $_POST["description"] : ''; $subjectid= isset($_POST["subjects-id"]) ? $_POST["subjects-id"] : ''; 

Edited Also, i see an error here...

$query=mysql_query($query); if($result){ 

should be

$result=mysql_query($query); if($result){ 

If you still can't update the table, print the $query variable before you do mysql_query. Tell me what you see and i will help you.....

2 Comments

errors are vanished by this but still its not updating the record... :( i never met this tyype of sticky problem ever... :(
yup i just got tht error solved but still it wasnt working as u said now.. i printed the $query and its updaing.. this is what i see UPDATE courses SET title='jnjk', description='hgszhn', 'subjects-id'='5' WHERE (id='4') here the values right.. but i dont know where the problem is :(
2

@Stony's answer is good solution to check if you are sending any POST data - and if not, do not try to fetch non-existing data.

Now, if you think $_POST["id"]; should be there, then there is something missing. Are you using GET instead of POST?

To see what data is submitted do a print_r($_POST). If this is blank, do a print_r($_REQUEST). If this is still blank, then no data is being submitted from the previous page.

You can also check by adding this in your URL &id=75 (or ?id=75 if you have no other parameters in the url). This will submit data and you will see one less error.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.