Jump to content

values from array & values from a previous post.. into a mysql query!


Recommended Posts

hi all...

 

im back with another question... how can i do somthing complicated in a simple way? ;D

 

no here is my real question...

 

i have a form on page1 it posts two variables username & password to a second page,  page now on this page there is a table with checkboxes... what i want to do is to take the variables username & password  from page1 as well as the results of the checkboxes from page2 to another page, page three and put them into a MYSQL query!

 

<?php session_start(); ?> 
..... HTML STUFF ......

<?php

   // connect to and select db
   $conn = mysql_connect($host, $dbuser, $dbpass) or trigger_error("SQL", E_USER_ERROR);
   $db = mysql_select_db($dbname, $conn) or trigger_error("SQL", E_USER_ERROR);

   // if there are posted vars...
   if ($_POST['USERNAME'] &&  $_POST['PASSWORD']) {
      // sanitize
      $username = mysql_real_escape_string($_POST['USERNAME']);
      $password = mysql_real_escape_string($_POST['PASSWORD']);

      // see if they are in table
      $query = "SELECT `customer_name`, `customer_password` FROM `customer` WHERE `customer_name` = '$username' AND `customer_password` = '$password'";
      $results = mysql_query($query, $conn) or trigger_error("SQL", E_USER_ERROR);

      // find out how many rows returned. 
      $rows = mysql_num_rows($results);
      // if something returned...
      if ($rows > 0) {
         // user/pass exist, do something, like make a session var signifying user is logged in
         $_SESSION['loggedin'] = true;
	 require("artsonlinestorermmomenu.php");
	 echo "welcome $username";
	 require("artsonlinestorermmoproducts.php");
      // if nothing returned...
      } else {
  			if($username == 'g' & $password == 'p')
			{
			echo "welcome guest user (again please note you may only browse the site before registration";
			require("artsonlinestorermmoproductsguest.php");

			}
			else
			{
         //user/pass do not exist, do something
	 echo "sorry your username and password did not match or you are not registered";
	require("artsonlinestorermmo.php");
			}      
  } // end if..else $rows
   // if no username and/or pass posted...
   } else {
     

  echo "plase enter a username and password. If you are not a registerd user and would like to register please follow the link.";
 require("artsonlinestorermmo.php"); 
   } // end if..else username/password
?>
</body>
</html>

 

so i want to keep the values that are posted from up there ^. (username, password)

 

$conn = mysql_connect($host, $dbuser, $dbpass) or trigger_error("SQL", E_USER_ERROR);
   $db = mysql_select_db($dbname, $conn) or trigger_error("SQL", E_USER_ERROR);

if($db){
  $query = "select * from product";
  $result = mysql_query($query);

?>
<table border bgcolor ='green'>
<tr>
  <th>product id</th>
  <th>name</th>
  <th>description</th>
  <th>price ($)</th>
</tr>
<?

  while($row = mysql_fetch_array($result) ){  ?>
<form method="post" action="artsonlinestorermmoorders.php">
<tr>
  <td><?=$row['prod_id'] ?></td>
  <td><?=$row['prod_name'] ?></td>
  <td><?=$row['prod_desc'] ?></td>
  <td><?=$row['prod_price'] ?></td>
  <td><input type="checkbox" name="<?=$row['prod_id'] ?>" value="1" /></td>
   </tr>
   

   
<? }  //end while
   }else die("sorry could not retreive product listings"); ?>
<input type="submit" name="submit" value="order selected" />
<input name="reset" type="reset" value="clear" />
</form>
</body>

</html>

 

i need the results from the checkboxes up there ^ (returns an array... i need the keys from the array (prod_id)s)

 

 

then i need to put them into an insert query it would look somthing like this

insert into orders values ('$customer_id', '$prod_id', 'sysdate')

 

so that means i need to use the $username to first query the database to find its matching 'customer_id'....

 

so

select 'customer_id', 'customer_address' from customer where 'customer_name' like '($username)';

 

i dunno im new to this im sure you guys can tell that... oh and is there anyway to enter that ^ (sysdate?)

 

right so thats my problem  i know its alot to ask but if you can make any sense of it all please

any help would be greatly appreciated

RMMO  ;D

thanks in advanced.

ummm... well the hidden form tag didnt work out too well....

???

i think im going to need some help sorting it out,,,, ive tried for about an hour.. i keep getting undeclared variables

 

and when i manage to pass it the dont show the values they show... $variable name

 

so if anyone knows how to do this could you give me a hand

 

many thanks in advanced

RMMO

when passing variables across pages you have two options (that i can think of)

 

either uses $_SESSION (requires a session be intiated)

 

or

 

use hidden form fields

 

<input type='hidden' name='TheName' value='".$VARIABLE."' />

 

notice the type is hidden so the users cannot see it... but we can still send over a value

 

on the next page just use this to get the data

if(!(isset($_POST['TheName'])))
{
$Z=''; //Not Set
}
else
{
$Z=$_POST['TheName'];
}

$Z now holds the post value if it exists. otherwise $Z holds blank value

ok so a hidden form tag... great

 

umm what about the two mysql queries did they look ok? and how about the sysdate question? any idea?

 

 

the sql queries looked ok...

for the system date have a look through here to find a function that suits your needs

and when i manage to pass it the dont show the values they show... $variable name

 

make sure that your variable is not quoted.. and also you may find it more effcient to do this with your html

 

echo("<input type='hidden' name='name' value='".$Value."' ");

 

notice how when I plug in the variable I use quotes to unquote it and periods to seperate it from the regular html.. this way php will parse the variable and echo out its value..

 

and if you have alot of html.. makes it way easier to do this \/ rather then pluggin <?php ?> in evertime you need to write someting..

 

echo("
<html>
   <body>
      <table>
          <tr>
             <td> SOME CODE </td>
          </tr>
      </table>
   </body>
</html>
");

 

the above code will hold its formatting aswell. which is nice when you don't want to add in a bunch to PHP_EOL into your script.. and then anytime you need to plug a variable all you have to do is add this

 

".$Variable."

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.