0

I am trying to insert the following query using prepared statement as follows, but it fails to add. Following is the code:

$name=$_POST['text_name'];
$rn=$_POST['text_rn'];
$email=$_POST['email'];
$gender=$_POST['gender'];
$upass= password_hash($_POST['text_pass'], PASSWORD_DEFAULT);
$addrec = $conn->prepare("INSERT INTO students(name,rollno,password,gender,email) VALUES (?,?,?,?,?)");
$addrec->bind_param("sssss",$name,$rollno,$password,$gender,$email);
if($addrec->execute()){
    echo    "<div class='w3-panel w3-green w3-display-container' style='width:40%;'>
                <span onclick='this.parentElement.style.display='none''
                class='w3-button w3-large w3-display-topright'>&times;</span>
                <p>New record created successfully</p>
            </div>";
}
else
    echo "<div class='w3-panel w3-red w3-display-container' style='width:40%;'>
            <span onclick='this.parentElement.style.display='none''
            class='w3-button w3-large w3-display-topright'>&times;</span>
            <p>Record not added</p>
        </div>";

The code is pretty simple but it is showing 'Record not added', which is else case. Following is my table structure.

+------------------+---------------+-------+------+--------------------+----------------+--+
|      Field       |     Type      | Null  | Key  |      Default       |     Extra      |  |
+------------------+---------------+-------+------+--------------------+----------------+--+
| id               | int(11)       | NO    | PRI  | NULL               | auto_increment |  |
| name             | varchar(200)  | NO    |      | NULL               |                |  |
| email            | varchar(200)  | NO    |      | NA                 |                |  |
| password         | text          | NO    |      | NULL               |                |  |
| gender           | varchar(6)    | NO    |      | NULL               |                |  |
| rollno           | int(255)      | NO    |      | NULL               |                |  |
| image            | varchar(255)  | NO    |      | default-pic.png    |                |  |
| personal_notice  | varchar(500)  | NO    |      | Not Available Yet  |                |  |
+------------------+---------------+-------+------+--------------------+----------------+--+

What could be the problem? Please help.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Thalow
  • 15
  • 5

1 Answers1

0

$rollno in $addrec->bind_param("sssss",$name,$rollno,$password,$gender,$email); should be $rn
And also $password should be $upass

Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26