Jump to content
#FlattenTheCurve ×

Revolutsio

Members
  • Content Count

    31
  • Joined

  • Last visited

Posts posted by Revolutsio


  1. Yes that is what I want 

    13 minutes ago, Barand said:

    It's much easier with CSS and divs than it is with a table.

    
    <?php
    $output = '';
    for ($i=1; $i<=28; $i++) {
        $output .= "<div class='db-entry'>Entry $i</div>\n";
    }
    ?>
    <!DOCTYPE html> 
    <html>
    <head>
    <meta http-equiv="content-language" content="en">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Example</title>
    </head>
    <style type="text/css">
        .db-entry {
            width: 18%;
            height: 50px;
            padding: 20px 2px;
            border: 1px solid gray;
            margin: 0 2px 2px 0;
            float: left;
        }
    </style>
    <body>
        <?=$output?>
    </body>
    </html>

    image.png.62172faeeb51fef8b8cbc3ee00350db4.png

    This is the way i would like to do it how would i put in the select query into the code?

    • Like 1

  2. <table>
        <thead>
            <tr>
                
                <th>Game</th>
                <th>Date Finished</th>
            </tr>
        </thead>
    <?php
     $sql= "SELECT * 
     FROM
            games
    WHERE 
    		completed=1 
    	ORDER BY 
    		Game_Name;";
    
    $result = mysqli_query($conn, $sql) or die("Bad Query: $sql");
    $num_rows = mysqli_num_rows($result);?>
    <?php
    $counter = 1;
    while ($row = mysqli_fetch_assoc($result)):
    ?>
        <tr>
    <!--    <td style="text-align:center"><?php echo $row['ID']; ?></td> -->
        <td style="text-align:center"><?php echo $row['Game_Name']; ?></td>
    <!--    <td style="text-align:center"><?php echo date ('d F Y', strtotime($row['Date_Finished'])); ?></td> -->
        <?php $counter++;?>
        </tr>  
        <?php endwhile; ?>
    </body>
    </table>
    </html>     

     


  3. I want my mysql database to be displayed across the screen similar to the below

    Entry 1        Entry 2             Entry 3         Entry 4      Entry 5

    eventually with a image above the name, but for now just as above.

     

    How would I go about this?

     


  4. 35 minutes ago, Barand said:

    If you define your array like this, to begin the indexing at 1 instead of 0,

    
    $names = array (1 => "Stan", "John", "Dean", "Sam", "Lee");

    then your indexes will align with your $num values, 1 - 5; In other words

    
    $num = 5;
    echo $names[$num];              //--> Lee

     

    Once again @Barand you come to me rescue thanks again


  5. I have five names in an array and also numbers 5,2,1,3 and what i want to do is php to look at the numbers and print out the name that corresponds to the number.

    so for my example the name it should echo out is 'Lee'

     

     $names = array ("Stan", "John", "Dean", "Sam", "Lee");

    here are the five names.

    The idea is to learn php with making a little fixture list with these five names.

    The problem i have is to make php read the number and then look up the array for the name that corresponds to the number.

    I hope this explains what I would like to do?

     


  6. On 1/12/2020 at 6:08 PM, NotSunfighter said:

    Revolutsio,
      I see where you have used "this is not my code I found this on youtube" on your questions before. Most YT teaching vids never post code - it has to be copied. If this is how your doing it, please verify your randision before asking for help. Chances are pretty high that the correct code, as was given on youtube, works.

    The code did work my question was how do I put the results in a table


  7. On 1/12/2020 at 4:39 PM, Barand said:

    try

    
    <?php
    require 'db_inc.php';
    $db = pdoConnect();            // function defined in db_inc.php
    
    $search = $_GET['search'] ?? '';
    $tdata = '';
    
    $stmt = $db->prepare("SELECT ID
                               , GAME
                               , PLATFORM
                               , Owned
                          FROM games
                          WHERE GAME LIKE ?     
                         ");
    $stmt->execute([ "$search%" ]);
    foreach ($stmt as $row) {
        $tdata .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n";
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <title>Sample Search</title>
    <style type='text/css'>
        body  { font-family: calibri, sans-serif; font-size: 11pt;}
        header{ background-color: black; color: white; padding: 16px; }
        form  { padding: 16px; }
        table { width: 75%; margin: 30px auto; border-collapse: collapse; }
        th    { background-color: black; color: white; padding: 8px; }
        td    { padding: 4px 8px;}
    </style>
    </head>
    <body>
    <header>
        <h1>Sample</h1>
    </header>
    <form method='GET'>
        Search for: 
        <input type="text" name="search" value="<?=$search?>" size="40">
        <input type="submit" name="btnSub" value="Search"> 
    </form>
    <div>
    <?php if ($search) { ?>
       <table border='1'>
           <thead>
               <tr>
                  <th>ID</th>
                  <th>GAME</th>
                  <th>PLATFORM</th>
                  <th>OWNED</th>
                </tr>
           </thead>
           <tbody>
               <?=$tdata?>
           </tbody>
       </table>
    <?php } ?>
    </div>
    </body>
    </html>

     

    Sorry for the late reply I have tried the code and I get an error in line 3

     

    Call to undefined function pdoConnect()

    Here is my loggin code called 'dbh.php'

     

    <?php
    
    $dbServername = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbName = "csv_db";
    
    $conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
    ?>

     


  8. I have copied this from a youtube video, I have a good grasp of html. I want a search my database and be able to edit the details on my webpage so I went looking for help and found this code that did search my database and print the results to the screen and this worked. but when I tried to add my css file and other bits from my site i get errors. 


  9. I have now got a search engine to work, but it only show in normal text, could someone help me get into a table, I have a CSS file but cannot get the search page to print in my table.

     

    <div id="main">
    <table>
        <thead>
            <tr>
                <th>ID</td>
                <th>GAME</td>
                <th>PLATFORM</td>
    
            </tr>
        <thead>        
    
    <?php
    $output = NULL;
    if(isset($_POST['submit'])) {
        //Connect to database
        $mysqli = NEW MySQLi ("localhost","root","","***.**");
    
        $search = $mysqli->real_escape_string($_POST{'search'});
    
        //Query the database
        $resultSet = $mysqli->query("SELECT * FROM games WHERE GAME LIKE '$search%'");
    
        if($resultSet->num_rows >0){
                while($rows = $resultSet->fetch_assoc())
                {
                   $id = $rows['ID'];
                   $gn = $rows['GAME'];
                   $pf = $rows['PLATFORM'];
                   $od = $rows['Owned'];?>
                }   
                   
                    <tr>
                        <td><?php $output .= $id; ?></tr>;
                   
                   <!-- Game: $gn<br />
                   Platform: $pf<br />
                   Owned: $od<br /><br />";-->
                   
    
    <form method="POST">
    <input type="TEXT" name="search" />
    <input type="SUBMIT" name="submit" value="Search" />
    </form>
    </table>
    </body>
    </html>

    Also how can i use my connect file instead of having the connect to database in this file?


  10. 44 minutes ago, mac_gyver said:

    the most immediate problem is you are not using the correct $_POST field and table column in the WHERE clause in the SELECT query (edit: i realized while writing this that the SELECT query you have shown is part of the update process, to retrieve a specific row of data to populate the form fields with, not part of a name search.) if you are entering the name (or partial name) of a game to search for, wouldn't you be searching the table column holding the game's name? why are you trying to match the id column?

    next, don't write code like this. there's a bunch of problems that have resulted in a wall of code that's both insecure and has created a 'cannot see the forest for the trees' problem (which is perhaps why you are using the wrong field/column in the WHERE clause.)

    a laundry list of issues -

    1. don't create a bunch of discrete variables for each different form you write code for. this is just a waste of typing time. instead, operated on the form data as a set, by keeping the data as an array, and operating on the elements in the array.
    2. by using exceptions for database statement errors, any connection, query, prepare, and execute error will transfer control to the exception handler. therefore, any discrete error handling logic in your code won't ever be executed and should be removed.
    3. related to #2, the only exception handling try/catch block you should have in your code are for those errors that are recoverable, that your code can do something about, such as dealing with the inserting/updating of duplicate or out of range user submitted data. all other database statement errors are non-recoverable and there's no good reason for your code to catch these exceptions. just let php catch and handle these exceptions.
    4. by using exceptions for errors and letting php catch them, php will use its error related settings to control what happens with the actual error information (database statement errors will 'automatically' get displayed or logged the same as php errors.)
    5. 'function getPosts()' - don't do this. you want code to be easy to write and debug and be readable by everyone. by intentionally using numerical indexes, you have made more work while writing this code and made more work for anyone trying to read/maintain the code. also, once you write and test the code for a function, you should not find yourself regularly editing the code.  functions should not contain application specific code that you must change each time you do something new.
    6. external data can be anything and cannot be trusted. you must validate all external data before using it.
    7. you should use a prepared query when supplying external/unknown data to the sql query statement.
    8. items #6 and #7 will help avoid the current error, because you would not attempt to run a query if an expected input is empty and a numerical input that can be empty/null won't produce a query error.
    9. a name or partial name search can match more than one row (the same game name for more than one platform.) you would loop over the result from such a query and display as many rows of data that the query matched. to update/delete the data for a specific row, you would produce an edit link and a delete form for each row that is displayed. the edit link would contain the id of the row. when you click the edit link, the code would query for an fetch the data matching that row and populate the form field values. the current SELECT query seems to be for this part of the process, not for a name search. you need one more SELECT query.

     

    Thank you for your reply this is not my code I found this on youtube I put this as an example of what i tried to do to get a search field on my screen


  11. Could somebody help me with a little problem I am having, I havew spent the last two weeks trying different ways to search my database without success.

    What I want to do is have an input field that I can search only one column of my database. (it is a game collection) for example I want to search the game 'Medal of Honor' i type this in the search field and click submit button and it should find all the entries with that name in the database.

    I have tried a few online tutorials and nothing works.

     

    here is some code that i tried.

    <?php
    
    $host = "localhost";
    $user = "root";
    $password ="";
    $database = "csv_db";
    
    $id = "";
    $game = "";
    $year = "";
    $platform = "";
    
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    
    // connect to mysql database
    try{
        $connect = mysqli_connect($host, $user, $password, $database);
    } catch (mysqli_sql_exception $ex) {
        echo 'Error';
    }
    
    // get values from the form
    function getPosts()
    {
        $posts = array();
        $posts[0] = $_POST['id'];
        $posts[1] = $_POST['game'];
        $posts[2] = $_POST['year'];
        $posts[3] = $_POST['platform'];
        return $posts;
    }
    
    // Search
    
    if(isset($_POST['search']))
    {
        $data = getPosts();
        
        $search_Query = "SELECT * FROM games WHERE id = $data[0]";
        
        $search_Result = mysqli_query($connect, $search_Query);
        
        if($search_Result)
        {
            if(mysqli_num_rows($search_Result))
            {
                while($row = mysqli_fetch_array($search_Result))
                {
                    $id = $row['ID'];
                    $game = $row['GAME'];
                    $year = $row['YR'];
                    $platform = $row['PLATFORM'];
                }
            }else{
                echo 'No Data For This Id';
            }
        }else{
            echo 'Result Error';
        }
    }
    
    
    // Insert
    if(isset($_POST['insert']))
    {
        $data = getPosts();
        $insert_Query = "INSERT INTO `games`(`Game`, `YR`, `PLATFORM`) VALUES ('$data[1]','$data[2]',$data[3])";
        try{
            $insert_Result = mysqli_query($connect, $insert_Query);
            
            if($insert_Result)
            {
                if(mysqli_affected_rows($connect) > 0)
                {
                    echo 'Data Inserted';
                }else{
                    echo 'Data Not Inserted';
                }
            }
        } catch (Exception $ex) {
            echo 'Error Insert '.$ex->getMessage();
        }
    }
    
    // Delete
    if(isset($_POST['delete']))
    {
        $data = getPosts();
        $delete_Query = "DELETE FROM `games` WHERE `ID` = $data[0]";
        try{
            $delete_Result = mysqli_query($connect, $delete_Query);
            
            if($delete_Result)
            {
                if(mysqli_affected_rows($connect) > 0)
                {
                    echo 'Data Deleted';
                }else{
                    echo 'Data Not Deleted';
                }
            }
        } catch (Exception $ex) {
            echo 'Error Delete '.$ex->getMessage();
        }
    }
    
    // Edit
    if(isset($_POST['update']))
    {
        $data = getPosts();
        $update_Query = "UPDATE `games` SET `GAMES`='$data[1]',`YR`='$data[2]',`PLATFORM`=$data[3] WHERE `ID` = $data[0]";
        try{
            $update_Result = mysqli_query($connect, $update_Query);
            
            if($update_Result)
            {
                if(mysqli_affected_rows($connect) > 0)
                {
                    echo 'Data Updated';
                }else{
                    echo 'Data Not Updated';
                }
            }
        } catch (Exception $ex) {
            echo 'Error Update '.$ex->getMessage();
        }
    }
    
    
    
    ?>
    
    
    <!DOCTYPE Html>
    <html>
        <head>
            <title>PHP INSERT UPDATE DELETE SEARCH</title>
        </head>
        <body>
            <form action="php_insert_update_delete_search.php" method="post">
                <input type="number" name="id" placeholder="ID" value="<?php echo $id;?>"><br><br>
                <input type="text" name="game" placeholder="Game" value="<?php echo $game;?>"><br><br>
                <input type="text" name="year" placeholder="Year" value="<?php echo $year;?>"><br><br>
                <input type="number" name="platform" placeholder="Platform" value="<?php echo $platform;?>"><br><br>
                <div>
                    <!-- Input For Add Values To Database-->
                    <input type="submit" name="insert" value="Add">
                    
                    <!-- Input For Edit Values -->
                    <input type="submit" name="update" value="Update">
                    
                    <!-- Input For Clear Values -->
                    <input type="submit" name="delete" value="Delete">
                    
                    <!-- Input For Find Values With The given ID -->
                    <input type="submit" name="search" value="Find">
                </div>
            </form>
        </body>
    </html>

    and here is the error i get.

    Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 in H:\xampp\htdocs\php_insert_update_delete_search.php:41 Stack trace: #0 H:\xampp\htdocs\php_insert_update_delete_search.php(41): mysqli_query(Object(mysqli), 'SELECT * FROM g...') #1 {main} thrown in H:\xampp\htdocs\php_insert_update_delete_search.php on line 41

    can anybody help me do I search engine for my database


  12. 8 minutes ago, Barand said:

    What does you FINISHED column contain if there is no finished date yet? (I am assuming a blank string)

    Can you provide some sample data? (Dump of your 'games' table)

    what file extension would you like?


  13. 	$date = $row['FINISHED'];
    	IF($date == '0000-00-00') {
    	$date='';
    	} else {
    		$parts = explode('-',$date);
    		$date = date('d-m-Y', mktime(0,0,0,$parts[1],$parts[2],$parts[0]));
    	}

    I have tried this code and i get 30 November -0001


  14. 11 hours ago, Barand said:

    As for that '✔', store in your table as 1/0 for completed/not completed.

    You convert to a '✔' on output, not on input.

    Thank you i will try this.

     

    Now with the date query, when i show the page with items that have not been completed i get the date '0000-00-00' how do i change this , i searched on here and found the same problem but it did not work with my code.

    How do i make this date blank if  'FINISHED' = '0000-00-00'

     

    if (FINISHED='0000-00-00', '', FINISHED) as FINISHED

    i have tried this but get '01 January 1970' and if I put a space in the second quotes I get today's date , a little bit confused

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.