I have a file with a table with movie titles. When I wish to edit I click on a link which is represented in this following code:

<a href="edit.php?autoid='.$row['autoid'].'">
           edit</a>

This redirects me to an edit file where I can change the name, year and category. After updating it says title updated but with the following error:
Notice: Undefined index: autoid in C:\wamp\www\edit.php on line 37
When I go back to the main page the title is never updated?
Can someone help me please?
I'll attach my file, so that anyone can see what I'm talking about...
Thanks in advance!

Attachments
<html>
<body style="background-color:powderblue;">
      <?php
      include("c:\wamp\www\conf.php");
       if(isset( $_GET['autoid']))
                    {

                  $connection=@mysql_connect($host, $user, $pass)or die("Unable to connect!");
                  mysql_select_db($db, $connection) or die ('Unable to select database!');

                   $id=$_GET['autoid'] ;
                   $query4= "select title, category, year from movie where autoid='$id'";
                $result2=mysql_query($query4) or die ("query failed!");


                     $row = mysql_fetch_assoc($result2);
         ?>

          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" style="text-align:center">
               Your title:
               </br> </br> </br>
               Title:<input type="text" name="title" value=<?php echo $row['title']; ?>>

               Category:<input type="text" name="category" value=<?php echo $row['category']; ?>>

               Year:<input type="text" name="year" value=<?php echo $row['year']; ?>>
               
               <input type="hidden" value="$autoid">
               <input type="submit" name="submit" value="change"  style=" height:35;width:70">



           <?php
                    }
                   if(isset($_POST['submit']))
                   {
                       $id=$_POST['autoid'] ;
                     $titel =  $_POST['title'];
                  $kategori =  $_POST['category'];
                  $ar =  $_POST['year'];

                  $connection=@mysql_connect($host, $user, $pass)or die("Unable to connect!");
                  mysql_select_db($db, $connection) or die ('Unable to select database!');

                    $query5= "update movie set title='$titel',category='$kategori', year='$ar'  where autoid='$id'";
                $result3=mysql_query($query5) or die ("query failed!");

                echo "Title updated!";

                   }
             ?>
                </br> </br> </br>
              <a href="members.php">Back to main page</a>
</body>
 </html>
<input type="hidden" value="$autoid">

The error is in this line, you have forgotten to name this field, so it can't find it in the $_POST array. Try changing to this:

<input type="hidden" name="autoid" value="$autoid">
<input type="hidden" value="$autoid">

The error is in this line, you have forgotten to name this field, so it can't find it in the $_POST array. Try changing to this:

<input type="hidden" name="autoid" value="$autoid">

Hi! Thanks for your reply!
I've tried this out and the error dissapeared, but it didn't update the title, when I changed the year of the movie.

Should it update the TITLE if you change the YEAR?

I can choose to update either the year, title or the category. So if I update the year it only updates the year. I'm sorry for not being clear. Instead of "editing a title" I should have written "edit movie info" as topic.

Try echo'ing your update sql statement before you run the query. This way you can check the sql that is being run for the correct variables. Another good hint for debugging is to copy and paste that sql statement into an mysql command line and seeing what errors or results it gives you there (as opposed to php errors).

Also, you will need to check that each field has been set. Use the isset method on each of your $_POST retrievals, like so:

if( isset( $_POST[ 'title' ]))
  $titel = $_POST[ 'title' ];

Then you will need to qualify your SQL to only update the fields that are not null.

Try echo'ing your update sql statement before you run the query. This way you can check the sql that is being run for the correct variables. Another good hint for debugging is to copy and paste that sql statement into an mysql command line and seeing what errors or results it gives you there (as opposed to php errors).

Good idea!
I've tried both things. In mySQL the query worked fine. However when I tried echoing the update statement everything was echoed (year,title, category) except autoid. It echoed $id, which means that autoid for some reason never is passed..

Also, you will need to check that each field has been set. Use the isset method on each of your $_POST retrievals, like so:

if( isset( $_POST[ 'title' ]))
  $titel = $_POST[ 'title' ];

Then you will need to qualify your SQL to only update the fields that are not null.

I've tried:

if( isset( $_POST[ 'autoid' ]))
  $id = $_POST[ 'autoid' ];

but it acted normal without any errors and without updating.

Good idea!
I've tried both things. In mySQL the query worked fine. However when I tried echoing the update statement everything was echoed (year,title, category) except autoid. It echoed $id, which means that autoid for some reason never is passed..

If autoid is a numeric field, you don't need the ' marks around it in the SQL statement. If you remove them, the $id should be resolved.

I checked your script.

<input type="hidden" value="$autoid">

is wrong. If you want to have a hidden element with php variable's value, you should do,

<input type="hidden" name="autoid" value="<?php echo $autoid; ?>">

Secondly, Everytime there is a database operation, you have mysql_connect and mysql_select_db in your script. You can put it on top of your script, or have it in a separate file and include it on top of this script.
And lastly, You are not sanitizing your input. Use mysql_real_escape_string to protect your application from sql injections.

I checked your script.
is wrong. If you want to have a hidden element with php variable's value, you should do,

<input type="hidden" name="autoid" value="<?php echo $autoid; ?>">

Secondly, Everytime there is a database operation, you have mysql_connect and mysql_select_db in your script. You can put it on top of your script, or have it in a separate file and include it on top of this script.
And lastly, You are not sanitizing your input. Use mysql_real_escape_string to protect your application from sql injections.

Thank you!!
It worked as a charm! I would never have guessed that it required an echo. Now I learned something new. :icon_cheesygrin:
Once again thank you!

This question has already been answered. Start a new discussion instead.