Click here to Skip to main content
15,916,462 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<body>
<div >
  <img class="img" src="uog.png"  width="100" height="100" style="background-color:rgb(98, 132, 160);" ><img src="h.png" class="img1" width="800" height="100 style="background-color:rgb(98, 132, 160);" >
 </div>
 <form  method="post">
  <label>First Name</label>
  <input type="text" name="Firstname"><br>
  <label>Last Name</label>
  <input type="text" name="Lastname"><br>
  <label>User-ID</label>
  <input type="text" name="User_ID"><br>
  <label>Age</label>
  <input type="number" name="Age"><br>
  <label>username</label>
  <input type="text" name="Role"><br>
  <label>username</label>
  <input type="text" name="username"><br>
  <label>password</label>
  <input type="text" name="password"><br>
  <label>Sex</label>
  <input type="text" name="Sex"><br>
  <input type="submit" name="submit">
  </form>
  <?php
      require_once('connect.php');
      echo "<br>";

      if(isset($_POST['submit'])){
          $First_name=$_POST['Firstname'];
          $Last_name=$_POST['Lastname'];
          $User_ID=$_POST['User_ID'];
          $Age=$_POST['Age'];
          $Role=$_POST['Role'];
          $username=$_POST['username'];
          $password=$_POST['password'];
          $Sex=$_POST['Sex'];

          $sql = "INSERT INTO user (Firstname,Lastname,User_ID,Age,Role,username,password,Sex) VALUES('$First_name','$Last_name','$User_ID','$Age','$Role','$username','$password','$Sex')";
          $result = mysqli_query($server,$sql);
      }


  ?>


What I have tried:

I have tried everything I could but it is still not working!!!
Posted
Updated 9-Aug-21 0:58am
Comments
Richard MacCutchan 9-Aug-21 6:53am    
Since you do not check the result of the mysqli_query statement, it is impossible to know what went wrong. Are you sure that the $server variable is correct, and has been opened?

Also, you should not be storing passwords in clear text.

Are you sure Age is a string ?
Advice: 'echo $sql;' to see what is the query executed.
PHP
$sql = "INSERT INTO user (Firstname,Lastname,User_ID,Age,Role,username,password,Sex) VALUES('$First_name','$Last_name','$User_ID','$Age','$Role','$username','$password','$Sex')";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) Never store passwords in clear text - it is a major security risk. See here: PHP: password_hash - Manual[^]

And remember: if this is web based and you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.

Fix those through your whole app, and you may find your other problem has gone away.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900