Home Can't check if data exists in database
Reply: 1

Can't check if data exists in database

DummyTarget
1#
DummyTarget Published in 2017-12-07 21:16:06Z

I know i post a lot of problems lately, but this one problem which i couldn't solve for about 1 week, I tried million times to check if data exists but still nothing. Searched, checked same problem but didn't get a hint. So please tell me what is wrong here. I try everything on my own but if i meet a problem i can't solve for a while. I ask here for help.

<!DOCTYPE html>
<html>
<head>
    <title>Test Title</title>
</head>
<body>
    <?php 
        class Database{
            public $db;

            public function __construct($host,$username,$password,$dbname){

                $this->db=new MySQLi($host,$username,$password,$dbname);

            }
            public function getData(){
                $query="SELECT * FROM artisan";
                $result=$this->db->query($query); 
                $users=array();
                if($result->num_rows>0){
                    while($row=$result->fetch_assoc()){
                    $users[]=array(
                        "id"=>$row['id'],
                        "username"=>$row['username'],
                        "email"=>$row['email']
                        );

                    }
                }else{
                    echo "No results found!";
                }
                return $users;
            }
            public function getContent(){
                $query="SELECT * FROM content";
                $result=$this->db->query($query);
                $values=array();
                if($result->num_rows>0){
                    while($row=$result->fetch_assoc()){
                    $values[]=array(
                        "title"=>$row['title'],
                        "body"=>$row['body']
                    );
                }

            }
            return $values;
            }

        public function insertData(){
            $title=$_POST['title'];
            $query="INSERT INTO content(title,body) VALUES (?,?)";
            $result=$this->db->query("SELECT COUNT(*) FROM content WHERE title=$title");
            $stmt=$this->db->prepare($query);
            $stmt->bind_param("ss",$_POST['title'],$_POST['body']);
            if(!empty($_POST['title']) || !empty($_POST['body'])){
            if($result->num_rows){
            echo "User allready exists";
            }else{
                    $stmt->execute();
            }
        }
        }

        private function validate(){
            $query="SELECT title and body FROM contents";
            $result=$this->db->query($query);
            if($result){
                echo "hi";
            }
        }
    }

        $database=new Database('localhost','root','','test');
        $users=$database->getData();
        $values=$database->getContent();
        $database->insertData();
     ?>

     <style type="text/css">
        .container{
            text-align:center;
        }
        table, th, td, {
            border: 1px solid black;
        }
        table{
            width:100%;
        }
        .content{
            border:1px solid black;
        }
     </style>
     <div class="container">
      <?php
        for($i=0;$i<count($values);$i++):
       ?>
    <div class="content">
        <?php echo $values[$i]["title"]."<br>"; ?>
        <?php echo $values[$i]["body"]."<br>";?>
     </div>
    <?php endfor;?>
     </div>
     <table>
  <colgroup>
    <col span="2" style="background-color:red">
    <col style="background-color:yellow">
  </colgroup>
  <tr>
    <th>ID</th>
    <th>username</th>
    <th>email</th>
  </tr>
  <tr>
    <td><?php foreach($users as $user)
    echo $user['id']."<br>";
    ?></td>
    <td><?php foreach($users as $user)
    echo $user['username']."<br>";
    ?></td>
    <td><?php foreach($users as $user)
    echo $user['email']."<br>";
    ?></td>
  </tr>
</table>
<form action="index.php" method="POST">
    <input type="text" name="title">
    <input type="text" name="body">
    <input type="submit" name="submit">
</form>
</body>
</html>
Funk Forty Niner
2#
Funk Forty Niner Reply to 2017-12-07 22:46:57Z

"$result=$this->db->query("SELECT title FROM content WHERE title='$title'"); just changed everything to this and it worked,$result was returning false,so i tried to solve it but i guess adding ' ' to $title solved idk why. – DummyTarget 12 mins ago"

That's because $title is a string literal and it needs to be quoted. You really should use a prepared statement for everything though, and will help safeguard against a potential SQL injection.

Reference:

  • https://dev.mysql.com/doc/refman/5.5/en/string-literals.html

Debugging:

  • http://php.net/manual/en/mysqli.error.php
  • http://php.net/manual/en/function.error-reporting.php
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311103 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO