passing values between .php pages

Problems with the Windows version of XAMPP, questions, comments, and anything related.

passing values between .php pages

Postby lunaticbit » 21. May 2015 09:51

I have to pass a file for uploading to another page which will validate and insert to a table in conjuction with a literal field which has a well defined value.

Code: Select all

<?php

$hidden_field = 'test";

echo "<form action=\"upload_file.phpl" target=\"_blank\">";
echo "<table align =] "cente]r" width =\ "50%\">";
echo "<tr align = \"center\">";
echo "th align =\ "cente\r"> select file</th>";
echo "<td><input type=\"file\" name=\"datafile\" size=\"40\"</td>";
echo "</tr>";
echo "<tr align = \"center\">";
echo "<th align = \"center\">comment</th>";
echo "<td><input type="hidden" name=\"hidden_field\" value=\"$hidden_field\"</td>";
echo "</tr>
echo "<tr  align = "center">
echo "<td><br><br></td>
echo "<td><input type="submit" name="submit" value="Submit"></td>
echo "</tr>
echo "</table>
echo "</form>

?>



In the other page, before perform any action, since the passed hidden type fied results always blank
I put there the statementQ

Code: Select all

<? php

$l_value = $_GET['hidden_field'];
// echo $l_value returms always blank/empy content

/* and so on ...
the datafile variables concerning their passed values reulting to be correct, according their further evaluation and insertion to the table...
*/



Pls any help cause I'm driving crazy about this think. Thanks in advance!
lunaticbit
 
Posts: 21
Joined: 09. April 2015 16:24
Operating System: Windows 7

Re: passing values between .php pages

Postby Nobbie » 21. May 2015 10:22

Already the first script contains tons of syntax errors and does not run. First of all, remove all errors and after copy & paste the original code her.
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: passing values between .php pages

Postby mark.mcdonald » 22. May 2015 14:45

lunaticbit wrote:echo "<form action=\"upload_file.phpl" target=\"_blank\">";
echo "<table align =] "cente]r" width =\ "50%\">";
echo "<tr align = \"center\">";
echo "th align =\ "cente\r"> select file</th>";

These are immediate red flags as to why things aren't working. First rule of programming is to get one thing working at a time. Don't worry about passing variables now, worry about fixing this code.
mark.mcdonald
 
Posts: 160
Joined: 13. March 2015 15:48
Location: Edmonton
Operating System: Windows Server 2012 R2

Re: passing values between .php pages

Postby lunaticbit » 22. May 2015 15:20

Sorry, when I mademy inquary I was out from my machine so once more I have
A originating script

<html>
<head>
<style type = "text/css">
@import url("test.css");
</style>
</head>
<body >
<h1>Select File to Upload</h1>
<hr>
<?php
$h_name = $_GET['h_name'];

$h_name = 'skatas';

echo("<h1>["); echo $h_name; echo("]</h1>");
echo("<br>"); echo ("<h1>["); echo ("$h_name"); echo("]</h1>");
echo("<hr>");

echo ("<form action=\"insert.php\" method=\"post\" enctype=\"multipart/form-data\">");
echo ("<form method=\"post\" enctype=\"multipart/form-data\">");
echo ("<INPUT TYPE=\"hidden\" NAME=\"MAX_FILE_SIZE\" VALUE=\"1000000\">");
echo ("<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"upload\">");
echo ("<input type=\"file\" name=\"myfile\" id=\"fileToUpload\">");

echo ("<INPUT TYPE=\"hidden\" NAME=\"h_user \" VALUE= \"$h_name \">");

echo ("<h1><input type=\"submit\" value=\"Upload File\" name=\"submit\"></h1>");
echo ("</form>");

?>
</body>
</html>

B. delivering script

<html>
<head>
<style type = "text/css">
@import url("test.css");
</style>
</head>
<body>
<h1>Inserting File</h1>
<hr>

<?php

$name = $_FILES['myfile']['name'];
$type = $_FILES['myfile']['type'];
$size = $_FILES['myfile']['size'];
$temp = $_FILES['myfile']['tmp_name'];


$user = $_GET['h_user '];
echo $user;


$fp = fopen($temp, 'r');
$data = fread($fp, filesize($temp));
$data = addslashes($data);
fclose($fp);

echo "<font face=\"Verdana\">";
echo "file name: "; echo $name; echo "<br>";
echo "file type: "; echo $type; echo "<br>";
echo "file size: "; echo $size; echo "<br>";
echo "temp location: "; echo $temp ; echo "<br>";
echo "user name: "; echo $user; echo "<br>";
echo "</font>";
echo "<hr>";

if ($name <> NULL and $type <> NULL and $data <> NULL and $size <>0 and $temp <> NULL)
{
$curdate = date('Y/m/d H:i');
$db = mysql_connect("localhost", "root", "r00t");
mysql_select_db("alpha", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");
$qry = "insert into myfiles (name, type, size, date, data) values
(\"$name\", \"$type\", \"$size\", \"$curdate\", \"$data\", \"$user\")";
mysql_query($qry) or die("Error... Query Failed!"); echo "<br>";
echo "<p align = \"center\"><font face =\"VERDANA\" color=\"GREEN\">Record Inserted</font><hr>";
echo "</p>";
echo "<br>";
//mysql_free_result($result);
mysql_close($db);
echo "<a href=\"select.php\"><img src=\"rewind.png\"></a>";
}
else
{
echo "Unable to Insert Record";
echo "<br>$data<hr>";
}
?>
</body>
</html>

and just in case

test.css contains

body
{
background: #000000;
text-color: #FFFFFF;
background-image: url("gaidouri.png");
background-repeat: no-repeat;
background-position: right top;
margin-left: 200px;
margin-right: 200px;
font-family: Verdana;
}
h1
{
color: red;
text-align : center;
}
h2
{
color: green;
}
h3
{
color: blue;
}
table, td, th
{
border: 1px solid green;
color: white;
}

th
{
background-color: light-green
color: white;
}
hr
{
height: 5px;
width: 600px;
}
}

Tat's all folks!
lunaticbit
 
Posts: 21
Joined: 09. April 2015 16:24
Operating System: Windows 7

Re: passing values between .php pages

Postby Nobbie » 22. May 2015 15:39

As your form specifies method="POST", of course you cannot use $_GET, but $_POST instead:

Replace

Code: Select all
$user = $_GET['h_user '];


by

Code: Select all
$user = $_POST['h_user '];
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: passing values between .php pages

Postby mark.mcdonald » 22. May 2015 15:44

lunaticbit wrote:echo "file name: "; echo $name; echo "<br>";

Could be combined into: echo "File Name: '$name'<br>"; as well as many other lines could be shortened like this.
lunaticbit wrote:echo ("</form>");
echo "</font>";

Why do you have brackets around one segment in php but not in another segment of php?
lunaticbit wrote:hr
{
height: 5px;
width: 600px;
}
}

Do you think this syntax could be causing issues?
As said I would first spend some time fixing your code before worrying about passing values.

HTML / HTTP is stateless, in other words, what you did / saw on the previous page, is completely disconnected with the current page. Except if you use something like sessions, cookies or GET / POST variables. Sessions and cookies are quite easy to use, with session being by far more secure than cookies. More secure, but not completely secure.

Session:

//On page 1
$_SESSION['varname'] = $var_value;

//On page 2
$var_value = $_SESSION['varname'];
Remember to run the session_start() statement on both these pages before you try to access the $_SESSION array, and also before any output is sent to the browser.

Cookie:

//One page 1
$_COOKIE['varname'] = $var_value;

//On page 2
$var_value = $_COOKIE['varname'];
The big difference between sessions and cookies are that the value of the variable will be stored on the server if you're using sessions, and on the client if you're using cookies. I can't think of any good reason to use cookies instead of sessions, except if you want data to persist between sessions, but even then it's perhaps better to store it in a DB, and retrieve it based on a username or id.

GET and POST

You can either add the variable in the link to the next page:

<a href="page2.php?varname=<?php echo $var_value ?>">Page2</a>
This will create a GET variable, or include a hidden field in a form that submits to page two:

<form method="get" action="page2.php">
<input type="hidden" name="varname" value="var_value">
<input type="submit">
</form>
And then on page two

//Using GET
$var_value = $_GET['varname'];

//Using POST
$var_value = $_POST['varname'];

//Using GET, POST or COOKIE.
$var_value = $_REQUEST['varname'];
Just change the method for the form to post if you want to do it via post. Both are equally insecure, although GET is easier to hack.

The fact that each new request is, except for session data, a totally new instance of the script caught me when I first started coding in PHP. Once you get used to it, it's quite simple though.
mark.mcdonald
 
Posts: 160
Joined: 13. March 2015 15:48
Location: Edmonton
Operating System: Windows Server 2012 R2

Re: passing values between .php pages

Postby Nobbie » 22. May 2015 17:52

mark.mcdonald wrote:Why do you have brackets around one segment in php but not in another segment of php?
lunaticbit wrote:hr
{
height: 5px;
width: 600px;
}
}


This is not PHP, that is a plain CSS File (test.css), this syntax is Ok (there is only one bracket too much, but i think its a typo here in the forum).
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: passing values between .php pages

Postby mark.mcdonald » 22. May 2015 18:04

Nobbie I know that's not php. That comment was referring to the quotes above the text. I know that's just css but when combing over code everything and anything can become an issue as I've learned the hard way.
mark.mcdonald
 
Posts: 160
Joined: 13. March 2015 15:48
Location: Edmonton
Operating System: Windows Server 2012 R2

Re: passing values between .php pages

Postby lunaticbit » 25. May 2015 15:20

Dearest friend, thank you spending time in my favor.
Tried to replace the GET with POST function, but the result still remains! On the other hand the GRT method works flowessly in the rest of the modules. Before upload the code in use,permit me to say "Don't pay attention to the CSS part, which it is not our aim, but fromother hand no mistakes or errors prompting when I call them. Finally the code had to bemore compact, but I used so many pieces in my quest on what'sgoing here. FYI the code with $$_GET['...'} used in the othermodules folows belong.

1. login.php

Code: Select all

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>

<style type = "text/css">
   body
   {
      background: #000000;      
      text-color: #FFFFFF;
      background-image: url("gaidouri.png");
      background-repeat: no-repeat;
      background-position: right top;
      margin-left: 200px;
      margin-right: 200px;
      font-family: "verdana";
   }
   h1
   {
      color: red;
      text-align : center;
      font-family: "verdana";
   }
   h2
   {
      color: green;
      font-family: "verdana";
   }
   h3
   {
      color: blue;
      text-align: right;
      font-family: "verdana";
   }
   p
   {
      text-align: left;
      font-family: "verdana";
   }
</style>

</head>
<body>
   <h1 >Log In</h1>
   <hr>
   <p>
   <form action="validate.php">
   <form method="post" enctype="multipart/form-data">
   <h2>
   user name: ______<INPUT TYPE=\"text\"  NAME = "user_name" VALUE ="">
   <br><br>
   user password: ___<INPUT TYPE="password"  NAME= "user_pass" VALUE="">
   <br><br>
   <input type="submit" value="validate user" name ="submit">
   </h2>
   </form>
   </font>
</body>
</html>



And the validate.php

Code: Select all

<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Validate</title>
<style type = "text/css">
   @import url("test.css");
</style>
</head>
<body >
   <h1 >Validate User</h1>
   <hr>
<?php
   $u_name = $_GET['user_name'];
   $u_pass = $_GET['user_pass'];
   //$u_name = $_POST['user_name'];
   //$u_pass = $_POST['user_pass'];

   echo "<h1>";    
   echo  "[$u_name]  [$u_pass]";
    echo"</h1>";
   echo "<hr>";
   $link = mysqli_connect("localhost", "root", "r00t", "alpha");
/* check connection */
   if (mysqli_connect_errno())
   {
      //printf("Connect failed: %s\n", mysqli_connect_error());
      echo ("Connection failed mysqli_connect_error()");
      exit();
   }
   if ($result = mysqli_query($link, "SELECT id, name, pass, site, descr  FROM users
                                          where name = '$u_name' AND pass = '$u_pass'
                                          ORDER BY name"))
   {
/* determine number of rows result set */
      $row_cnt = mysqli_num_rows($result);
      echo "<h1>";
      //printf("Result set has %d rows.\n", $row_cnt);
      echo ("Num of Rows: $row_cnt");
      echo "</h1>";
/* close result set */
      mysqli_free_result($result);
   }
/* close connection */
   mysqli_close($link);
   if ($row_cnt == 1)
   {
      echo("<h1>");
      echo ("Ok $u_name<br><br>");
      echo ("<form action=\"select.php\" method=\"post\">");
      echo ("<INPUT TYPE=\"hidden\" NAME = \" 'h_name' \" VALUE=   \'' $u_name ");
      echo ("<h1><input type=\"submit\" value=\"Proceed\" name=\"submit\"></h1>");
      echo("</form>");
      echo ("</h1>");
   }
   else
   {
      echo ("<h1>USER NOT VALIDATED SUCCESSFULLY</h1>");
   }
?>
</body>
</html>



Pls forgive my poor knowledge on these matters, but I have to step, littlt by little forward
My regards and my respects dear friend from Nic
lunaticbit
 
Posts: 21
Joined: 09. April 2015 16:24
Operating System: Windows 7

Re: passing values between .php pages

Postby mark.mcdonald » 25. May 2015 16:52

lunaticbit wrote:
Code: Select all
echo("<h1>");
echo ("Ok $u_name<br><br>");
echo ("<form action=\"select.php\" method=\"post\">");
echo ("<INPUT TYPE=\"hidden\" NAME = \" 'h_name' \" VALUE=   \'' $u_name ");
echo ("<h1><input type=\"submit\" value=\"Proceed\" name=\"submit\"></h1>");
echo("</form>");
echo ("</h1>");


Difference of single and double quotes. Single quotes will print the variable name, not the value:

<?php
$color = "red";
echo "Roses are $color";
echo "<br>";
echo 'Roses are $color';
?>
mark.mcdonald
 
Posts: 160
Joined: 13. March 2015 15:48
Location: Edmonton
Operating System: Windows Server 2012 R2

Re: passing values between .php pages

Postby lunaticbit » 26. May 2015 18:23

As yo can see the Login and Vaidate moduls ar working fine using form method equal to POST and their values of the "fields" are also passing correct using the $_GET['var_name'} statemrnt. The p1roblem arises when the value(s) of the variables (name_variable) has to forward it's value on the next page. I exhaust any posssible action(single vs double quotes either in the corresponded names and or values, even their names has been changed and as for last debugging the syntax used opening and closing php section to the "field", concerning both variable name(s) and variable value(s). Also you can see that the so called "validation" derives from the digging of an sql query to a DB table which on column (user) contains the desired name, and pass the password. I cannot understand what it is causing this behaviour! That's the reason of adapting my nick name, of course in the past I was missleded from other "brains" as far it concerns my previous posts on "ancient code". Now my mind has been blast for ever!
Thank you for sharing my troubled mind thoughts.
Sill lost in space...
lunaticbit
 
Posts: 21
Joined: 09. April 2015 16:24
Operating System: Windows 7

Re: passing values between .php pages

Postby lunaticbit » 28. May 2015 10:35

So, the Quest goes on, trying to simplify things, I decided to make a new series of actions in sequence

1. The nic.css script which can be omited
Code: Select all

   body
   {
      background-color=WHEAT;
      background-image: url("spiral.png");
      background-repeat: repeat-y;
      margin-left: 200px;
      margin-right: 200px;

      font-family: Verdana;
      font-size: 16px;
   }

   h1
   {
      color: red;
      text-align : center;
      font-family:verdana;
      font-size: 48px;
   }
   h2
   {
      color: green;
      text-align: right;
      font-family: "verdana";
      font-size: 36px;
   }
   h3
   {
      color: blue;
      text-align: left;
      font-family: "verdana";
      font-size: 24px;
   }
   hr
   {
      height: 10px;
      width: 600px;
   }
   p
   {
      font-family: "verdana";
   }




2. The initial upload_form.php

Code: Select all

<html>
<head>
<style>
@import url("nic.css");
</style>
</head>
<body>
<h1>Theognis</h1>
<hr color = "magenta">
<h2>Upload Example</h2>

</p>
<h3>
<p align = "center">
<?php

$datafile = $_GET['datafile']; echo $datafile; echo "<br>";
$comment = $_GET['comment']; echo $comment;  echo "<br>";
$hide1 = $_GET['hide1']; echo $hide1;  echo "<br>";
$hide2 = $_GET['hide2']; echo $hide2;  echo "<br>";

echo("<form action=\"validate.php\" method=\"post\">");
   echo("<input type=\"hidden\" name=\"l_hide1\" value=\"$hide1\">");
   echo ("<br>");
   echo("<input type=\"hidden\" name=\"l_hide2\" value=\"$hide2\">");
   echo ("<input type=\"submit\" name=\"submit\" value=\"Submit\">");
echo ("</form>");

?>
</p>
</h3>
<h1>
<a href="form_example.html"><image src = "cyan.png" border="0"
onmouseover="this.src='yellow.png'"
onmouseout="this.src='cyan.png' "/ >
</a>
<br><input type="button" value="Close this window" onclick="self.close()">
</h1>
</body>
</html>



3. And the last one validate.php

Code: Select all

<html>
<head>
<style>
@import url("nic.css");
</style>
</head>
<body>
<?php

$z_hide1 = $_SESSION['l_hide1'];
$z_hide2 = $_SESSION['l_hide2'];
/*
$z_hide1 = $_GET['l_hide1']; echo $z_hide1; echo "<br>";
$z_hide2 = $_GET['l_hide2']; echo $z_hide2; echo "<br>";
*/
echo $z_hide1; echo "<br>";
echo $z_hide2; echo "<br>";
?>
</body>
</html>



IN case of adding the $_session on pg. 2 and pg. 3 modules such as upload_form.php

Notice: Undefined variable: hide1 in C:\xampp\htdocs\myfolder\upload_file.php on line 17

Notice: Undefined variable: in C:\xampp\htdocs\myfolder\upload_file.php on line 17

Notice: Undefined variable: hide2 in C:\xampp\htdocs\myfolder\upload_file.php on line 18

Notice: Undefined variable: in C:\xampp\htdocs\myfolder\upload_file.php on line 18
C:\Users\Public\Pictures\Sample Pictures\Koala.jpg //OK value
Fox message //OK value
alpha //OK value
beta //OK value

And for the validate the same relative messages appeaing once more!

Alas!!
lunaticbit
 
Posts: 21
Joined: 09. April 2015 16:24
Operating System: Windows 7

Re: passing values between .php pages

Postby Nobbie » 28. May 2015 11:06

Its getting worse and worse...

This is really bad:

Code: Select all
  <form action="validate.php">
   <form method="post" enctype="multipart/form-data">


What is that? You are "stacking" two forms? One without method (results in method="GET") and without action? Horribly wrong.

And the session variables $_SESSION do NOT WORK in any case, because you MUST CALL session_start() in every(!) module, where you want to use Session Variables. Your next problem is, that you always start you PHP FIles with plain HTML (<html> etc.). If you do so, you cannot call session_start() later on, you will receive an error message like "cannot send session cookie - headers already sent by".

You are a beginner, thats not a problem, but you really should read some tutorials, should work out simple programming examples, read books about PHP and the concepts of HTTP etc. - we cannot debug your code, this forum is meant for technical problems, not for teaching PHP. Its not an easy job to learn HTML and PHP and this will take quite a while. At least months.
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: passing values between .php pages

Postby lunaticbit » 29. May 2015 10:08

As you can detect from the newer simplified version "upload" -> "validate" There is only one form which contains only plain text "fields" and the method is post.
There's no conflict, as far as I instructed where to put <? php ... ... ?> section translating an HTML to a PHP module. Further only two (2) "fields" are waiting for furthrter transition. The rest gas been commented from this point, in order to understand what has to be fixed in the code before go on. Using $_session drove me to nowhere, and my intention is also not to involve "cookies" in the oter hand. Thanks for all! Any additional suggestion?
And last but no least using $_session() always returns with "Notice: Undefined index: hide1 in C:\xampp\htdocs\myfolder\validate.php on line ..."
lunaticbit
 
Posts: 21
Joined: 09. April 2015 16:24
Operating System: Windows 7

Re: passing values between .php pages

Postby lunaticbit » 31. May 2015 16:40

Brothers in arms!
Seems that a battle has been achieved, by using the forgotten statement $_REQUEST on 2nd page, no matter hw 1st page is.
Now one step further after the before mentionent obstacle:
Insert a DB recoed, with a files content (BLOB) including also, except contents, with features which can serve to the user's workstation to apply the correct application for accessing it.
Th problem derives when you want to a column to add content which is out of file characterusticsm e.g. a litteral who' already available. Whwn in the formhas to be inserted AND this litteral, selected file is unable to b inserted in thw table.
So let me [resentto you the rest acts of my drama:
Repeatinf the first one, as above

Code: Select all

<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Select File to Upload</title>
   <style type = "text/css">
      @import url("test.css");
   </style>
</head>
<body >
   <h1>Select File to Upload</h1>
   <hr color = "red" >

<?php
   
   //$u_name = $_GET['u_name'];
   $u_name = $_REQUEST['u_name'];
   
   echo("<h1>[$u_name]</h1>");
   
   echo ("<form action=\"insert.php\" method=\"post\" enctype=\"multipart/form-data\">");
   //echo ("<form method=\"post\" enctype=\"multipart/form-data\">");
   echo ("<INPUT TYPE=\"hidden\" NAME=\"u_name   \" VALUE= \"$u_name \">");
   
   echo ("<INPUT TYPE=\"hidden\" NAME=\"MAX_FILE_SIZE\" VALUE=\"5000000\">");
   echo ("<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"upload\">");
   echo ("<input type=\"file\" name=\"myfile\" id=\"fileToUpload\">");
   
   echo("</h1>");   echo ("<h1><input type=\"submit\" value=\"Upload\" name=\"submit\"></h1>");
   echo ("</form>");

?>
   
</body>
</html>



And the page tHAT FOLLWS IS:

Code: Select all

<html>
<head>
   <style type = "text/css">
      @import url("test.css");
   </style>
</head>   
<body>
<h1>Insert File</h1>
<hr color = "red">

<?php

$user = $_REQUEST['u_name '];
echo"<h1>[$user]</h1>";

$name = $_FILES['myfile']['name'];
$type = $_FILES['myfile']['type'];
$size = $_FILES['myfile']['size'];
$temp = $_FILES['myfile']['tmp_name'];

$fp = fopen($temp, 'r');
$data = fread($fp, filesize($temp));
$data = addslashes($data);
fclose($fp);
echo("<h2>");
echo "file name: "; echo $name; echo "<br>";
echo "file type: "; echo $type; echo "<br>";
echo "file size: "; echo $size; echo "<br>";
echo "temp location: "; echo $temp  ; echo "<br>";
echo "user name: "; echo $user; echo "<br>";
echo("</h2>");
echo "</font>";
echo "<hr color = \"red\">";

if ($name <> NULL and $type <> NULL and $data <> NULL and $size <>0 and $temp <> NULL)
{
   $curdate = date('Y/m/d H:i');
   $db = mysql_connect("localhost", "root", "r00t");
   mysql_select_db("alpha", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");
   $qry = "insert into myfiles (name, type, size, date, data, user) values
      (\"$name\", \"$type\", \"$size\", \"$curdate\", \"$data\", \"$user\")";
   mysql_query($qry) or die("Error... Query Failed!"); echo "<br>";
   echo "<h2>Record Inserted<h2>";
   echo "<br>";
   //mysql_free_result($result);
   mysql_close($db);
   echo "<a href=\"select.php\"><img src=\"rewind.png\"></a>";
}
else
{
   echo "<h1>Unable to Insert Record</h1>";
   echo "<br>$data<hr color = \"red\">";
}
?>
</body>
</html><html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Select File to Upload</title>
   <style type = "text/css">
      @import url("test.css");
   </style>
</head>
<body >
   <h1>Select File to Upload</h1>
   <hr color = "red" >

<?php
   
   //$u_name = $_GET['u_name'];
   $u_name = $_REQUEST['u_name'];
   
   echo("<h1>[$u_name]</h1>");
   
   echo ("<form action=\"insert.php\" method=\"post\" enctype=\"multipart/form-data\">");
   //echo ("<form method=\"post\" enctype=\"multipart/form-data\">");
   echo ("<INPUT TYPE=\"hidden\" NAME=\"u_name   \" VALUE= \"$u_name \">");
   
   echo ("<INPUT TYPE=\"hidden\" NAME=\"MAX_FILE_SIZE\" VALUE=\"5000000\">");
   echo ("<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"upload\">");
   echo ("<input type=\"file\" name=\"myfile\" id=\"fileToUpload\">");
   
   echo("</h1>");   echo ("<h1><input type=\"submit\" value=\"Upload\" name=\"submit\"></h1>");
   echo ("</form>");

?>
   
</body>
</html>


and the third one whp' s complting the task:

Code: Select all

<html>
<head>
   <style type = "text/css">
      @import url("test.css");
   </style>
</head>   
<body>
<h1>Insert File</h1>
<hr color = "red">

<?php

$user = $_REQUEST['u_name '];
echo"<h1>[$user]</h1>";

$name = $_FILES['myfile']['name'];
$type = $_FILES['myfile']['type'];
$size = $_FILES['myfile']['size'];
$temp = $_FILES['myfile']['tmp_name'];

$fp = fopen($temp, 'r');
$data = fread($fp, filesize($temp));
$data = addslashes($data);
fclose($fp);
echo("<h2>");
echo "file name: "; echo $name; echo "<br>";
echo "file type: "; echo $type; echo "<br>";
echo "file size: "; echo $size; echo "<br>";
echo "temp location: "; echo $temp  ; echo "<br>";
echo "user name: "; echo $user; echo "<br>";
echo("</h2>");
echo "</font>";
echo "<hr color = \"red\">";

if ($name <> NULL and $type <> NULL and $data <> NULL and $size <>0 and $temp <> NULL)
{
   $curdate = date('Y/m/d H:i');
   $db = mysql_connect("localhost", "root", "r00t");
   mysql_select_db("alpha", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");
   $qry = "insert into myfiles (name, type, size, date, data, user) values
      (\"$name\", \"$type\", \"$size\", \"$curdate\", \"$data\", \"$user\")";
   mysql_query($qry) or die("Error... Query Failed!"); echo "<br>";
   echo "<h2>Record Inserted<h2>";
   echo "<br>";
   //mysql_free_result($result);
   mysql_close($db);
   echo "<a href=\"select.php\"><img src=\"rewind.png\"></a>";
}
else
{
   echo "<h1>Unable to Insert Record</h1>";
   echo "<br>$data<hr color = \"red\">";
}
?>
</body>
</html>


lunaticbit
 
Posts: 21
Joined: 09. April 2015 16:24
Operating System: Windows 7

Next

Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 91 guests