Current location: Hot Scripts Forums » Programming Languages » PHP » help passing variable via form


help passing variable via form

Reply
  #1 (permalink)  
Old 04-20-04, 07:58 PM
move3rd move3rd is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
help passing variable via form

I'm trying to pass the $artist variable from this form:

PHP Code:

<form action="admin_edit_artist.php" method="P0ST">

<select name="artist">
<?       
include "db.php";
      
$sql "SELECT * FROM `users` WHERE `user_group` = 'artist' ";

$result mysql_query($sql);

if (!
$result) {
   echo 
"Could not successfully run query ($sql) from DB: " mysql_error();
   exit;
              }

if (
mysql_num_rows($result) == 0) {
    echo 
"<h4 align=center>Could not find any artists!</h4>";  
   exit;
                                  }

 while (
$row mysql_fetch_array($result))
  {
extract($row);

 echo 
"<option>".$artist."</option>"

  }
mysql_free_result($result);
mysql_close($conn);
?> 
</select> 
<input name="submit" type="submit" value='Select'>
  </form>
to this script:
PHP Code:

<? // start the session 

session_start(); 
header("Cache-control: private"); //IE 6 Fix 
?>
 
<?

// Asign session variables
$username $_SESSION['username'];

$artist addslashes ($_POST['artist']); 
?>
<table width="100%" border="0" cellpadding="5">
  <tr> 
    <td width="41%">Welcome <? echo "$username"?></td>
    <td width="59%"><div align="right"> </div></td>
  </tr>
  <tr> 
    <td height="29"><a href="index.php">Home</a> : <a href="edit_profile.php">Profile</a> 
      : <a href="logout.php"> Log-out</a></td>
    <td><div align="right"></div></td>
  </tr>
</table>
 <? 
      
include "db.php";

if (
$_SERVER['REQUEST_METHOD'] == "POST") {
                                              

$sql "SELECT * FROM `cds` WHERE `artist` = '$artist'";
                                          } 
$result mysql_query($sql);

if (!
$result) {
   echo 
"Could not successfully run query ($sql) from DB: " mysql_error();
   exit;
              }

if (
mysql_num_rows($result) == 0) {
    echo 
"<h4 align=center>Could not find any CD's by ".$artist."!</h4>";  
   exit;
                                  }
?>
<table width="700" cellpadding="5" cellspacing="0">
  <tr bgcolor="#F2F2F2"> 
    <td width="119">Title </td>
     
    <td width="82">
<div align="left">Status</div>
    </td>
    <td width="191">Promotion</td>
    <td width="44">Price</td>
    <td width="67">&nbsp;</td>
    <td width="135">&nbsp;</td>
  </tr>
  <? while ($row mysql_fetch_array($result))
  {
extract($row);
//If CD's are availble print Cd's

    
       
echo "<tr><td>".$cd_title."</td>";
       echo 
"<td>".$cd_status."</td>";
       echo 
"<td>".$cd_promotion."</td>";
       echo 
"<td>".$cd_price."</td>";
       echo 
"<td><div align='right'>";
       echo 
"<form action='edit_cd.php' method='POST'>";
       echo 
"<input type='hidden' name='cd_title' value='".$cd_title."'>"
       echo 
"<input name='POST' type='submit' value='Edit'>";
       echo 
"</form></div></td>";
       echo 
"<td><form name=\"tracks\" method=\"POST\" action=\"tracks.php\">"
       echo 
"<input type='hidden' name='cd_title' value='".$cd_title."'>"
       echo 
"<input type='hidden' name='artist' value='".$artist."'>"
       echo 
"<input type=\"submit\" name=\"Submit\" value=\"Tracks\"></form>"
  }
 
mysql_free_result($result);
mysql_close($conn);
 
?>
  </td>
 </tr>
</table>
I'm getting this error:

Could not successfully run query () from DB: Query was empty
Reply With Quote
  #2 (permalink)  
Old 04-20-04, 08:45 PM
Addict's Avatar
Addict Addict is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Ohio
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

echo "<option>".$artist."</option>"
should be...
PHP Code:

echo "<option value=\"".$artist."\">".$artist."</option>"
Reply With Quote
  #3 (permalink)  
Old 04-20-04, 09:08 PM
move3rd move3rd is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for checking it out for us but that isnt the problem. <option>$artist</option> the the value is auto set the same as name.
Reply With Quote
  #4 (permalink)  
Old 04-21-04, 05:42 AM
Addict's Avatar
Addict Addict is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Ohio
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by move3rd
Thanks for checking it out for us but that isnt the problem. <option>$artist</option> the the value is auto set the same as name.
According the code posted above, its not. You need to have the value attribute in the option tag in order to receive any value for POST. You will still see $_POST['artist'] look like it has been assigned (which it has), but it will be empty. The $artist POST variable is the name of the SELECT box with the OPTION value attribute being the value.

Hopefully that makes sense.
Reply With Quote
  #5 (permalink)  
Old 04-21-04, 06:32 AM
move3rd move3rd is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
I'm still getting the same error. I've spent 2 days trying to figure this out now!
Reply With Quote
  #6 (permalink)  
Old 04-21-04, 06:57 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
I think I know what is the porblem !
these lines always return false !
PHP Code:

if ($_SERVER['REQUEST_METHOD'] == "POST") { 

                                               

$sql "SELECT * FROM `cds` WHERE `artist` = '$artist'"
                                       } 
so after this IF statment you are asking to excute $sql which wasn't declared because of this IF statment !
so I suggest you change these lines into:
PHP Code:

if (isset($_POST['artist'])) { 

$sql "SELECT * FROM `cds` WHERE `artist` = '$artist'";

this one should work !

also I suggest that you don't close the IF statment just after you declare the variable sql, instead close it at the end of the script to avoid useless processing when $_POST['artist'] is not set !

my suggestion is to change it to this:
PHP Code:

if (isset($_POST['artist'])) { 

$artist = trim($_POST['artist']);
$sql = "SELECT * FROM `cds` WHERE `artist` = '$artist'";

$result = mysql_query($sql); 

if (!$result) { 
   echo "Could not successfully run query ($sql) from DB: " . mysql_error(); 
   exit; 
              } 

if (mysql_num_rows($result) == 0) { 
    echo "<h4 align=center>Could not find any CD's by ".$artist."!</h4>";   
   exit; 
                                  } 
?> 
<table width="700" cellpadding="5" cellspacing="0"> 
  <tr bgcolor="#F2F2F2"> 
    <td width="119">Title </td> 
      
    <td width="82"> 
<div align="left">Status</div> 
    </td> 
    <td width="191">Promotion</td> 
    <td width="44">Price</td> 
    <td width="67">&nbsp;</td> 
    <td width="135">&nbsp;</td> 
  </tr> 
  <? while ($row mysql_fetch_array($result)) 
  { 
extract($row); 
//If CD's are availble print Cd's 

     
       
echo "<tr><td>".$cd_title."</td>"
       echo 
"<td>".$cd_status."</td>"
       echo 
"<td>".$cd_promotion."</td>"
       echo 
"<td>".$cd_price."</td>"
       echo 
"<td><div align='right'>"
       echo 
"<form action='edit_cd.php' method='POST'>"
       echo 
"<input type='hidden' name='cd_title' value='".$cd_title."'>"
       echo 
"<input name='POST' type='submit' value='Edit'>"
       echo 
"</form></div></td>"
       echo 
"<td><form name=\"tracks\" method=\"POST\" action=\"tracks.php\">"
       echo 
"<input type='hidden' name='cd_title' value='".$cd_title."'>"
       echo 
"<input type='hidden' name='artist' value='".$artist."'>"
       echo 
"<input type=\"submit\" name=\"Submit\" value=\"Tracks\"></form>"
  } 

mysql_free_result($result); 
mysql_close($conn); 

}
?>
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #7 (permalink)  
Old 04-21-04, 08:21 AM
move3rd move3rd is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
I'm totaly lost now! I did as you suggested and it's now out-putting a blank page.

I'm totaly lost.
Reply With Quote
  #8 (permalink)  
Old 04-21-04, 09:44 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
ok then, just change the first if statment into :
PHP Code:

if (isset($_POST['artist'])) {

  
$sql "SELECT * FROM `cds` WHERE `artist` = '$artist'";

when blank pages appear it means that some of the IFs is wrong ! which usually makes me rewrite the whole if statment from scratch !
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #9 (permalink)  
Old 04-21-04, 10:02 AM
move3rd move3rd is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Back to the original error:

Could not successfully run query () from DB: Query was empty
Reply With Quote
  #10 (permalink)  
Old 04-21-04, 10:15 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
well, try this if instead:
PHP Code:

if ($artist) {

   
$sql "SELECT * FROM `cds` WHERE `artist` = '$artist'";

__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 04-21-04 at 10:22 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
formmail problem gscraper Perl 12 08-27-04 03:06 AM
Passing value from one form to another form sunilmzp ASP.NET 0 04-05-04 04:02 AM
Dreamweaver changes form variable when a cut/paste is done Thor135 The Lounge 2 02-20-04 12:37 PM
passing values from one form to another -urgent aspuser25 ASP 1 09-19-03 06:41 PM
Passing value from one form to another form sasi ASP 2 08-29-03 11:38 PM


All times are GMT -5. The time now is 10:27 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.