Page 1 of 1

question about variable scope

Posted: Tue Oct 10, 2006 3:01 pm
by jbille
I am creating a site with online administration. However I am having a problem. I am trying to somehow pass a variable from one php file to another php file. The only way I know how to do this is with the include file. However if I include this previous file it does not work out for me because it executes the entire file. Can anyone please help me?

Posted: Tue Oct 10, 2006 3:09 pm
by feyd
I'm not sure I understand your file layout. Could you post your code (or a similar facsimile)?

here's my three files

Posted: Tue Oct 10, 2006 5:07 pm
by jbille
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


what i am trying to do is transfer the variable $prodnumber to editproduct.php

findproduct.html
[syntax="html"]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" 
      xml:lang="en" lang="en">
<head><title>Administration Page</title>
<link rel=stylesheet href="main.css" type="text/css"></head>
<body bgcolor="#204D70">
	<table  bgcolor="white" frame="box" border="0" width="700" cellpadding="0" cellspacing="0" align="center" valign="center" height=600>
	<tr>
	<td>
		<table bgcolor="#C76114" border="0" cellpadding="5" cellspacing="0" align="center" valign="center">
		<form name="findproduct" action="findproduct.php" method="POST" id="findproduct">	
			<tr><td align="center" class="adminform">Product Number:</td><td></td><td><input type="text" name="number" id="number" size="27" /></td></tr>
			<tr><td></td><td></td><td align="right"><input type="submit" value="submit"></td></tr>
		</form>
	</table>
	</td>
	</tr>
</body>
</html>
findproduct.php[/syntax]

Code: Select all

<?php
	$host = xxxxxxxxx;
	$user = xxxxxxxxx;
	$pass = xxxxxxxx;
	$dbname = xxxxxx;

         $prodnumber = $_POST['number'];
	

	$link = mysql_connect($host, $user, $pass);
	if (!$link) {
   	die('Not connected : ');
	}
	mysql_select_db($dbname, $link) or die("Unable to select database");
	$query ="SELECT * FROM electrical WHERE (number = " . $prodnumber . ")";
	$result = mysql_query($query);
	if (!result) {
		echo "Product Number could not be found.  Please go back and try again";
	}
	
	while($row = mysql_fetch_array($result, MYSQL_NUM))
	{
    	$name = $row[0];
    	$number = $row[1];
    	$price = $row[2];
    	$order = $row[3];
    	$description = $row[4];
    	$picture = $row[5];
    } 

    echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" 
      xml:lang=\"en\" lang=\"en\">
      <body><head><title>Edit product</title>
<link rel=stylesheet href=\"main.css\" type=\"text/css\"></head>
<body bgcolor=\"#204D70\">
<table  bgcolor=\"white\" frame=\"box\" border=\"0\" width=\"700\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\"  valign=\"center\" height=600>
	<tr><td align=\"center\" class=\"admin\">Edit a product</td></tr>

	<tr>
		<td>
			<table bgcolor=\"#C76114\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\" align=\"center\" valign=\"center\">
			<form name=\"editproduct\" action=\"editproduct.php\" method=\"POST\" encytype=\"multipart/form-data\">	
			<tr><td class=\"adminform\">Product Name:</td><td></td><td><input type=\"text\" value=" . $name . " name=\"name\" id=\"name\" size=\"27\" /></td></tr>
			<tr><td class=\"adminform\">Product Number:</td><td align=\"right\" class=\"adminform\">#</td><td><input value=" . $prodnumber . " type=\"text\" name=\"number\" id=\"number\" size=\"27\" /></td></tr>
			<tr><td class=\"adminform\">Product Price:</td><td align=\"right\" class=\"adminform\">$</td><td valign=\"top\"><input type=\"text\" value=" . $price . " name=\"price\" id=\"price\" size=\"27\" /></td></tr>
			<tr><td class=\"adminform\">Where do you want this item to be in the list?:</td><td></td><td><input type=\"text\" value=" . $order . " name=\"order\" id=\"order\" size=\"27\" /></td></tr>
    		<tr><td class=\"adminform\">What category do you want the product to go in?:</td><td></td>
    		<td><select id=\"category\" name=\"category\" size=\"1\" value=\"electrical\">
			<option value=\"0\">Choose a category</option> 
			<option value=\"electrical\">Electrical</option>
			<option value=\"hydraulic\">Hydraulic</option>
			<option value=\"air\">Air</option>
			<option value=\"springs\">Springs</option>
			<option value=\"shoes\">Brake Shoes</option>
			<option value=\"suspension\">Suspension</option> 
			</select></td>
			<tr><td valign=\"top\" class=\"adminform\">Product Description:</td><td></td><td><textarea name=\"description\" cols=20 rows=6>" . $description . "</textarea></td></tr>
			<tr>
			<td class=\"adminform\"><input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"300000\" />Upload this picture: </td>
			<td></td>
			<td><input name=\"picture\" value=" . $picture . " type=\"file\" />
    		</td>
    		</tr>
    		<tr height=\"10\"></tr>
  			<tr><td></td><td></td><td align=\"left\"><input type=\"submit\" value=\"submit\"></td></tr>
    		</form>
  			</table>
    	</td>
    </tr>
</table>
</body>
</html>";


	mysql_close($link);	
	
	?>
editproduct.php

Code: Select all

<?php	
	
	$host = xxx
	$user = xxxxxxxxxxx
	$pass = xxxxxxxxx
	$dbname = xxxxxxxxxxx
	
	$name = $_POST['name'];
	$number = $_POST['number'];
	$price = $_POST['price'];
	$order = $_POST['order'];
	$category = $_POST['category'];
	$description = $_POST['description'];
	$picture = $_POST['picture'];
	
	if((empty($name)) || (empty($number)) || (empty($price)) 
	|| (empty($order)) || (empty($category)) || (empty($description)) || (empty($picture)))
	{
		echo "Please go back and fill in the following information: <br><br>";
		if(empty($name)) echo ("Product Name <br>");
		if(empty($number)) echo ("Product Number<br>");
		if(empty($price)) echo ("Product Price<br>");
		if(empty($order)) echo ("Order of item in the list<br>");
		if(empty($category)) echo ("Category must be picked<br>");
		if(empty($description)) echo ("Product Description<br>");
		if(empty($picture)) echo ("Please upload a photo<br>");
		exit(1);
	}
	
	$data = addslashes(fread(fopen($picture, "r"), filesize($picture)));
	
	$link = mysql_connect($host, $user, $pass);
	if (!$link) {
   	die('Not connected : ');
	}
	mysql_select_db($dbname, $link) or die("Unable to select database");
	
	
	
	$query ="UPDATE " . $category . " SET number='$number' WHERE number='$prodnumber'"; 
	mysql_query($query) or die('Error, query failed, Call Jimmy');
	
	mysql_close($link);
	
	echo "<html><body><p>Thank you. <a href=\"findproduct.html\">Click here</a> to edit another product
			or <a href=\"admin.html\">here</a> to return to administration home page";
			
	
	
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Oct 10, 2006 6:40 pm
by RobertGonzalez
Either use sessions, cookies, or a querystring (GET) ver.

Posted: Tue Oct 10, 2006 7:57 pm
by chakhar86
Just want to, comment the code.
You better separate the HTML and PHP code
ex:

Code: Select all

<?php
    if ($something_happen) {
?>
<form>
  Something happen, please comment<br>
  <input type="text" name="comment" />
  <input type="submit" name="Submit" />
</form>
<?php
    } else {
?>
    There's nothing happens, you can relax<br>
    comment's value = 
<?php
        echo $_POST['comment'];
    }
?>
Your code will run slightly faster, and you don't have to type echo just to show HTML tags and lotsa escaped string (\") it reduce readibility