Trying to add some minimal validation when updating a record, but I am getting an include error.

Here is the validation code:

if (strlen($_POST['fname']) > 30) {
		$message = "Character cannot be more than..."; 
		include 'edit.php?id=' . $_POST['id']; 
		exit(); 
	} 

Here is my error:

Warning: include(edit.php?id=97) [function.include]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Homework/Advanced/LittleSprout/update.php on line 13

Warning: include() [function.include]: Failed opening 'edit.php?id=97' for inclusion (include_path='.:/Applications/MAMP/bin/php/php5.3.6/lib/php') in /Applications/MAMP/htdocs/Homework/Advanced/LittleSprout/update.php on line 13

I have no idea what I am doing wrong. It says it can't find the file or directory, but it's there, and within the same folder where edit.php is located. Any suggestions? Thanks in advance.

    Why are you trying to pass a variable (id) to edit.php? The file edit.php is probably there, but it's literally looking for edit.php?id=97. That file does not exist. You cannot pass a variable to an include or require. If there is a function you're trying to run that's in the script, I would include the script then call the function and pass the variable that way.

      Upon further reading, you can pass variables to include if you use HTTP (however I would still avoid this and just call a function).

      I would read the PHP documentation on include for more information

        Bonesnap;10989818 wrote:

        Upon further reading, you can pass variables to include if you use HTTP (however I would still avoid this and just call a function).

        If calling a script over HTTP (or by using system calls such as exec('php ...') you are NOT including the script, you are executing it. This also means that any variables or other data which exists in the currently executing script will no longer exist in the script which you choose to execute from the first script.
        Do not do this. Instead, read about include/require as well as scope.

          In other words:

          if (strlen($_POST['fname']) > 30) {
          	$message = "Character cannot be more than...";
          	$_GET['id'] = $_POST['id'];
          	include 'edit.php'; 
          	exit(); 
          }
          

            Reason I was passing off an id varaible is because I want the user to return back to item they were editing, which is set to an id number.

            Went to the documentation, and read that the only way I can do what I want to do is if I include the absolute path within include()

            For example:

            include "/Applications/MAMP/htdocs/Homework/Advanced/LittleSprout/edit.php?id=" . $POST['id'];
            

            I know I saw something with dirname(FILE), but was unsure of exactly how to use it.

              Edit:
              To clarify. include/require just read the file from the file system. The contents of the file is parsed as php code (if it's inside php opening and closing tags - just like for any other php script). They have nothing to do with a get or post request to a web server.

              thelos999;10989852 wrote:
              include "/Applications/MAMP/htdocs/Homework/Advanced/LittleSprout/edit.php?id=" . $POST['id'];
              

              So you have the id in $_POST['id']. You want to include more code in a separate file.

              # $_POST['id'] contains id allready
              
              echo $_POST['id'];
              
              function someFunc()
              {
                 echo "I'm func!";
              }
              include 'somefile.php';
              

              Why do you expect somefile.php to NOT have access to everything else that exist when that piece of code is included?

              somefile.php

              # should this fail?
              echo $_POST['id'];
              
              # should this fail?
              someFunc();
              

              Like I said before (without link), read up on scope. And read up on [man]include[/man]/[man]require[/man].

              If you on the other hand want the user to be able to request a new page and pass on the id, then you might want to do something like

              printf('<a href="%url?id=%d">Edit %d</a>', $url, $_POST['id'], $_POST['id']);
              

              or

              header(sprintf('location: http://%s?id=%d', $url, $_POST['id']));
              
                Write a Reply...