How to send exact data from one table to another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brada257
    New Member
    • Aug 2019
    • 2

    How to send exact data from one table to another

    Hey everyone, I'm working on a small webshop project. I have register and login form which are working fine and are storing data into table users. Now, I have another table: orders, in which I want to transfer data from logged user when he makes order.

    users data: id, username, email, password, address, city, country

    orders data: id, username, email, address, city, country, item, quantity, price, total_price (where i want firt 6 data to be the same as user which ordered something from the shop)

    this is the php code in index.php regarding storing the data in orders table

    Code:
    <?php
    	require_once('connect.php');
      $username2 = $_SESSION['username'];
      $email = $_SESSION['email'];
      $address = $_SESSION['address'];
      $city = $_SESSION['city'];
      $country = $_SESSION['country'];
    
    
    	if (isset($_POST['purchase_btn'])) 
      {
        $sql = "SELECT * FROM users WHERE username='$username2'";
        $result = mysqli_query($conn, $sql);
    
        if(mysqli_num_rows($result) > 0)
        {
        while($row = mysqli_fetch_assoc($result))
        {
          $id = $row["id"];
          $username2 = $row["username"];
          $email = $row["email"];
          $address = $row["address"];
          $city = $row["city"];
          $country = $row["country"];
        }
        $item = $_POST['item'];
        $quantity = $_POST['quantity'];
        $price = $_POST['price'];
        $total_price = $_POST['total_price'];
    
        $sql = "INSERT INTO orders (username, email, address, city, country, item, quantity, 
                price, total_price) VALUES ('$username2', '$email', '$address', '$city', 
                '$country', $item, $quantity, $price, $total_price);";
        mysqli_query($conn, $sql);
    
      }
    }
    ?>
    There is no error codes, it just does nothing when clicked on purchase_btn

    any help would be lovely, thanks :)
  • brada257
    New Member
    • Aug 2019
    • 2

    #2
    Update: every code file in my project. Is it maybe something to do with the js code for cart section that needs to be refrenced? Because cart is empty when you open the index.php file and through js it is stored in shop section (add to cart button)

    index.php

    Code:
    <!--Zadnje dodano: PHP kod (potrebna provjera jel dobro, svi ovi values dodani u kodu za svaku stvar (dal valja?) )-->
    
    
    <!DOCTYPE html>
    <html lang=en>
    	<head>
    		<!-- Required meta tags always come first -->
    		<meta charset="utf-8">
    		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    		<meta http-equiv="x-ua-compatible" content="ie=edge">
    
    		<!-- Bootstrap CSS -->
    		<link rel="stylesheet" href="css/bootstrap.min.css">
    
    		<!-- Custom Style CSS-->
    		<link rel="stylesheet" type="text/css" href="style.css">
    		<script src="store.js" async></script>
    	</head>
    
    <!--**************************************SPAJANJE NA BAZU****************************************************************************************-->
    <?php
    	require_once('connect.php');
      $username2 = $_SESSION['username'];
      $email = $_SESSION['email'];
      $address = $_SESSION['address'];
      $city = $_SESSION['city'];
      $country = $_SESSION['country'];
    
    
    	if (isset($_POST['purchase_btn'])) 
      {
        $sql = "SELECT * FROM users WHERE username='$username2'";
        $result = mysqli_query($conn, $sql);
    
        if(mysqli_num_rows($result) > 0)
        {
        while($row = mysqli_fetch_assoc($result))
        {
          $id = $row["id"];
          $username2 = $row["username"];
          $email = $row["email"];
          $address = $row["address"];
          $city = $row["city"];
          $country = $row["country"];
        }
        $item = $_POST['item'];
        $quantity = $_POST['quantity'];
        $price = $_POST['price'];
        $total_price = $_POST['total_price'];
    
        $sql = "INSERT INTO orders (username, email, address, city, country, item, quantity, price, total_price) VALUES ('$username2', '$email', '$address', '$city', '$country', $item, $quantity, $price, $total_price);";
        mysqli_query($conn, $sql);
    
    	 /*$_SESSION['message'] = "Thank you for your purchase! You can expect your order in next 3-5 working days. Team MFDAGS :)";*/
      }
    }
    ?>
    
    
    <body>
    <!--*******************************************COVER SECTION***************************************************************************************-->
    	<section id="cover">
    		<div id="cover-caption">
    			<div class="container">
    				<div class="col-sm-12 col-sm-offset-1">
    					<h1 class="display-3">Welcome <?php echo "$username2"; ?> to MFDAGS Official website</h1> <!--MOĊ½DA NE RADI-->
    					<p class="front">Determine who you are and what your brand is, and what you're not. The rest of it is just a lot of noise.</p>
    							<button id="btnshop" class="btn btn-success btn-lg">Shop!</button>
    					<a href="#yogi-shop" class="btn btn-secondary-outline btn-sm" role="button">&darr;</a>
    				</div>
    			</div>
    		</div>
    	</section>
    <!--*******************************************NAV BAR***********************************************************************************************-->
    	<nav class="navbar navbar-expand-lg navbar-light" style="background-color: #e3f2fd;" id="nav-main">
      		<img class="navbar-brand" src="photos/showcase.png" height="120px" width="120px">
      		<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        		<span class="navbar-toggler-icon"></span>
      		</button>
    
      		<div class="collapse navbar-collapse" id="navbarSupportedContent">
        		<ul class="navbar-nav mr-auto">
          			<li class="nav-item active">
            			<a class="nav-link" href="#cover">Home <span class="sr-only">(current)</span></a>
          			</li>
          			<li class="nav-item">
            			<a class="nav-link" href="#about">About Us</a>
          			</li>
          			<li class="nav-item dropdown">
            			<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Shop now 
            			</a>
            				<div class="dropdown-menu" aria-labelledby="navbarDropdown">
              					<a class="dropdown-item" href="#yogi-shop">Yogi Collection</a>
              					<a class="dropdown-item" href="#badger-shop">Badger Collection</a>
              					<a class="dropdown-item" href="#owl-shop">Owl Collection</a>
            				</div>
          			</li>
          			<li class="nav-item">
            			<a class="nav-link" href="#kosarica">Cart</a>
          			</li>
        		</ul>
            <div class="dropdown navbar-right" id="right">
              <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown"><?php echo $username2;?>
                <span class="caret"></span></button>
                <ul class="dropdown-menu">
                  <li><a href="logout.php">Logout</a></li>
                </ul>
            </div>
        		<form class="form-inline my-2 my-lg-0" action="" id="login-register-form" method="post">
        			<li class="nav-item">
            			<a class="nav-link" href="login_form.php">Login here</a>
            			<a class="nav-link" href="signup.php">Register here</a>
          			</li>
            </form>
      		</div>
    	</nav>
    <!--*******************************************SLIDE SHOW**************************************************************************************-->
    	<section id="carousel">
    		<div class="bd-example">
      			<div id="carousel-home" class="carousel slide" data-ride="carousel">
        			<ol class="carousel-indicators">
          				<li data-target="#carousel-home" data-slide-to="0" class="active"></li>
       					<li data-target="#carousel-home" data-slide-to="1"></li>
          				<li data-target="#carousel-home" data-slide-to="2"></li>
          				<li data-target="#carousel-home" data-slide-to="3"></li>
          				<li data-target="#carousel-home" data-slide-to="4"></li>
          				<li data-target="#carousel-home" data-slide-to="5"></li>
        			</ol>
        			<div class="carousel-inner">
          				<div class="carousel-item active">
            				<img src="photos/DAGS1.jpg" alt="YOGI for man, 2018" height="934px" width="936px">  <!--SLIKA MODELA-->
            				<div class="carousel-caption">
              					<h5>YOGI Collection, black T-Shirt</h5>
              					<p>Domagoj Malic as a model in our black YOGI Male T-Shirt.</p>
            				</div>
          				</div>
          				<div class="carousel-item">
            				<img src="photos/DAGS2.jpg" alt="..." height="934px" width="936px">		<!--SLIKA MODELA-->
            				<div class="carousel-caption">
              					<h5>YOGI Collection, yellow T-Shirt</h5>
              					<p>Mia Dumbovic as a model in our yellow YOGI Female T-Shirt</p>
            				</div>
          				</div>
          				<div class="carousel-item">
            				<img src="photos/DAGS3.jpg" alt="..." height="934px" width="936px">		<!--SLIKA MODELA-->
           					<div class="carousel-caption">
              					<h5>YOGI Collection, white T-Shirt</h5>
              					<p>Mia Dumbovic as a model in our white YOGI Female T-Shirt</p>
            				</div>
          				</div>
          				<div class="carousel-item">
            				<img src="photos/DAGS4.jpg" alt="..." height="934px" width="936px">		<!--SLIKA MODELA-->
            				<div class="carousel-caption">
              					<h5>YOGI Collection, black T-Shirt</h5>
              					<p>Mia Dumbovic as a model in our black YOGI Female T-Shirt</p>
            				</div>
          				</div>
          				<div class="carousel-item">
            				<img src="photos/DAGS5.jpg" alt="..." height="934px" width="936px">		<!--SLIKA MODELA-->
           					<div class="carousel-caption d-none d-md-block">
              					<h5>YOGI Collection, yellow T-Shirt</h5>
              					<p>Juraj Gobac as a model in our yellow YOGI Male T-Shirt</p>
            				</div>
          				</div>
          				<div class="carousel-item">
            				<img src="photos/DAGS6.jpg" alt="..." height="934px" width="936px">		<!--SLIKA MODELA-->
           					<div class="carousel-caption d-none d-md-block">
              					<h5>YOGI Collection, white T-Shirt</h5>
              					<p>Domagoj Malic as a model in our white YOGI Male T-Shirt</p>
            				</div>
          				</div>
        			</div>
        			<a class="carousel-control-prev" href="#carousel-home" role="button" data-slide="prev">
          				<span class="carousel-control-prev-icon" aria-hidden="true"></span>
          				<span class="sr-only">Previous</span>
        			</a>
        			<a class="carousel-control-next" href="#carousel-home" role="button" data-slide="next">
          				<span class="carousel-control-next-icon" aria-hidden="true"></span>
          				<span class="sr-only">Next</span>
        			</a>
      			</div>
    		</div>
    	</section>	
    <!--************************************ABOUT SECTION************************************************************************************************-->
    	<section id="about">
    		<div class="section-content">
    			<div class="container">
    				<div class="col-md-6">
    					<div class="about-text">
    						<h3>About us</h3>
    						<p class="lead">Located in Sisak, Croatia, we're three friends with an idea about making new clothing brand for young folks like ourselves. From high quality material, reasonable prices and funky style, we are determined to change the bussines! Feel free  to contact us for more information.</p>
    						<p>Juraj Gobac as Commercial Executor, <br>Domagoj Malic as Financial Value-Driver and <br>Dino Kekić as Corporate Ambassador</p>
    						<h5>Follow us on the web!</h5> <!--LINKAJ OVO-->
    						<a href="https://www.facebook.com/" class="btn btn-sm btn-secondary-outline">Facebook</a>
    						<a href="https://www.instagram.com/mfdags/" class="btn btn-sm btn-secondary-outline">Instagram</a>
    						<a href="https://twitter.com/?lang=en" class="btn btn-sm btn-secondary-outline">Twitter</a>
    					</div>
    				</div>
    			</div>
    		</div>		
    	</section>
    <!--**************************************SHOPPING******************************************************************************************************-->
    	<section class="container content-section" id="yogi-shop">
                <h2 class="section-header">YOGI Collection</h2>
                <div class="shop-items">
                    <div class="shop-item">
                        <span class="shop-item-title" name="item">YOGI Male</span>
                        <img class="shop-item-image" src="photos/slik1.png">
                        <div class="shop-item-details">
                            <span class="shop-item-price" name="price">$23.99</span>
                            <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
                        </div>
                    </div>
                    <div class="shop-item">
                        <span class="shop-item-title" name="item">YOGI Female</span>
                        <img class="shop-item-image" src="photos/slik2.png">
                        <div class="shop-item-details">
                            <span class="shop-item-price" name="price">$23.99</span>
                            <button class="btn btn-primary shop-item-button"type="button">ADD TO CART</button>
                        </div>
                    </div>
                    <div class="shop-item">
                        <span class="shop-item-title" name="item">YOGI Dogs</span>
                        <img class="shop-item-image" src="photos/doggy.png">
                        <div class="shop-item-details">
                            <span class="shop-item-price" name="price">$19.99</span>
                            <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
                        </div>
                    </div>
                </div>
            </section>
            <section class="container content-section" id="badger-shop">
                <h2 class="section-header">BADGER Collection</h2>
                <div class="shop-items">
                    <div class="shop-item">
                        <span class="shop-item-title" name="item">BADGER Male</span>
                        <img class="shop-item-image" src="photos/majica8.jpg">
                        <div class="shop-item-details">
                            <span class="shop-item-price" name="price">$39.99</span>
                            <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
                        </div>
                    </div>
                    <div class="shop-item">
                        <span class="shop-item-title" name="item">BADGER Female</span>
                        <img class="shop-item-image" src="photos/majica7.jpg">
                        <div class="shop-item-details">
                            <span class="shop-item-price" name="price">$34.99</span>
                            <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
                        </div>
                    </div>
                    <div class="shop-item">
                        <span class="shop-item-title" name="item">BADGER UniSex</span>
                        <img class="shop-item-image" src="photos/majica5.jpg">
                        <div class="shop-item-details">
                            <span class="shop-item-price" name="price">$44.99</span>
                            <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
                        </div>
                    </div>
                </div>
            </section>
            <section class="container content-section" id="owl-shop">
                <h2 class="section-header">OWL Collection</h2>
                <div class="shop-items">
                    <div class="shop-item">
                        <span class="shop-item-title" name="item">OWL Male</span>
                        <img class="shop-item-image" src="photos/owl6.jpg">
                        <div class="shop-item-details">
                            <span class="shop-item-price" name="price">$29.99</span>
                            <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
                        </div>
                    </div>
                    <div class="shop-item">
                        <span class="shop-item-title" name="item">OWL Female</span>
                        <img class="shop-item-image" src="photos/owl5.jpg">
                        <div class="shop-item-details">
                            <span class="shop-item-price" name="price">$34.99</span>
                            <button class="btn btn-primary shop-item-button"type="button">ADD TO CART</button>
                        </div>
                    </div>
                    <div class="shop-item">
                        <span class="shop-item-title" name="item">OWL UniSex</span>
                        <img class="shop-item-image" src="photos/owl2.jpg">
                        <div class="shop-item-details">
                            <span class="shop-item-price" name="price">$29.99</span>
                            <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
                        </div>
                    </div>
                </div>
            </section>
    <!--**************************************CART*****************************************************************************************************-->
    	<section class="container content-section" id="kosarica">
                <h2 class="section-header">CART</h2>
                <h3>Before ordering anything, make sure that you are logged in</h3>
                <div class="cart-row">
                    <span class="cart-item cart-header cart-column">ITEM</span>
                    <span class="cart-price cart-header cart-column">PRICE</span>
                    <span class="cart-quantity cart-header cart-column">QUANTITY</span>
                </div>
                <div class="cart-items">
                	
                </div>
    
                <div class="cart-total">
                    <strong class="cart-total-title">Total</strong>
                    <span class="cart-total-price"  name="total_price">$0</span>
                </div>
                <button class="btn btn-primary btn-purchase" name="purchase_btn" type="button">PURCHASE</button>
            </section>
    
    <!--********************************************PRE-FOOTER*********************************************************************************************-->
    	<section id="company">
            <div class="container">
              <div class="row">
                <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
                  <h4>Contact Us</h4>
                    <ul>
                      <li><i class="fa fa-phone"></i> (+385)981828557</li>
                      <li><i class="fa fa-envelope"></i>dagsbussines@gmail.com</li>
                      <li><i class="fa fa-map"></i> Ulica Stjepana Rozankovica 4, 44000 Sisak</li>
                    </ul>
                </div>
                <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
                  <h4>About Us</h4>
                  <ul>
                      <li>Fashion label company.</li>
                      <li>High quality material.</li>
                      <li>Changing the business.</li>
                    </ul>
                </div>
                <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
                  <h4>Newsletter</h4>
                  <p>Subscribe to our Newsletter</p>
                  <form class="" action="" method="post">
                    <input type="text" name="email" value="" placeholder="Enter Email">
                    <button type="submit" name="button">Submit</button>
                  </form>
                </div>
              </div>
            </div>
        </section>
    <!--*******************************************FOOTER*******************************************************************************************************-->
        <!--Footer-->
        <footer id="mainfooter">
          <div class="container">
            <div class="row center-xs center-sm center-md center-lg">
              <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <p>Copyright &copy; 2019 | MFDags - Bart</p>
              </div>
            </div>
          </div>
        </footer>
    <!--*******************************************DISCOUNT*****************************************************************************************************-->
        <div class="bg-modal">
        	<div class="modal-content">
        		<div class="close">+</div>
        		<img src="photos/bravocado.jpg" height="150px" width="150px">
        		<h4>Congratulations!</h4>
        		<p>
    				Take advantage of 20% discount on your order<br>
    				Use code<br>
    				<b>MFDAGS</b>
    			</p>
        	</div>
        </div>
    
    
    								<!-- jQuery first, then Bootstrap JS. -->
    	<script src="js/bootstrap.min.css"></script>
    	<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    	<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    	<script>
    		document.getElementById('btnshop').addEventListener('click',
    		function() {
    		document.querySelector('.bg-modal').style.display = 'flex';
    		});
    
    		document.querySelector('.close').addEventListener('click',
    		function(){
    		document.querySelector('.bg-modal').style.display = 'none';
    		});
    	</script>
    </body>
    </html>
    store.js

    Code:
    if(document.readyState == 'loading') {
    	document.addEventListener('DOMContentLoaded', ready)
    } else {
    	ready()
    }
    
    function ready() {
    	var removeCartItemButtons = document.getElementsByClassName('btn-danger')
    	for (var i = 0; i < removeCartItemButtons.length; i++){
    		var button = removeCartItemButtons[i]
    		button.addEventListener('click', removeCartItem)
    		}
    
    	var quantityInputs = document.getElementsByClassName('cart-quantity-input')
    		for (var i = 0; i < quantityInputs.length; i++) {
    			var input = quantityInputs[i]
    			input.addEventListener('change', quantityChanged)
    		}
    
    	var addToCartButtons = document.getElementsByClassName('shop-item-button')
    		for (var i = 0; i < addToCartButtons.length; i++) {
    			var button = addToCartButtons[i]
    			button.addEventListener('click', addToCartClicked)
    		}
    	document.getElementsByClassName('btn-purchase')[0].addEventListener('click', purchaseClicked)
    }
    
    function purchaseClicked(){
    	alert('Thank you for your purchase')
    	var cartItems = document.getElementsByClassName('cart-items')[0]
    	while (cartItems.hasChildNodes()){
    		cartItems.removeChild(cartItems.firstChild)
    	}
    	updateCartTotal()
    }
    
    function removeCartItem(event) {
    	var buttonClicked = event.target
    		buttonClicked.parentElement.parentElement.remove()
    		updateCartTotal()
    }
    
    function quantityChanged(event) {
    	var input = event.target
    	if (isNaN(input.value) || input.value <= 0) {
    		input.value = 1
    	}
    	updateCartTotal()
    }
    
    function addToCartClicked(event) {
    	var button = event.target
    	var shopItem = button.parentElement.parentElement
    	var title = shopItem.getElementsByClassName('shop-item-title')[0].innerText
    	var price = shopItem.getElementsByClassName('shop-item-price')[0].innerText
    	var imageSrc = shopItem.getElementsByClassName('shop-item-image')[0].src
    	addItemToCart(title, price, imageSrc)
    	updateCartTotal()
    }
    
    function addItemToCart(title,price,imageSrc) {
    	var cartRow = document.createElement('div')
    	cartRow.classList.add('cart-row')
    	var cartItems = document.getElementsByClassName('cart-items')[0]
    	var cartItemNames = cartItems.getElementsByClassName('cart-item-title')
    	for (var i = 0; i < cartItemNames.length; i++){
    		if (cartItemNames[i].innerText == title) {
    			alert('This item is already added to the cart!')
    			return
    		}
    	}
    	var cartRowContents = `
    	<div class="cart-item cart-column">
            <img class="cart-item-image" src=${imageSrc} width="100" height="100">
       		<span class="cart-item-title">${title}</span>
        </div>
        <span class="cart-price cart-column">${price}</span>
        <div class="cart-quantity cart-column">
          	<input class="cart-quantity-input" type="number" value="1">
          	<button class="btn btn-danger" type="button">REMOVE</button>
    	</div>`
    	cartRow.innerHTML = cartRowContents
    	cartItems.append(cartRow)
    	cartRow.getElementsByClassName('btn-danger')[0].addEventListener('click',removeCartItem)
    	cartRow.getElementsByClassName('cart-quantity-input')[0].addEventListener('change',quantityChanged)
    }
    
    
    function updateCartTotal() {
    	var cartItemContainer = document.getElementsByClassName('cart-items')[0]
    	var cartRows = cartItemContainer.getElementsByClassName('cart-row')
    	var total = 0
    	for (var i = 0; i < cartRows.length; i++){
    		var cartRow = cartRows[i]
    		var priceElement = cartRow.getElementsByClassName('cart-price')[0]
    		var quantityElement = cartRow.getElementsByClassName('cart-quantity-input')[0]
    		var price = parseFloat(priceElement.innerText.replace('$', ''))
    		var quantity = quantityElement.value
    		total = total + (price*quantity)
    	}
    	total = Math.round(total * 100) / 100
    	document.getElementsByClassName('cart-total-price')[0].innerText = '$' + total
    }
    signup.php

    Code:
    <?php
    	require_once('connect.php');
    
    if (isset($_POST['register_btn'])) {
    
    	$username2 = mysqli_real_escape_string($conn, $_POST['username']);
    	$email = mysqli_real_escape_string($conn, $_POST['email']);
    	$password = mysqli_real_escape_string($conn, $_POST['password']);
    	$password2 = mysqli_real_escape_string($conn, $_POST['password2']);
    	$address = mysqli_real_escape_string($conn, $_POST['address']);
    	$city = mysqli_real_escape_string($conn, $_POST['city']);
    	$country = mysqli_real_escape_string($conn, $_POST['country']);
    
    	if ($password == $password2) {
    	$password = md5($password);
    	$sql = "INSERT INTO users(username, email, password, address, city, country) VALUES('$username2', '$email', '$password', '$address', '$city', '$country')";
    	$result = mysqli_query($conn, $sql);
    	header("Location: login_form.php");
    	}
    	else{
    		echo "Error: The two passwords don't match!";
    	}
    	}
    ?>
    
    <html>
    <head>
    <title>Sign up Form</title>
    	<link rel="stylesheet" type="text/css" href="login.css">
    <body>
    	<div class="loginbox">
    		<img src="photos/doggy.png" class="avatar">
    			<h1>Sign up Here</h1>
    			<?php
        			if (isset($_SESSION['message'])) {
        				echo "<div id='error_msg'".$_SESSION['message']."</div>";
        				unset($_SESSION['message']);
        			}
        		?>
    			<form method="post" action="signup.php">
    				<p>Enter your email</p>
    				<input type="text" name="email" placeholder="Enter Email">
    				<p>Choose your username</p>
    				<input type="text" name="username" placeholder="Enter Username">
    				<p>Password</p>
    				<input type="password" name="password" placeholder="Enter Password">
    				<p>Enter your password again</p>
    				<input type="password" name="password2" placeholder="Re-enter Password">
    				<p>Enter your address</p>
    				<input type="text" name="address" placeholder="Enter Address">
    				<p>Enter your city</p>
    				<input type="text" name="city" placeholder="Enter City">
    				<p>Enter your country of residence</p>
    				<input type="text" name="country" placeholder="Enter country">
    				<input type="submit" name="register_btn" value="Register"><br>
    				<a href="login_form.php">Already have an account?</a>
    			</form>
    	</div>
    </body>
    </head>
    </html>
    login_form.php

    Code:
    <?php
    	require_once('connect.php');
    
    if (isset($_POST['login_btn'])) {
    
    	$username2 = mysqli_real_escape_string($conn, $_POST['username']);
    	$password = mysqli_real_escape_string($conn, $_POST['password']);
    
    	$password = md5($password);
    	$sql = "SELECT * FROM users WHERE username='$username2' AND password='$password'";
    	$result = mysqli_query($conn, $sql);
    
    	if(mysqli_num_rows($result) > 0)
    	{
    		while($row = mysqli_fetch_assoc($result))
    		{
    			$id = $row["id"];
    			$username2 = $row["username"];
    			session_start();
    			$_SESSION['id'] = $id;
    			$_SESSION['username'] = $username2;
    			$_SESSION['email'] = $email;
    			$_SESSION['address'] = $address;
    			$_SESSION['city'] = $city;
    			$_SESSION['country'] = $country;
    		}
    		header("Location: index.php");
    	}
    	else
    	{
    		echo "Invalid username or password";
    	}
    }
    ?>
    
    
    
    <html>
    <head>
    <title>Login Form</title>
    	<link rel="stylesheet" type="text/css" href="login.css">
    <body>
    	<div class="loginbox">
    		<img src="photos/doggy.png" class="avatar">
    			<h1>Login Here</h1>
    	 		<?php
        			if (isset($_SESSION['message'])) {
        				echo "<div id='error_msg'".$_SESSION['message']."</div>";
        				unset($_SESSION['message']);
        			}
        		?>
    			<form method="post" action="login_form.php">
    				<p>Username</p>
    				<input type="text" name="username" placeholder="Enter Username">
    				<p>Password</p>
    				<input type="password" name="password" placeholder="Enter Password">
    				<input type="submit" name="login_btn" value="Login"><br>
    				<a href="signup.php">Still don't have an account?</a>
    			</form>
    	</div>
    </body>
    </head>
    </html>
    connect.php and logout.php not needed i believe

    Comment

    Working...