Trying to send a php request via hyperlink to other frame - php

I am a novice to HTML, JavaScript, PHP and pretty much programming in general and I've run into a bit of a problem with a basic shopping cart/e-commerce site I am trying to build.
The main page has a navigation tree in the left frame (before you say "why are you using frames!!!" I know.... My professor for some reason requires us to use outdated techniques in his assignments...). The top right frame contains a product detail which when will show product details when you click on the lowest tier (actual products) of the navbar. When I actually define the product_id in the query I am able to populate the product details table with the correct information. However, when I click on the actual navigation bar links, it doesn't send the product_id to that page for the query.
I'm pretty sure I'm missing something fundamental here, and unfortunately I can't proceed with actually trying to teach myself how to populate the cart until I can get the product details up on the page! Heavy....
Anyway, here is the way I've formatted the links: i.e:
<li>Chocolate Bar
</li>
And here is the php I have on the products page:
<?php
$product_id = $REQUEST['product_id'];
$query_string = "select product_name, unit_quantity, unit_price from products
where (product_id = $product_id)";
$result = mysql_query($query_string,$link);
$num_rows = mysql_num_rows($result);
if ($num_rows > 0 ) {
print "<table border='0'>";
while ($a_row = mysql_fetch_assoc($result) ) {
print "<tr>\n";
print "<td>$a_row[product_name]</td>";
print "<td>$a_row[unit_quantity]</td>";
print "<td>$a_row[unit_price]</td>";
print "</tr>";
}
print "</table>";
}
mysql_close($link)
?>
What am I doing wrong here? I feel like I'm maybe missing some critical PHP on the main page to tell it to send that information over and maybe missing something on the products page to receive it?

Change this
$product_id = $REQUEST['product_id'];
to this
$product_id = $_REQUEST['product_id'];
or this
$product_id = $_GET['product_id'];
You may go through this manual for further information.
Suggestion, unrelated, refrain yourself from using mysql_* functions because they are deprecated and will be discontinued in future.

Try $_GET instead of REQUEST
and try without the ()'s
$query_string = "select product_name, unit_quantity, unit_price from products
where product_id = $product_id";

the issue is in the query, $product_id must be inside another pair of quotes caouse thats the string that gouse to MYsql
$query_string = "select product_name, unit_quantity, unit_price from products
where (product_id = '".product_id."'")";
$result = mysql_query($query_string,$link);

You could indead use REQUEST put you should write it like $_REQUEST. To prevent future errors though you could also use $_GET.
$product_id = $_GET['product_id'];
$query_string = "select product_name, unit_quantity, unit_price from products
where (product_id = '".$product_id."')";

Related

What PHP do I need to insert into this mysqli_fetch_array to ensure NULL urls aren't used?

I have a webpage which uses php to pull through data from a MySQL database. The database stores my writing portfolio organised by columns date, url, category, title, publication, description. It pulls through the title and publication and uses the url to turn it into a hyperlink (see code below). So far, so good.
Where I'm stuck: Sometimes there's no url for something I've published (e.g. for a few of the 'Content & Copywriting' items on my site), but it still turns my title and publication into a link: a link to "_blank". When there is a NULL url, I need it to not turn my title into a link at all.
So, I think I need to insert some sort of logic so that if url=NULL, then it doesn't make the text into a link, instead of creating a link to "_blank". But this is completely beyond my capabilities right now - I don't know where to even start !
I hope that all makes sense. Any guidance/pointers in the right direction welcome! And please let me know if anything's not clear.
<?php
$query = mysqli_query($dbconnect, "SELECT * FROM main WHERE category = 'Content & copywriting' ORDER BY date DESC")
or die (mysqli_error($dbconnect));
while ($row = mysqli_fetch_array($query)) {
echo "<a href=$row[url] target='_blank'>$row[publication] - $row[title]</a><br>";
echo "$row[description]<br><br>";
}
?>
try check for empty values
while ($row = mysqli_fetch_array($query)) {
if( !empty( $row['url'])){
echo "<a href=$row[url] target='_blank'>$row[publication] - $row[title]</a><br>";
}
echo "$row[description]<br><br>";
}

Performing a MYSQL query based off of $_GET results

When a user clicks an item on my items page, it takes them to blank page template using $_GET to pass the item brand and model through.
I'd like to perform another MYSQL query when that user clicks through to populate the blank page with the product details from my database. I'd like to retrieve the single row using the model number (unique ID) to populate the page with the information. I've tried a couple of things but am having a little difficulty.
On my blank item page, I have
$brand = $_GET['Brand'];
$modelnumber = $_GET['ModelNumber'];
$query = mysql_query("SELECT * FROM items WHERE `Model Number` = '$modelnumber'");
$results = mysql_fetch_row($query);
echo $results;
I think having ''s around Model Number is causing troubles, but without them, I get a Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given error.
My database columns looks like
Brand | Model Number | Price | Description | Image
A few other things I have tried include
$query = mysql_query("SELECT * FROM item WHERE Model Number = $_GET['ModelNumber']");
Which gave me a syntax error. I've also tried concatenating the $_GET which gives me a mysql_fetch_row() expects parameter 1 to be resource, boolean given error
Which leads me to believe that I'm also going about displaying the results incorrectly. I'm not sure if I need to put it in a where loop like I have with my previous page which displays all items in the database because this is just displaying one.
It seems that your "result" is pulling in too much information for a single variable.
Also, I don't think your table should have column names with spaces, I would suggest filling them all with dashes or underscores.
Here's my suggestion:
$qry = mysql_query("SELECT * FROM items WHERE Model_Number = '$modelnumber'"); //REMEMBER TO MAKE THE CHANGE "Model Number" -> "Model_Number"
while($row = mysql_fetch_array($qry)){
$brand = $row['Brand'];
$modelnumber = $row['Model_Number'];
$price = $row['Price'];
$description = $row['Description'];
$image = $row['Image'];
}
This will populate all of these variables with whatever you have in your tables.
First, notice the first comment on your question. You definitely want to add some sort of sanitation, mysql_real_escape_string at the very least, although PDO or Mysqli would be preferred.
Second, before getting a real answer to your question, let's get some more information about your error.
Try the following:
$brand = mysql_real_escape_string($_GET['Brand']);
$modelnumber = mysql_real_escape_string($_GET['ModelNumber']);
$query = mysql_query("SELECT * FROM items WHERE `Model Number` = '$modelnumber'")OR die(mysql_error());
$results = mysql_fetch_row($query);
var_dump($results);
mysql_error tell us what is going on behind the scenes.
If you are using mysql_fetch_row it will only returns a numerical array of strings that corresponds to the fetched row, or FALSE if there are no more rows http://php.net/mysql_fetch_row
If you need to retrieve the data inside that row. You need to use mysql_fetch_array()
also try using mysql_error();
$results = mysql_fetch_row($query) or die(mysql_error());
you will see the error output produce by your code

Pulling data from one table, modifying the data, and inserting it into two others?

I'm trying to figure out how to modify data I copy from one table into another two.
Currently I have it working so my form submits it pulls the data from 'prices' and puts it into 'pricestwo' and 'pricesthree'.
However I need to modify the data before hand to make one 10% less on price, and one 20% less on price.
<?php
$priceid = $_POST['priceid'] ;
$name = $_POST['productname'] ;
$weight = $_POST['productweight'];
$price = $_POST['productprice'];
if(isset($_POST['updateprices'])) {
for($i=0;$i<$count;$i++){
$sql1= mysqli_query($myConnection, "UPDATE pricestwo SET productname='$name[$i]', productweight='$weight[$i]', productprice='$pricetwo[$i]' WHERE priceid='$priceid[$i]'");
$sql2= mysqli_query($myConnection, "UPDATE pricesthree SET productname='$name[$i]', productweight='$weight[$i]', productprice='$price[$i]' WHERE priceid='$priceid[$i]'");
}
echo "<meta http-equiv=\"refresh\" content=\"0;URL=edit_product_prices.php\">";
}
?>
This currently works to copy just the data, I've tried something like:
$pricetwo = $price - ($price * 0.15);
to modify the data, but there are multiple rows been transferred so this just returns a 0.00 value.
Anyone have any ideas on how to do this?
Write a stored procedure for the set of queries and execute the whole in one database call. It makes a better code and improves performance.
By the way, check if you trying to modify a variable instead of array element.

php for loop query in magento

Hello all i am new but got so many helps from here. Thanks to all the people who helped.
Now i am facing a problem in magento e-commerce for making a loop query of top 10 point holders. Well i have made a separate database which contain the points .
my database is table : magentodatabase_points
Now here is total 3 colums -
1. ID
2. User_id
3. points
well now, here i would like to make a loop of top 10 members contain the highest points i have tried but failed so can anybody help me on that.
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$table = $resource->getTableName('database_points');
$query = 'SELECT user_id FROM ' . $table . ' ORDER BY points DESC LIMIT 10';
$results = $readConnection->fetchAll($query);
well but after that i have tried with php for loop but that is not supporting for magento. So can anybody know what will be the loop code for magento.
Well, one more help would be greatful for me how do i get magento customer names from the using the top results of user_id because i will get only the results not the names.
Anyways that is not that important because i hope i will solve it by my own.
So, please if anyone can help me on the loop query of top 10 point holders.
Thanks in advace
and sorry for my bad english
If your table name and fields exist then you can just add this below your code:
foreach ($results as $result) {
$user_id = $result['user_id'];
echo $user_id; // or do whatever you wanted to with the variable
}
You should consider using Magento's Models to do this instead. There's an excellent tutorial series from Alan Storm on MagentoCommerce's website: http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-basics
For the customer's names you can try this in your loop:
foreach ($results as $result) {
$user_id = $result['user_id'];
$customer = Mage::getModel('customer/customer')->load($user_id);
print_r($customer);
}
May be because your field name has uppercase for u of user_id correctd "User_id" and you are using lowercase in your query.
else your method is fine to work with magento
for gettting customer, if User_id u get from this result is a customer id then $customer_data = Mage::getModel('customer/customer')->load(User_Id); would give u all detail of customer. you can fetch name from this using $customer_data->getName() method

Php with html: creating master variable

I have a page that is querying a database quite a few times using php with that's being repeated; the query blocks are interspersed with html in between as well. An example is
<?php
--code here--
SELECT DISTINCT `Retailer`
FROM `product`
WHERE `Product` = 'iPad 2'
--continue code here--
?>
html content here
<?php
--code here--
SELECT DISTINCT `Brand`
FROM `product`
WHERE `Product` = 'iPad 2'
--continue code here--
?>
This is repeated a few times. This page that the queries sit on will need to duplicated for other pages but the "Product" line have to change (for example, Product = 'iPhone'). Currently, each of the queries is located within separate php code blocks so I have to go to each place where it is referenced to change it. Is it possible to have one location at the top of the document that I can change? If so, how do I do this?
I personally like using the $_GET variable for things like this, using mod_rewrite to generate necessary URLs that don't include the variables. I assume that some of your html changes along with the WHERE line of your query, too. You could try something like this:
Links to product pages
<ul>
<li>iPad 2</li>
<li>iPhone 4S</li>
</ul>
The products/index.php document
<?php
$product = $_GET['product'];
//various code
$retailer_result = mysql_query("SELECT DISTINCT Retailer FROM product WHERE Product = '$product'");
//more code
//define and require the html document
$product_page_dir = "products/pages/";
$product_page_path = $product_page_dir . $product;
require $product_page_path;
//more various code
$brand_result = mysql_query("SELECT DISTINCT Brand FROM product WHERE Product = '$product'");
//the rest of the code
This is the simplest way of doing it using this method. Don't forget to establish the connection MySQL and select the database before any queries. Also keep in mind that you should also probably include error handling. Note: the code above is untested.
You probably mean to write something like a function that you can reuse:
function get_product($column, $name) {
return db("SELECT DISTINCT `$column` FROM `product` WHERE `Product` = ?", $name);
}
Then just set a variable $product_name atop your page, and reuse that for issueing the queries. (If that's what you meant.)
$product_name = "iPad 2";
...
get_product("Retailer", $product_name);
...
get_product("Brand", $product_name);
...
get_product("Price", $product_name);

Categories