0

Im facing one problem. I already created session. All I want is when someone clicks on submit button my php code executes. I want to store the name of user who clicked button into mysql. When I click on it I get an echo "created successful" but mysql table is filled with zeros. I can echo ".$r['username']." and its ok.

 <?php
    session_start();
    if($_SESSION['user']==''){
        header("Location:login.php");
    }else{
        $dbh=new PDO('mysql:dbname=myDB;host=127.0.0.1', 'user', 'pass');
        $sql=$dbh->prepare("SELECT * FROM users WHERE id=?");
        $sql->execute(array($_SESSION['user']));
        while($r=$sql->fetch()){
            sql = "INSERT INTO table (tablerow) VALUES ('`".$r['username']."`')";

            try {
                $dbh->exec($sql);
                echo " `".$r['username']."` inserted successfully";
            } catch(PDOException $e) {
                echo $sql . "<br>" . $e->getMessage();
            }
        }

        $conn = null;
    }
    ?>
Leap
  • 158
  • 11
  • 1
    `('mycolumn')` incorrect identifier qualifiers. http://dev.mysql.com/doc/en/identifier-qualifiers.html and http://php.net/manual/en/pdo.error-handling.php would have told you about the syntax error. – Funk Forty Niner Mar 16 '16 at 16:26
  • To get PDO to throw an exception, I think you need to specify. After the connection is made: `$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` – spencer7593 Mar 16 '16 at 16:30
  • $sql = "INSERT INTO `table` (tablerow) VALUES ('`".$r['username']."`')"; Now i got inputs. But these inputs are 0 no matter which user click on it . in table shows only zero-s – Leap Mar 16 '16 at 17:34

0 Answers0