CRUD - JWalshe86/nags-with-notions4 GitHub Wiki

Common Commands

To run the xampp gui

sudo /opt/lampp/manager-linux-x64.run

Adding Crud Functionality

Going to use FidelTech's youtube video

Created a crud branch Created signUp.php, login.php & connection.php in root folder

Several tutorials go to xampp image

Installing xampp

index.php image

Using How to install xampp on Ubuntu

Installed net-tools

Get this error image

🎉 Removed all docker containers and Apache Server now runs

image

image

image

sudo ln -s ~/projects/nags-with-notions4 /opt/lampp/htdocs

This worked xampp-access-forbidden-php but getting page not found now image

Symlink to projects present but getting 403 permission errors image

This isn't working: image

Trying to restart apache

image

image

I created a 'tests' folder in htdocs and this worked, so it's the symlink image

🎉 image

sudo chown johnwalshe:johnwalshe /opt/lampp/htdocs

Changing the owner of the projects dir in the htdocs folder to 'johnwalshe' and in the 'etc/httpd.conf' file changing the User from daemon to johnwalshe appeared to fix the issue

image

image

This is where I got the httpd.conf solution

403 access error apache

image

To run the xampp gui

sudo /opt/lampp/manager-linux-x64.run

if apache doesn't run try turning down all docker containers image

🎉 image

signUP.php

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Sign Up</title>
</head>
<body>
	<div class="form">
	<form action="" method='post'>
            <input type='text' name="name" placeholder='Name'>
            <input type='text' name="lastName" placeholder='Last Name'>
            <input type='text' name="email" placeholder='Email'>
            <input type='text' name="password" placeholder='Password'>
            <input type='text' name="confiPassword" placeholder='Confirm Password'>
        </form>
        </div>
</body>
</html>

image

SignUP image

Connecting to mysql

http://localhost/phpMyAdmin

image

sudo chown -R root:root temp

Changed permissions to temp dir and get the following: image

From this reset mysql password article I changed the password

/opt/lampp/bin/mysqladmin --user=root password 'new password'

Get this error now image

image

Went to config.inc.php and entered password: image

🎉 image

Created CRUDsystem database image image image

PHP to connect to mysql

<?php

    class crud{
/* The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. You don't need to instantiate the class with 'new'. Use when inheritance isn't required.*/
        public static function connect()
        {
           try {
 /* The new keyword is used to create an object from a class. PDO (PHP Data Objects) is a built in PHP class used to make it easier
to interact with a database. With PDO you can issue queries or fetch data using the same functions on different databases
 */
		   $con=new PDO('mysql:localhost=host; dbname=ol5wws00_crudsystem2','root','Sunshine7!');
		   return $con;
/*
The PHP PDOException is a runtime exception that occurs when something goes wrong while using the PDO (PHP Data Objects) class or its related extensions.  */
           } catch (PDOException $error1) 
                echo 'Something went wrong, with you connection!'.$error1->getMessage();
           }catch (Exception $error2){
                 echo 'Generic error!'.$error2->getMessage();
           }
        }
	public static function Selectdata()
	{
/* An array is used so multiple values can be stored here in the one variable */
	    $data=array();
/*
the double colon, is a token that allows access to a constant, static property, or static method of a class or one of its parents.
'prepare' is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency.
*/
            $p=crud::connect()->prepare('SELECT * FROM crudtable');	
	    $p->execute();
	    $data=$p->fetchAll(PDO::FETCH_ASSOC);
	    return $data;
	}
	public static function delete($id)
	{
	    $p=crud::connect()->prepare('DELETE FROM crudtable WHERE id=:id');
	    $p->bindValue(':id',$id);
	    $p->execute();
	
	}
        public static function userDataPerId($id)
{
    $data=array();
    $p=crud::connect()->prepare('SELECT * FROM crudtable WHERE id=:id');
/* bindValue is used as a safety feature for placeholders when using prepare */
    $p->bindValue(':id',$id);
    $p->execute();
   $data=$p->fetch(PDO::FETCH_ASSOC);
   return $data;
}

    }

?>

when use PDO or mysqli_connect?

Test to check connection in signUP.php

 <?php
        require('./connection.php');
        if (isset($_POST['signUP_button'])) {
	   echo 'It is working!';
	}

     ?>

🎉 image

Getting this error: image

Changed root password to default password. Screen shows empty now. image 🎉 Had incorrect table name image Can see entry in phpMyAdmin image

🎉 Logged in successfully image

<?php
        require('./connection.php');
        if (isset($_POST['signUP_button'])) {
            $name=$_POST['name'];
            $lastName=$_POST['lastName'];
            $email=$_POST['email'];
            $password=$_POST['password'];
            $confPassword=$_POST['confiPassword'];
           if (!empty($_POST['name'])&& !empty($_POST['lastName'])&& !empty($_POST['email'])&&!empty($_POST['password'])) {
            if ($password== $confPassword) {
                $p=crud::conect()->prepare('INSERT INTO CRUDTables(name,lastName,email,pass) VALUES(:n,:l,:e,:p)');
                $p->bindValue(':n', $name);
                $p->bindValue(':l', $lastName);
                $p->bindValue(':e', $email);
                $p->bindValue(':p',$password);
                $p->execute();
                echo 'User added successfully!';
            }else{
                echo 'Password does not match!';
            }
           }
        }

    ?> 

🎉 Users display in table image

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./table.css">
    <title>Document</title>
</head>
<body>
   
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Last Name</th>
                <th>Email</th>
                <th>Password</th>
                <th>Delete</th>
                <th>Edit</th>
            </tr>
        </thead>
        <tbody>
        <?php
        require('./connection.php');
            $p=crud::Selectdata();
            if (isset($_GET['id'])) {
                $id=$_GET['id'];
                $e=crud::delete($id);
            }
            if (count( $p)>0) {
                for ($i=0; $i < count( $p); $i++) { 
                   echo '<tr>';
                   foreach ( $p[$i] as $key => $value) {
                    if ($key!='id') {
                        echo '<td>'.$value.'</td>';
                    }
                    }
                    ?>

                    <td><a href="users.php?id=<?php echo $p[$i]['id'] ?>"><img src="./trash.svg" alt="" srcset=""></a></td>
                    <td><a href="upDate.php?id_up=<?php echo $p[$i]['id'] ?>"><img src="./edit.svg" alt="" srcset=""></a></td>
                    <?php
                    echo '</tr>';
                }
            }


    ?>
        </tbody>
    </table>
</body>
</html>

New users being added successfully image

Trash icon by Icons8 🎉 Delete button working image 🎉 updated successfully image

🐛 Updating updates all names image

Data not rendering image

The table name was incorrect in the update function. Update form preloads now & updates name correctly image

🐛 #1932 - Table 'CRUDsystem.CRUDTables' doesn't exist in engine

image

Attempting to delete CRUDsystem db Get this error image

when running mysql_upgrade I get this image

As per this mysql error upgrade I ran

mysqld_upgrade --upgrade=FORCE

It then got this image

Changed the binlog.index permissions & get this: image

This error now: image

There's an ibdata1 in /var/lib/mysql && in /opt/lampp/var/mysql

Get this error now:

image

changed server host from 'localhost' to 127.0.0.1 image

Used remove mysql = i think I've a lampp version & a normal version and it's causing mix ups

Installed new version of phpmyadmin - old tables not there image

🎉 Working again image

Needed to add update password to table & not just database to get it working again

Added image table to crud db image

⚠️ **GitHub.com Fallback** ⚠️