Lance Hietpas
Lance Hietpas

Reputation: 361

0 value when inserting data into mysql table using php

I am new to php and mysql and seem to be running into an issue while trying to insert data into a mysql table.

I have a insert.php page with the following below

<?php
$db_host = 'hostname';
$db_user = 'username';
$db_pass = 'password';
$db_name = 'databasename';

 if (!empty($_POST))
{   
    $con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
    if ($con->connect_error)
        die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());

    $sql = "INSERT INTO train2015 (emp_num, name, supervisor) VALUES (?,?,?)";
    if (!$stmt = $con->prepare($sql))
        die('Query failed: (' . $con->errno . ') ' . $con->error);

    if (!$stmt->bind_param('ssi',$_POST['emp_num'],$_POST['name'],$_POST['supervisor']))
        die('Bind Param failed: (' . $con->errno . ') ' . $con->error);

    if (!$stmt->execute())
            die('Insert Error ' . $con->error);

    echo "Record added";
    $stmt->close();
    $con->close();
}
?>

I then have add.php page

<?php
include('includes/insert.php');

?>

<html>
<body>
<form action="includes/insert.php" method="post">

Employee Number : <input type="text", name="emp_num"> </br>
Full Name : <input type="test" name="name"> </br>
Supervisor Name : <input type="text" name="supervisor"></br>

<input type="submit">

</form>
</body>
</html>

Everything seems to work fine, I hit the submit button, I see the Record added, go and check the table and I see the emp_num is fine, the name field is fine, but the supervisor always has a 0 in it. I seem to be missing something.

Thanks in advance for your help.

Upvotes: 3

Views: 4107

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74230

As requested:

supervisor's most likely a string, so do sss - plus that comma in type="text", get rid of that.

You also need to check the column's type if it's an int or varchar.

Strings are varchar. Check the length also.

Upvotes: 4

Related Questions