php mysqli

jayampathi

Well-known member
  • May 22, 2006
    2,442
    44
    48
    machan mata podi script ekak hamba una
    mata one data insert karanna

    html form ekain data mysqli walata yana widihata
    mata poddak karana widiha kiyanna

    DB

    CREATE TABLE `banners` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `url` VARCHAR( 150 ) NOT NULL ,
    `banner` VARCHAR( 150 ) NOT NULL ,
    `name` VARCHAR( 60 ) NOT NULL, 'clicks' INT NOT NULL DEFAULT '0'
    ) ;


    conection
    <?php $db_host = "localhost";
    $db_username = "root";
    $db_password = "";
    $db_database = "counter";

    $link = mysqli_connect($db_host,$db_username,$db_password) or die("Cannot connect");
    mysqli_select_db($link, $db_database) or die("Cannot select the database"); ?>

    index
    <?php
    include("config.php");
    $sql = mysqli_query($link, "SELECT id, url, banner, name FROM banner");
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>banners index page</title>
    </head>
    <body>
    <div id="container">
    <?php
    while($result = mysqli_fetch_object($sql)):
    ?>
    <p>
    <a href="counter.php?id=<?php echo $result->id; ?>">
    <img src="<?php echo $result->banner; ?>" alt="<?php echo $result->name; ?>" />
    </a>
    </p>
    <?php
    endwhile;
    ?>
    </div><!-- end of container -->
    </body>
    </html>


    count
    <?php

    //1. include the configuration file
    include("config.php");

    //2. Get the id from the url and store it into a variable
    $id = mysqli_real_escape_string($link, $_GET['id']);

    //3. fetch the url and clicks from this banner
    $clicks = mysqli_fetch_object(mysqli_query($link, "SELECT url, clicks FROM banners WHERE id=".$id.""));

    //4. increase clicks with 1
    $new_click = $clicks->clicks+1;

    //5. update this into the database, check if it was succesfull
    if(mysqli_query($link, "UPDATE banners SET clicks=".$new_click." WHERE id=".$id."")):
    //6. redirect to the url
    header("Location: ".$clicks->url);
    else:
    //6. else write to error log
    endif;
    ?>
     

    KRipTER

    Well-known member
  • Sep 13, 2015
    3,243
    357
    83
    Everywhere...
    <html>
    <form action="add.php" method="post">
    <input id="name" name="name" type="text" placeholder="name">
    <input id="permisions" name="password" type="password" placeholder="password">
    <input id="submit" type="submit" value="Submit">
    </form>
    </html>

    ඔය form එකෙන් data යවන්න php එකක් මෙහෙම ලියන්න පුළුවන්..මේ මචන් mysqli OOP ක්‍රමේ

    <?php

    define('DB_SERVER', 'host:port');
    define('DB_USER', 'username');
    define('DB_PASS', 'password');
    define('DB_NAME', 'database');


    $name=$_POST["name"];
    $password=$_POST["password"];

    $conn = new mysqli(DB_SERVER, DB_USER, DB_PASS,DB_NAME) or die();

    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }

    if ($conn->query("insert into users(name,password) values('$name','$password')") === TRUE) {
    echo "New record created successfully";
    } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();

    ?>
     
    Last edited:

    KRipTER

    Well-known member
  • Sep 13, 2015
    3,243
    357
    83
    Everywhere...
    මෙහෙම data ගන්න පුළුවන්

    <?php

    define('DB_SERVER', 'host:port');
    define('DB_USER', 'username');
    define('DB_PASS', 'password');
    define('DB_NAME', 'database');


    $name=$_POST["name"];
    $password=$_POST["password"];

    $conn = new mysqli(DB_SERVER, DB_USER, DB_PASS,DB_NAME) or die();

    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }

    $conn = new mysqli(DB_SERVER, DB_USER, DB_PASS,DB_NAME) or die();

    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }


    if ($result->num_rows > 0) {
    // output data of each row
    while ($row = $result->fetch_assoc()) {
    $user= $row["name"] ;

    }
    } else {
    echo "0 results";
    }

    $conn->close();

    ?>
     

    Ayeshlive

    Well-known member
  • Jul 1, 2011
    10,570
    943
    113
    *̶͑̾̾​̅ͫ͏̙̤g͛͆̾ͫ̑͆&
    <html>
    <form action="add.php" method="post">
    <input id="name" name="name" type="text" placeholder="name">
    <input id="permisions" name="password" type="password" placeholder="password">
    <input id="submit" type="submit" value="Submit">
    </form>
    </html>

    ඔය form එකෙන් data යවන්න php එකක් මෙහෙම ලියන්න පුළුවන්..මේ මචන් mysqli OOP ක්‍රමේ

    <?php

    define('DB_SERVER', 'host:port');
    define('DB_USER', 'username');
    define('DB_PASS', 'password');
    define('DB_NAME', 'database');


    $name=$_POST["name"];
    $password=$_POST["password"];

    $conn = new mysqli(DB_SERVER, DB_USER, DB_PASS,DB_NAME) or die();

    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }

    if ($conn->query("insert into users(name,password) values('$name','$password')") === TRUE) {
    echo "New record created successfully";
    } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();

    ?>
    Vulnerable to SQL injection.
     

    jayampathi

    Well-known member
  • May 22, 2006
    2,442
    44
    48
    mata one html form code tikak sqli code tika
    man add.php ekata db eka connect kara

    <?php
    include("config.php");

    ithuru tika thama danne neththe
     

    Lanil T

    Junior member
  • Apr 8, 2009
    101
    9
    18
    Kurunegala, Sri Lanka
    Use mysqli Procedural.

    ලොක්කා MySQLi Procedural Method එක ලේසි නැද්ද ඔයිට වැඩිය. W3Schools එකේ වැඩේ පැහදිලි වෙන උදාහරණ ටිකක් තියනවා
     

    KRipTER

    Well-known member
  • Sep 13, 2015
    3,243
    357
    83
    Everywhere...
    ලොක්කා MySQLi Procedural Method එක ලේසි නැද්ද ඔයිට වැඩිය. W3Schools එකේ වැඩේ පැහදිලි වෙන උදාහරණ ටිකක් තියනවා

    OOP ලෙසියි බං..code ටිකයි..

    උබේ database table එකේ coumn ටික දාහන්..