I am currently making a product selection guide that with multiple variables. One the first page, there are 4 drop downs that let the user select various options. Here is the code:
// Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$database")or die("cannot select DB");
$total_result = mysql_query( "SELECT* FROM $tbl_name" ) or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($total_result);
// Retrieve data from database $sql="SELECT * FROM $tbl_name WHERE technology = '$tech' AND range = '$range' AND output = '$output' AND configuration='$config'";
$result=mysql_query($sql);
// Start looping rows in mysql database. while($rows=mysql_fetch_array($result)){ ?>
<p><?php echo $rows['product']; ?></p>
<?php // close while loop }
// close connection mysql_close(); ?>
</body> </html>
I believe I am coding my $sql variable. Can you code it like this?
$sql="SELECT * FROM $tbl_name WHERE technology = '$tech' AND range = '$range' AND output = '$output' AND configuration='$config'";
to have it include all the variables from the previous page? Any help would be greatly appreciated.
I'm not sure I follow what your problem is? What is not working?
NB your current code leaves you open to SQL injection, you're not doing any validation, cleaning or defensive coding at all. Also there is also no need at all for this:
I'm not sure I follow what your problem is? What is not working?
NB your current code leaves you open to SQL injection, you're not doing any validation, cleaning or defensive coding at all. Also there is also no need at all for this:
Why is it needed for the SQL? It's just assigning one variable to another. There is no reason at all to do this. The code could just be written as:
PHP Code:
$sql="SELECT * FROM $tbl_name WHERE technology = '{$_GET['tech']}' AND range = '{$_GET['range']}' AND output = '{$_GET['output']}' AND configuration='{$_GET['config']}'";
This saves 4 pointless variable re-assignments. I don't understand why so many people write code which re-assigns $_GET and $_POST values without performing any operation on them.
When I submit the form on config.php, here is the URL: http://10.0.0.123/selection/product_...&Submit=Search. So I know the variables are being passed. When I look at the sql, it matches those variables (i have them set up as INT columns). The product result does not display though. It is just a blank screen
Here is the error that it reported instead of a blank screen
Notice: Undefined variable: host in C:\Program Files\Apache Group\Apache2\htdocs\selection\product_view.php on line 10
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\selection\product_view.php on line 23
yes i use that all the time sometimes hosts don't setup their servers to display all php errors, this is the workaround for that doomed blank white screen! !
Ok switch the line of code with the one you suggested, now I get this error (I think the same as above, just spaced different).
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\selection\product_view.php on line 23
// Connect to server and select database.
mysql_connect($hostname, $username, $password)or die("cannot connect");
mysql_select_db($database)or die("cannot select DB");
$total_result = mysql_query( "SELECT* FROM $tbl_name" )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($total_result);
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE technology = '{$_GET['technology']}' AND range = '{$_GET['range']}' AND output = '{$_GET['output']}' AND configuration='{$_GET['configuration']}'";
$result=mysql_query($sql);
// Start looping rows in mysql database.
while ($rows=mysql_fetch_assoc($result)) {
?>
Bookmarks