Jump to content

Two Problems Passing Variable & Select Where Statement

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
msebar

msebar

    Newbie

  • Members
  • Pip
  • 5 posts
I have two problems with the code below. I am having trouble passing the varible in a drop down list ( list does populate correctly) and the where statement is not working correctly even if I place the correct lookup name in the statement. Database connection does work and info is hidden.

The Form:
<body>

  
<p align="center"> </p>
<p align="center"> </p>

<?php
  


include 'config.php';
include 'dbconopen.php';

$Link = mysql_connect ($host, $usr, $pwd); 
$Query = "Select distinct lname from contacts"; 
$results = mysql_db_query($db, $Query, $Link); 

?> 

Which category would you like to select? 
<select name = "drop1" size="1"> 
<Option Value=" ">Select One:</option> 

<? //Begins PHP 
for($u=0;$u<mysql_num_rows($results); $u++) { 
$ID=mysql_result($results,$u,'lname'); 
?> 

<option value="<? echo($ID); ?>"><? echo($ID); ?></option> 
<? //Begins PHP 
}  

?> 
</select> 


<form action="export.php?cat=$ID" method="post">

<input type="Submit">
</form>


<? //Begins PHP 
if (mysql_db_query ($db, $Query, $Link)) { 
print ("link was SUCCESSFUL<BR>\n"); 
} 

else { 
print ("FAILURE<BR>\n"); 
} 

mysql_close ($Link); 

?>


</body>
</html>


________________________________________

The output ( even putting the work Jones in the where statment cause and error

Parse error: syntax error, unexpected $end in export.php on line 43


<?php

 include 'config.php';
 include 'dbconopen.php';
{
$query="SELECT * FROM contacts Where lname=Jones";
if (!$query) { 
die ('invalid query'); 
} 
if ($query){ 
$result = mysql_query ($query) 
or die ("there are no entries"); 


$num=mysql_numrows($result);



echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$id=mysql_result($result,$i,"id");
$fname=mysql_result($result,$i,"fname");
$lname=mysql_result($result,$i,"lname");
$address=mysql_result($result,$i,"address");
$city=mysql_result($result,$i,"city");
$state=mysql_result($result,$i,"state");
$zip=mysql_result($result,$i,"zip");
$email=mysql_result($result,$i,"email");
$source=mysql_result($result,$i,"source");
$ip=mysql_result($result,$i,"ip");
$date_time_stamp=mysql_result($result,$i,"date_time_stamp");

  

echo "<b>Record Number:$id</b><br>Name: $fname $lname<br>Address: $address<br>City: $city<br>State: $state<br>Zip: $zip<br>E-mail: $email<br>Source: $source<br>IP: $ip<br>Date-Time-Stamp: $date_time_stamp<br><hr><br>";

$i++;
}
include 'dbconclosed.php';
	?>

Edited by Orjan, 31 December 2009 - 07:03 AM.
Please use code cags when posting code


#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
try this, it's rewritten for PHP5, as some of your code was deprecated.

<body>

<p align="center"> </p>

<p align="center"> </p>

<?php

include 'config.php';

include 'dbconopen.php';


$Link = mysql_connect ($host, $usr, $pwd); 

$Query = "Select distinct lname from contacts"; 

$results = mysql_query($Query); 


echo 'Which category would you like to select?'; 

echo '<form action="export.php?cat=$ID" method="post">';

echo '<select name="drop1" size="1">';

echo '<Option Value=" ">Select One:</option>';


while ($row = mysql_fetch_array($results)){ 

    echo '<option value="'.$row['lname'].'">'.$row['lname'].'</option>'; 

}  


?> 

</select> 

<input type="Submit">

</form>


</body>

</html>



and the second file:

<?php


include 'config.php';

include 'dbconopen.php';

$query = "SELECT * FROM contacts Where lname='".mysql_real_escape_string($_POST['drop1'])."'";

$result = mysql_query($query) or die("there are no entries");


if (!$result) {

	die('invalid query');

} else {

	$num = mysql_numrows($result);

	echo "<b><center>Database Output</center></b><br><br>";


	while ($row = mysql_fetch_array($result)) {

		echo "<b>Record Number:".$row['id']."</b><br>";

        echo "Name: ".$row['fname']." ".$row['lname']."<br>";

        echo "Address: ".$row['address']."<br>";

        echo "City: ".$row['city']."<br>";

        echo "State: ".$row['state']."<br>";

        echo "Zip: ".$row['zip']."<br>";

        echo "E-mail: ".$row['email']."<br>";

        echo "Source: ".$row['source']."<br>";

        echo "IP: ".$row['ip']."<br>";

        echo "Date-Time-Stamp: ".$row['date_time_stamp']."<br><hr><br>";

	}

	include 'dbconclosed.php';

}

?>


__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
msebar

msebar

    Newbie

  • Members
  • Pip
  • 5 posts
No errors but the drop down box is not populating and when I just hit the submit query button all records show up which I belive it would from the code.

#4
msebar

msebar

    Newbie

  • Members
  • Pip
  • 5 posts
Sorry about that my bad the code works great. I uploaded a new list to the database and forgot to add the lname data.

I hope to give back in the comming months

Once again thanks so much.