PHP ecommerce website won't link to AR website - php

I'm doing an e-commerce fashion website that is suppose to be integrated with an AR html website. The idea is have a clickable button in the product page that would have the option to display (when clicked) the AR shirt by utilizing the AR website. I already tried redoing the code from scratch but can't seem to find the solution.
Tried redoing the code from scratch. It won't link.
<?php require_once('header.php'); ?>
<?php
if(!isset($_REQUEST['id'])) {
header('location: index.php');
exit;
} else {
// Check the id is valid or not
$statement = $pdo->prepare("SELECT * FROM tbl_product WHERE p_id=?");
$statement->execute(array($_REQUEST['id']));
$total = $statement->rowCount();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
if( $total == 0 ) {
header('location: index.php');
exit;
}
}
/if (isset($_POST['form_virtual_try_on'])) { *use LANG_VALUE on the database*
echo '<form method="POST" action="vto.php">
<input type="submit"/>
</form>';
}/
// here is to initialize the vto --------------------------------------------------------------------------------------------------------
?>

Related

Shopping Cart $_GET problem can't work with the id

I have a problem with $_GET.
On my index.php file I have a little shopping site where I can buy random stuff by clicking the "buy" button. After clicking the "buy" button I am trying to get the id of the clicked product like this:
//Stuff like connection, sql, etc..
$response="";
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
//Other Stuff...
$response .= "<a href='addtocart.php?id=". $row['ProduktID'] . "'><button class='inCart'>Buy</button></a>";
}
And then I have a new file called addtocart.php. I can see the id's:
Image
Image
addtocart.php:
<?php
session_start();
if(isset($_GET['ProduktID']) && !empty($_GET['ProduktID'])){
//Do Stuff
}
else{
echo "Error, can't add the item";
}
I am always getting the Error message..
Here addtocart.php?id you have used id, so in URL it will passed param as id
$response .= "<a href='addtocart.php?id=". $row['ProduktID'] . "'><button class='inCart'>Buy</button></a>";
addtocart.php:
so in php you should access as $_GET['id']
<?php
session_start();
if(isset($_GET['id']) && !empty($_GET['id'])){
//Do Stuff
}
else{
echo "Error, can't add the item";
}

How to remove a row from MySQL table data using html delete button in PHP

I am working on a project, for school. I currently have a product page to display an assortment of item includes image, description and price etc...
Under each product I have a delete button, when logged in as admin, which displays fine.
if (is_admin())
echo '<button>Delete item</button>'; }
I want to know how remove the row of data from MySQL table on clicking the delete button.
<?php
// Include need php scripts
require_once ("Includes/simplecms-config.php");
require_once ("Includes/connectDB.php");
include ("Includes/header.php");
if (!empty($_GET['cat'])) {
$category = $_GET['cat'];
$query = mysqli_query($db, "SELECT * FROM products WHERE category = '".$category."'");
} else {
$query = mysqli_query($db, "SELECT * FROM products");
}
if (!$query) {
die('Database query failed: ' . $query->error);
}
$deleted = mysql_query($db, "DELETE FROM products");
?>
<section>
<div id="productList">
<?php
$row_count = mysqli_num_rows($query);
if ($row_count == 0) {
echo '<p style="color:red">There are no images uploaded for this category</p>';
} elseif ($query) {
while($products = mysqli_fetch_array($query)){
$file = $products['image'];
$product_name = $products['product'$];
$image_id = $products['id'];
$price = $products['price'];
$desc = $products['description'];
echo '<div class="image_container">';
echo '<a href="viewProduct.php?id=' . $image_id . '"><p><img src="Images/products/'.$file.'" alt="'.$product_name.'" height="250" /></p>';
echo '' . $product_name ."</a><br>$" . $price . "<br>" . $desc;
echo '</div>';
if (is_admin()){
echo '<button>Delete item</button>';
}
}
} else {
die('There was a problem with the query: ' .$query->error);
}
mysqli_free_result($query);
?>
</div>
</section>
<?php include ("Includes/footer.php"); ?>
<!-- end snippet -->
You should post to a url with the id in the post data, then redirect back to where you were.
<?php
//html on productpage
if(isset($_GET['product_deleted'])){
if($_GET['product_deleted'] === 'true'){
echo 'The product was deleted';
}else{
echo 'The product could not be deleted';
}
}
if (is_admin()){
/**
* It's a good idea for the page that deletes to be different from the one your on, so that when you redirect back,
* they can refresh the page without getting something
* along the lines of 'refreshing with page will re-post the data'
*/
?>
<form method="POST" action="/product/delete.php">
<button>Delete item</button>
<input type="hidden" name="id" value="<?php echo $image_id; ?>" />
</form>
<?php
}
//PHP on /product/delete.php
if(is_admin() && $_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['id'])){
//delete sql here
header('Location: /productpage.php?product_deleted=true'); //redirect back
}
One approach
Change the button to a a element and make the href look like this:
yourdomain.tld/products/delete/{id}
You have to echo the primary key from your mysql database at the id position. It will look like this:
yourdomain.tld/products/delete/5
Then you have to change your .htaccess in a way that all requests go to your index.php in your root project. At the index.php you can do the actually query then.
Update
Keep in mind that anyone visiting this URL can delete products with this approach. You have to make sure that only the admin can do that. The preferred method is a POST request.
You can also send the primary key parameter to your PHP script you are just showed. With this approach you don't need to edit your .htaccess. You may pass it as an URL parameter like this:
yourdomain.tld/your-script.php?delete-product={id}
In your script you can get the parameter like this:
<?php
if (isset($_GET['delete-product'])) {
// your mysql query to delete the product
} else {
// something else
}
If you want to delete the entire row of an record from your db you can do like this. So that you can pass the product id and delete the row. Just bind the id with query using bind parameters concept
$knownStmt=mysqli_prepare($conn, "DELETE FROM `YourTableName` WHERE `pdt_id` = ?;");
if( $knownStmt ) {
mysqli_stmt_bind_param($knownStmt,"d",$pdt_id);
mysqli_stmt_execute($knownStmt);
mysqli_stmt_close($knownStmt);
}

How to update the data from mysql database with out refreshing the page

I have a website and I need to put a chat box but when someone write the users has to refresh the whole page to read the text but I need to make it automatically update the data please help me.
Now this is the code:
<?
session_start();
include("includes/config.php");
if($_GET['with']){
if($_SESSION['login']){
if($_SESSION['login'] == $_GET['with']){
header("Location: index.php");
}else{
$id = $_SESSION['login'];
$with = intval($_GET['with']);
if($_POST['submit']){
$text = $_POST['text'];
if(empty($text)){
}else{
$query = mysqli_query($connect,"INSERT INTO chat(`from`,`to`,`topic`) VALUES('$id','$with','$text')");
}
}
?>
<form method="post" action="chat.php?with=<?=$with?>">
<textarea name="text" placeholder="Write Here..." style="text-align:right;resize:none;width:100%;height:200px;font-size:24">
</textarea>
<br/>
<input type="submit" name="submit" value="Send"/>
</form>
<div id="chat">
<?
$query = mysqli_query($connect,"SELECT * FROM users WHERE id='$id'");
$f = mysqli_fetch_array($query);
$query = mysqli_query($connect,"SELECT * FROM users WHERE id='$with'");
$ff = mysqli_fetch_array($query);
$query = mysqli_query($connect,"SELECT * FROM chat order by id desc");
while($fetch = mysqli_fetch_array($query)){
if($fetch['from'] == $with && $fetch['to'] == $id or $fetch['from'] == $id && $fetch['to'] == $with){
if($fetch['from'] == $f['id']){
echo "<div style='word-wrap: break-word;'>".$f['fname']." ".$f['lname'].":<br/>".$fetch['topic']."</div>";
}
if($fetch['from'] == $ff['id']){
echo "<div style='max-width:200px;word-wrap: break-word;'>".$ff['fname']." ".$ff['lname'].":<br/>".$fetch['topic']."</div>";
}
}
}?>
</div>
<?}
}else{
header("Location: index.php");
}
}else{
header("Location: index.php");
}
?>
With only using php you cannot achieve what you want. However you can use ajax to accomplish what you asked for. An there is examples with source codes provided. Here is some of them:
https://css-tricks.com/jquery-php-chat/ (i recommend this/ well documented and with source code as well)
http://code.tutsplus.com/tutorials/how-to-create-a-simple-web-based-chat-application--net-5931
http://www.sitepoint.com/9-crazy-jquery-chat-chat-box-plugins/ (lis of ajax/php based chat applicatiosn)
Ajax will help you. But it is a bad practice the way you mix html javascript and php in the same code; Like this, it will be difficult for many people to help you to adapt your code with ajax.

Redirect to another page after button submit

I am building an application where details are displayed (admin.php).The user clicks on edit(edit.php) and he is redirected to another page to edit his details. Once the edit is done the values are updated and I want to redirect to the previous page once again where the details are displayed. All the operations carry out as desired , but when I try to redirect inside the "submit" to the previous page it doesn't occur.
Admin.php
$event_id = $_SESSION['fedit'];
if($_GET['edit'])
{
$url = "edit.php?event_id=". $event_id;
header("Location: $url");
}
Edit.php
if($_GET["Submit"])
{
$val=$_GET['event'];
$remail=$_GET['email'];
$rname = $_GET['ename'];
$sqt=mysql_query("UPDATE users SET NAME='$rname',EMAIL='$remail' WHERE EID ='$val'");
$page= "admin.php";
header("Location: $page");
}
I don't know where you have placed this code on the page but if somehow a http header output is being sent before that header(location:"") then it wont work. Try the suggestions by other people, but if all else fails, you might wanna use JS.
echo "<script>window.location.assign("admin.php")</script>";
Use that instead of
header("Location: $page");
and it should work I guess.
add action='edit.php' method='get' to your form tag.
and in edit.php page place your rest of the code after submit.
Preferably do this:
if ($_GET["Submit"]) {
...
// Clean any output
ob_clean();
header("Location: $page");
// Exit the PHP process
exit;
}
It's very hard to tell what are you doing from the incomplete code you posted.
However, it looks like that not redirect but form submission is broken.
There are also 2 serious flaws in your code:
Whatever edits have to be done using POST method, not GET
you have to properly format your SQL query.
Anyway, here is an example of such an application (though editing only one field. you can easily add more) which works.
<?
mysql_connect();
mysql_select_db("new");
$table = "test";
if($_SERVER['REQUEST_METHOD']=='POST') { //form handler part:
$name = mysql_real_escape_string($_POST['name']);
if ($id = intval($_POST['id'])) {
$query="UPDATE $table SET name='$name' WHERE id=$id";
} else {
$query="INSERT INTO $table SET name='$name'";
}
mysql_query($query) or trigger_error(mysql_error()." in ".$query);
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
exit;
}
if (!isset($_GET['id'])) { //listing part:
$LIST=array();
$query="SELECT * FROM $table";
$res=mysql_query($query);
while($row=mysql_fetch_assoc($res)) $LIST[]=$row;
include 'list.php';
} else { // form displaying part:
if ($id=intval($_GET['id'])) {
$query="SELECT * FROM $table WHERE id=$id";
$res=mysql_query($query);
$row=mysql_fetch_assoc($res);
foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v);
} else {
$row['name']='';
$row['id']=0;
}
include 'form.php';
}
?>
And you will need 2 templates too, to hold your HTML
form.php
<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
Return to the list
</form>
list.php
Add item
<? foreach ($LIST as $row): ?>
<li><?=$row['name']?>
<? endforeach ?>
use header("Location : ". $page); instead of header("Location: $page");
if($_GET["Submit"])
{
$val=$_GET['event'];
$remail=$_GET['email'];
$rname = $_GET['ename'];
$sqt=mysql_query("UPDATE users SET NAME='$rname',EMAIL='$remail' WHERE EID ='$val'");
$page= "admin.php";
header("Location : ". $page);
}
Actually, that is not going to work, isnce your edit.php will never reach its if statement, as you are not sending $_GET["submit"]. You need to do the following (send the submit variable by setting it to any value)
admin.php
$event_id = $_SESSION['fedit'];
if($_GET['edit'])
{
$url = "edit.php?event_id=". $event_id . "&submit=1";
header("Location: $url");
}
edit.php
if($_GET["Submit"])
{
$val=$_GET['event'];
$remail=$_GET['email'];
$rname = $_GET['ename'];
$sqt=mysql_query("UPDATE users SET NAME='$rname',EMAIL='$remail' WHERE EID ='$val'");
header("Location: admin.php");
}

creating two different sessions in PHP

I am trying to create two separate sessions- one for if the user is admin and another if the user is author. $type stored type as enum (can be either author or admin). But my code is creating author session even for admin. I am new to PHP and MySQL . can somebody tell me where the error is in my code.
<?php
include("dbconnect.php");
$con= new dbconnect();
$con->connect();
//create and issue the query
$sql = "SELECT type FROM users WHERE username = '".$_POST["username"]."' AND password = PASSWORD('".$_POST["password"]."')";
$result = mysql_query($sql);
//get the number of rows in the result set; should be 1 if a match
if (mysql_num_rows($result) == 1) {
$type_num=0;
//if authorized, get the values
while ($info = mysql_fetch_array($result)) {
$type =$info['type'];
}
if($type == "admin")
{
$_SESSION['type']=1;
$u = 'welcome.php';
header('Location: '.$u);
}
else
{
$_SESSION['type']=$type_num;
$u = 'welcome.php';
header('Location: '.$u);
}
}
else {
//redirect back to loginfailed.html form if not in the table
header("Location: loginfailed.html");
exit;
}
?>
My welcome.php is as below
<?php
session_start();
?>
<html>
<body>
<h2>Welcome.</h2>
<?
if($_SESSION['type']==1){
echo "You are of the usertype Admin and your session id is ";
echo session_id();
}
else {
echo "You are of the usertype Author and your session id is ";
echo session_id();
}
?>
</body>
</html>
Thank You so much in advance.
Try to use roles for your permissions.
In general you have just one session. I mean you don't have two variables called _SESSION.
With the concept of roles you can simply check if a user has the permission to do something.
You have to call session_start() in the first part of the code, before register the var $_SESSION['type'] in the session
No your code seams fine, I think.
I don't see where you are calling the database
And what you have in there
So here is how you trouble shoot
while ($info = mysql_fetch_array($result)) {
$type =$info['type'];
echo $type . '<br />';
}
OR
echo '<pre>';
while ($info = mysql_fetch_array($result)) {
$type =$info['type'];
print_r($info);
}
echo '</pre>';
If you never see admin in there, and it must be 'admin' not Admin or ADMIN; then the problem is in your database. You don't have admin as admin defined, or spelled right.
By the way. see how nicely I formatted that. It's easier to read that way.
Coders wont look at your code if you don't do that.
Try using session_regenerate_id(); method to create different session ids.

Categories