Jump to content

How do I connect my MYSQL to my webshop?

AntonDVries

Sup guys,

 

I got an assignment to do for school which concluded developing a webshop. I got the website with al the articles on it and I got the database (MYSQL). The only question now is how to connect those two.

 

I really appreciate it.

 

Greetz

Link to comment
Share on other sites

Link to post
Share on other sites

With some PHP.

 

With the PHP code you can basically read on MySQL databases, so you can make the stuff appear as you want (product names, prices, etc. etc.) and also handle user accounts (keep in mind actual sites have way more security on this stuff).

Don't forget to rename your .html file a .php. To see PHP code, the file needs to have a .php extension. 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

43 minutes ago, Minibois said:

With some PHP.

 

With the PHP code you can basically read on MySQL databases, so you can make the stuff appear as you want (product names, prices, etc. etc.) and also handle user accounts (keep in mind actual sites have way more security on this stuff).

Don't forget to rename your .html file a .php. To see PHP code, the file needs to have a .php extension. 

I got that. But I got the products "I want to sell" already in the website, do I have to link each product one by one or is there a faster way?

Link to comment
Share on other sites

Link to post
Share on other sites

23 minutes ago, AntonDVries said:

I got that. But I got the products "I want to sell" already in the website, do I have to link each product one by one or is there a faster way?

This is probably something you should have thought about, if you knew you had to implement the PHP with the products too.

 

The store page should be linked to your back end system, so you can easily change the price (instead of having to edit every .html page whenever there's a sale for example) and so you can display if the item is in stock.

I know this is a school project, but these kind of things are important to real projects.

 

But uhh, I wouldn't know of an easier way than manually doing it I guess.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, AntonDVries said:

I got that. But I got the products "I want to sell" already in the website, do I have to link each product one by one or is there a faster way?

well fastest way to do this would have been to add all your products to the database to start with and then return that using PHP.

 

Personally I would return JSON from a get and then use angular and ng-repeat to get all your products on the page but that might be a bit above where you're at.

 

You don't need to code it for every single page, I would create a class in php and then include it in the page and then call the function to do all you're queries.

 

db.php

<?php
class db {
  private static $handle;
  public static function connect($host, $username, $password, $db){
  self::$handle = new PDO("mysql:host={$host};dbname={$db}", $username, $password);
  self::$handle->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
  }
  public static function query($sql, $args=null){
  $sth = self::$handle->prepare($sql);
  $sth->execute($args);
  return $sth;
  }
//end of class
}

db::connect("host", "username", "password", "database");

?>


Then in your pages

include(db.php);

$sql = db::query("select * from products");

if($sql->rowCount()>0){
  //this is where you will echo out the data for the products
//assumign you have rows name and price
	while($row = $sql->fetch(PDO::FETCH_ASSOC)){
		echo $row['name'];
		echo $row['price'];
    }
}

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, vorticalbox said:

well fastest way to do this would have been to add all your products to the database to start with and then return that using PHP.

 

Personally I would return JSON from a get and then use angular and ng-repeat to get all your products on the page but that might be a bit above where you're at.

 

You don't need to code it for every single page, I would create a class in php and then include it in the page and then call the function to do all you're queries.

 

db.php


<?php
class db {
  private static $handle;
  public static function connect($host, $username, $password, $db){
  self::$handle = new PDO("mysql:host={$host};dbname={$db}", $username, $password);
  self::$handle->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
  }
  public static function query($sql, $args=null){
  $sth = self::$handle->prepare($sql);
  $sth->execute($args);
  return $sth;
  }
//end of class
}

db::connect("host", "username", "password", "database");

?>


Then in your pages


include(db.php);

$sql = db::query("select * from products");

if($sql->rowCount()>0){
  //this is where you will echo out the data for the products
//assumign you have rows name and price
	while($row = $sql->fetch(PDO::FETCH_ASSOC)){
		echo $row['name'];
		echo $row['price'];
    }
}

 

Thnx helped a lot, and how about pictures of the products? What do I have to do when I want to include those as well? Sorry for all those noob questions, I'm new to this.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, AntonDVries said:

Thnx helped a lot, and how about pictures of the products? What do I have to do when I want to include those as well? Sorry for all those noob questions, I'm new to this.

In your db table you can add another column that is just a plain text link to your image file. Then you can just use it the same way you get name and price in the above example

 

echo '<img src="' . $row["prodcutImage"] . '">';

 

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, vorticalbox said:

well fastest way to do this would have been to add all your products to the database to start with and then return that using PHP.

 

Personally I would return JSON from a get and then use angular and ng-repeat to get all your products on the page but that might be a bit above where you're at.

 

You don't need to code it for every single page, I would create a class in php and then include it in the page and then call the function to do all you're queries.

 

db.php


<?php
class db {
  private static $handle;
  public static function connect($host, $username, $password, $db){
  self::$handle = new PDO("mysql:host={$host};dbname={$db}", $username, $password);
  self::$handle->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
  }
  public static function query($sql, $args=null){
  $sth = self::$handle->prepare($sql);
  $sth->execute($args);
  return $sth;
  }
//end of class
}

db::connect("host", "username", "password", "database");

?>


Then in your pages


include(db.php);

$sql = db::query("select * from products");

if($sql->rowCount()>0){
  //this is where you will echo out the data for the products
//assumign you have rows name and price
	while($row = $sql->fetch(PDO::FETCH_ASSOC)){
		echo $row['name'];
		echo $row['price'];
    }
}

 

Thnx helped a lot, and how about pictures of the products? What do I have to do when I want to include those as well? Sorry for all those noob questions, I'm new to this

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, AntonDVries said:

Thnx helped a lot, and how about pictures of the products? What do I have to do when I want to include those as well? Sorry for all those noob questions, I'm new to this

 

5 hours ago, Hazy125 said:

In your db table you can add another column that is just a plain text link to your image file. Then you can just use it the same way you get name and price in the above example

 


echo '<img src="' . $row["prodcutImage"] . '">';

 

see this answer. You could store the image in the database but it's really not worth it when as said a link to the image will do.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×