-4

Im having trouble inserting an integer in my DB the string variable work just fine, but the integer is getting set to 0. Here is my code.

if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['submit'])) {
    $empId = $conn-> escape_string($_POST['empID']);
    $empIdInt = (int)$empId;
    $name_esc = $conn-> escape_string($_POST['name']);
    $username_esc = $conn-> escape_string($_POST['username']);
    $password_esc = $conn-> escape_string($_POST['password']);
    $confirm_password_esc = $conn-> escape_string($_POST['confirm_password']);
    $empAddress_esc = $conn-> escape_string($_POST['empAddress']);

    $query = "
    INSERT INTO users (empId,username,password,securityLevel,name,empAddress) 
    VALUES 
    ('$empIdInt','$username_esc','$password_esc','user','$name_esc','$empAddress_esc')";

    if ($password_esc == $confirm_password_esc)
    {
        $result = $conn->query($query) or die(mysqli_error($conn)); 
    } else {
        echo("Passwords do not match.");
    }
}

<label>Employee ID: </label><input type = "text" name="empId"  /><br><br>

Swatantra Kumar
  • 1,324
  • 5
  • 24
  • 32
  • 1
    Which column? What is the structure of that column? Can you give an example of data? – El_Vanja Apr 09 '20 at 17:15
  • 2
    You're also wide open to [SQL injection](https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work). You should use [prepared statements](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead. – El_Vanja Apr 09 '20 at 17:16
  • thanks for the prepared statement suggestion i will look into that. My DB Structure is 6 columns like int,varchar,varchar,varchar,varchar,varchar – CallMeAaron Apr 09 '20 at 17:29
  • Can you give an example id that fails? – El_Vanja Apr 09 '20 at 17:38
  • Yeah so basically the issues is the number i enter get changed into a 0 so the initial entry validates all fields but the id becomes a 0. when i go to enter another employee i use empId = 2 i get an error duplicate entry 0 for primary key. – CallMeAaron Apr 09 '20 at 17:52
  • I found my mistake in the POST empID vs empId. thanks for your time. – CallMeAaron Apr 09 '20 at 17:57

1 Answers1

-1

If i see correctly, you are trying to insert a "string" into an "integer" column. see here: '$empIdInt' => the ' .. ' makes it a string. drop them and give it a go.

Kosem
  • 373
  • 2
  • 11
  • thanks for the suggestion. I thought that might be an issue as well, but it did not make a difference. – CallMeAaron Apr 09 '20 at 17:52
  • yet you should not enter a string to a int column. even if it didn't solve your problem, i advise to fix that :) – Kosem Apr 09 '20 at 17:54
  • in addition, please provide output of $_POST. do: print_r($_POST); and show the array. You might not be getting the value of the integer at all (and maybe that's why it inserts as zero) – Kosem Apr 09 '20 at 17:55
  • 1
    Yes, Thanks. I did make that change and found the silly mistake i made. In my post i was have empID instead of empId. Thanks for your time. – CallMeAaron Apr 09 '20 at 17:56