-4

I am looking for answers on Stack Overflow and Google, have read about 50 same questions, every time its different, my head is already boiling. I have a simple PHP CRUD application with OOP.

It has a connection to the database (it works, my data is fetched from the database and I could see it at my index.php in Opera/Safari), has 'add' action (it doesn't work - white page), 'edit' action (it doesn't work - white page), 'delete' action (it works).

UPDATE2:

I have changed cli interpreter to 5.6. Now I see some errors (if I add this line to .ini file 'always_populate_raw_post_data' to '-1' errors are disappear, but I see a white page again):

Deprecated: Automatically populating $HTTP_RAW_POST_DATA is . 
deprecated and will be removed in a future version. To avoid this 
warning set 'always_populate_raw_post_data' to '-1' in php.ini and 
use the php://input stream instead. in Unknown on line 0

Warning: Cannot modify header information - headers already sent in 
Unknown on line 0
Array ( [type] => 2 [message] => Cannot modify header information - 
headers already sent [file] => Unknown [line] => 0 ) Array ( [type] 
=> 2 [message] => Cannot modify header information - headers already 
sent [file] => Unknown [line] => 0 )

I am using in my project:

  • PHPSTORM

  • PHP CLI interpreter 7.2

  • PHP version 5.6

add.php code:

<html>
<head>
<title>Add Data</title>
</head>
<body>
<?php
include_once("classes/Crud.php");
include_once("classes/Validation.php");
$crud = new Crud();
$validation = new Validation();
if(isset($_POST['Submit'])) {   
$name = $crud->escape_string($_POST['name']);
$age = $crud->escape_string($_POST['age']);
$email = $crud->escape_string($_POST['email']); 
$msg = $validation->check_empty($_POST, array('name', 'age', 'email'));
$check_age = $validation->is_age_valid($_POST['age']);
$check_email = $validation->is_email_valid($_POST['email']);
if($msg != null) {
    echo $msg;      
    echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} elseif (!$check_age) {
    echo 'Please provide proper age.';
} elseif (!$check_email) {
    echo 'Please provide proper email.';
}   
else { 
    $result = $crud->execute("INSERT INTO users(name,age,email) VALUES('$name','$age','$email')");
    echo "<p style='color=green'>Data added successfully.";
    echo "<br/><a href='index.php'>View Result</a>";
}
}?>
</body>
</html>

edit.php

<?php
include_once("classes/Crud.php");
$crud = new Crud();
$id = $crud->escape_string($_GET['id']);
$result = $crud->getData("SELECT * FROM users WHERE id=$id");
foreach ($result as $res) {
$name = $res['name'];
$age = $res['age'];
$email = $res['email'];
}
?>
<html>
<head>  
<title>Edit Data</title>
</head>
<body>
<a href="index.php">Home</a>
<br/><br/>  
<form name="form1" method="post" action="editaction.php">
    <table border="0">
        <tr> 
            <td>Name</td>
            <td><input type="text" name="name" value="<?php echo 
$name;?>"></td>
        </tr>
        <tr> 
            <td>Age</td>
            <td><input type="text" name="age" value="<?php echo 
$age;?>"></td>
        </tr>
        <tr> 
            <td>Email</td>
            <td><input type="text" name="email" value="<?php echo 
$email;?>"></td>
        </tr>
        <tr>
            <td><input type="hidden" name="id" value=<?php echo 
$_GET['id'];?>></td>
            <td><input type="submit" name="update" value="Update">
</td>
        </tr>
    </table>
</form>
</body>
</html>

editaction.php

<?php
include_once("classes/Crud.php");
include_once("classes/Validation.php");
$crud = new Crud();
$validation = new Validation();
if(isset($_POST['update']))
{   
$id = $crud->escape_string($_POST['id']);   
$name = $crud->escape_string($_POST['name']);
$age = $crud->escape_string($_POST['age']);
$email = $crud->escape_string($_POST['email']); 
$msg = $validation->check_empty($_POST, array('name', 'age', 'email'));
$check_age = $validation->is_age_valid($_POST['age']);
$check_email = $validation->is_email_valid($_POST['email']);
if($msg) {
    echo $msg;      
    echo "<br/><a href='javascript:self.history.back();'>Go 
Back</a>";
} elseif (!$check_age) {
    echo 'Please provide proper age.';
} elseif (!$check_email) {
    echo 'Please provide proper email.';    
} else {    
    $result = $crud->execute("UPDATE users SET 
name='$name',age='$age',email='$email' WHERE id=$id");
    header("Location: index.php");
}
}
?>

DB SCHEME:

create database test;
use test;
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) NOT NULL,
`age` int(3) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY  (`id`)
);

Crud.php

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
include_once 'DbConfig.php';
class Crud extends DbConfig
{
public function __construct()
{
    parent::__construct();
}
public function getData($query)
{       
    $result = $this->connection->query($query); 
    if ($result == false) {
        return false;
    }   
    $rows = array();    
    while ($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }   
    return $rows;
}   
public function execute($query) 
{
    $result = $this->connection->query($query); 
    if ($result == false) {
        echo 'Error: cannot execute the command';
        return false;
    } else {
        return true;
    }       
}
public function delete($id, $table) 
{ 
    $query = "DELETE FROM $table WHERE id = $id";   
    $result = $this->connection->query($query);
    if ($result == false) {
        echo 'Error: cannot delete id ' . $id . ' from table ' . $table;
        return false;
    } else {
        return true;
    }
}
public function escape_string($value)
{
    return $this->connection->real_escape_string($value);
}
}
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Can you turn on the php errors: `error_reporting(E_ALL); ini_set('display_errors', 1);` ? – Sayonara Oct 23 '17 at 10:43
  • 1
    Here is the obligatory comment about the lack of prepared statements and bound parameters. Do not build queries using variables like that, dangerous for SQL injection. https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php and http://bobby-tables.com/php. – Nic3500 Oct 23 '17 at 10:48
  • @Sayonara i add it in my add.php, it doesn't show anything. – Александр Шаповалов Oct 23 '17 at 10:51
  • Please do this to help us understand. Print your queries before you send them to crud->execute. This will validate that the query is what you expect. Then add the code to crud.php, the error might be there. Is there nothing at all in your error_log file? – Nic3500 Oct 23 '17 at 10:52
  • 1
    And show us you table **users** schema – nacho Oct 23 '17 at 11:02
  • There is nothing at all(. I was trying to push add button (name = 'Submit' it calls add.php functions) without any parameters and my browser have started to load http://localhost:63342/myprohect/add.php (white screen). It doesnt run check lines in code. I added my DB Scheme in main post. – Александр Шаповалов Oct 23 '17 at 11:03
  • Changed cli interpreter to 5.6: Deprecated: Automatically populating $HTTP_RAW_POST_DATA is . deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0 Warning: Cannot modify header information - headers already sent in Unknown on line 0 Array ( [type] => 2 [message] => Cannot modify header information - headers already sent [file] => Unknown [line] => 0 ) – Александр Шаповалов Oct 24 '17 at 03:17

1 Answers1

0

The problem was with phpStorm. My app won't work with PHP interpreter running inside phpStorm.

I run my app in Apache2 HTTP server and it works well.

halfer
  • 19,824
  • 17
  • 99
  • 186