0

Hi I'm trying to update a single field from a HTML form, for some reason one of the session variables I am passing to the update query is not being accepted. I have already echoed the variable in the page so am fairly certain it exists in memory.

NB, I know my code is horrifically insecure but I'm learning PHP and once I've got the basics working Ill go over it and bring it upto best practice standards.

E2A: If I do var_dump($filename); before trying to run the query it returns string(6) "356/18", after the query it returns NULL. I'm not unsetting the variable anywhere so where could it be going!

Here is my form:

      <form method="post" action="">
      <p>Your username is:&nbsp;<?php echo $_SESSION['userid'] ?>&nbsp;Your company ID is:&nbsp;<?php echo $companyid['id']?></p>
      <h3>Please enter note for file: <?php echo $filename; ?></h3>
      <table width="200" cellpadding="5">
        <tr>
        <th width="18%" align="right" nowrap>Add Note:&nbsp;</th>
        <td width="82%" nowrap>
            <input type="text" name="note" />
        </td>
        </tr>
        <tr>
        <td colspan="2" width="100%" nowrap>
            <input type="submit" value="Submit" name="Submit" />
        </td>
        </tr>
        </table>
      </form>

Here is my UPDATE query:

     $sql = "UPDATE fields SET Notes =          ('".mysql_real_escape_string(stripslashes($_REQUEST['note']))."') 
        WHERE companyId='".$companyid['id']."' AND fileNumber ='".$filename."'";
    if($result = mysql_query($sql)) { 
        echo "<h1>Thank you</h1>Your information has been entered into our database<br><br>";
        echo $sql;
        echo $filename;
        } else { 
        echo "ERROR: ".mysql_error(); 
        } 
        } else { 

echoing $sql produces the following:

     UPDATE fields SET Notes = ('asdasda') WHERE companyId='11' AND fileNumber =''

and here is the bit where I instantiate the POST vars.

         include "header.php";
         $checkFiles = "checkFiles.php";
         // Catches form input from previous page and stores it into session variable called filename for future reference;
         $_SESSION['filename']=$_POST['filename'];
         $filename = $_SESSION['filename'];
         //User id stuff from previous page too; 
         $userid = $_SESSION['userid'];
         $id = mysql_query("SELECT id FROM users WHERE DXNumber='".$userid."'");
         // Returns pointer so fetch it as an array and insert it into variable $companyid for later use;
         $companyid = mysql_fetch_array($id);
2
  • Maybe you are overwriting $_SESSION['filename'] with $_POST['filename']? Commented Jan 16, 2013 at 16:44
  • Am posting the variable contents from a previous page, thats just there to catch the contents. Again, if I echo $_SESSION['filename']; prior to the query it shows the correct contents, but if I echo it after the query its blank. Very confusing. Commented Jan 16, 2013 at 16:46

2 Answers 2

1

You need to include session_start() on the top of each file.

Just do:

AND fileNumber ='".$_SESSION[filename]."'";

In your update query.

If that doesn't work, make sure that a value for $_SESSION[filename] is being set.

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

4 Comments

I've done that in header.php, the session variable works if I echo it, just not in the UPDATE query.
@Funk247 Why don't you try $_SESSION['filename'] instead of $filename?
Hi Boz, I did, that also didnt work so I went back to $filename
@Sean That also doesnt work. Its really weird, the <h3> in the form shows the correct value, but the query shows null.
0
<h3>Please enter note for file: <?php echo $filename; ?></h3> 

Create a input box

<input type="text" name="filename" value="<?php echo $filename; ?>"/>

Then filename value will be pass to $_POST array

3 Comments

I already passed the filename value to the $_POST array in a previous page. The instance in the H3 you quoted outputs correctly, its when I use it in a query it shows null value.
It is available when you load the page at the first time. But after submitting the form your $filename variable does not contains any value since nothing has been assigned to it. try What I have said . if this not works the please let me know
Hi Rohit this makes sense, I've just tried a var_dump and after submit my variable is indeed cleared. Thanks for your assistance, I'll try your solution again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.