简体   繁体   中英

I want to fetch values from 2 textboxes and add them to mysql database using PHP

I want to fetch values from 2 textboxes and add them to mysql database using PHP. My HTML:

<form name='form' method='post'>
  Username:<input type="text" id="username" name="username"/>
   Password:<input type="text" id="password" name="password"/>
 <input type="submit" name="submit" value="Add" style="width:50%;padding:10px;">

And here goes the PHP:

<?php
     $username="";
     $pwd="";
     $pword="";
        try{
        global $username,$pwd;
        if ( ! empty($_POST['username']) && ! empty($_POST['password'])){
        $username = $_POST['username'];
         $pwd = $_POST['password'];
        }        
        $servername = "localhost";
        $conusername = "root";
        $conpassword = "";
        $dbname = "annapoorna";
        $dsn='mysql:dbname='.$dbname.';host='.$servername;
        $conn = new PDO($dsn, $conusername, $conpassword);
        $sql='INSERT INTO users(username, password) values (:username,:password)';
        $sth=$conn->prepare($sql); 
        $sth->execute(array(':username'=>$username,':password'=>$pwd ));
        }

catch(PDOException $e){
    echo "Error: " . $e->getMessage();
     }
   ?>    

But the values are not adding to the database. Why?

Input values are received by php trougth name attr HTML code:

<form name='form' method='post'>
Username:<input type="text" id="username" name='username'/>
Password:<input type="text" id="password" name='password'/>
<input type="submit" name="submit" value="Add" style="width:50%;padding:10px;">

Global not needed

PHP code:

<?php
 $username="";
 $pwd="";
 $pword="";
    try{
    if ( ! empty($_POST['username']) && ! empty($_POST['password'])){
    $username = $_POST['username'];
     $pwd = $_POST['password'];
    }        
    $servername = "localhost";
    $conusername = "root";
    $conpassword = "";
    $dbname = "annapoorna";
    $dsn='mysql:dbname='.$dbname.';host='.$servername;
    $conn = new PDO($dsn, $conusername, $conpassword);
    $sql='INSERT INTO users(username, password) values (:username,:password)';
    $sth=$conn->prepare($sql); 
    $sth->execute(array(':username'=>$user`enter code here`name,':password'=>$pwd ));
    }catch(PDOException $e){
echo "Error: " . $e->getMessage();
 }
  ?>  

A bit too long for a comment, but as it will probably help you to solve the problem: You should set up PDO to throw exceptions when something goes wrong. Now you are trying to catch them, but probably nothing gets thrown.

You can do that in the constructor:

$conn = new PDO(
    $dsn, 
    $conusername, 
    $conpassword, 
    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
);

You should also enable error handling and display them all. You can do that by adding this to the top of your script:

error_reporting(E_ALL);
ini_set('display_errors', '1');

Note that I assume that you have already added the missing name attributes in the form as mentioned in the comments.

Also note that you should hash and salt the password using for example password_hash() , never store plain-text, encrypted or weakly hashed passwords.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM