3

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.

3
  • 2
    supervisor's most likely a string, so do sss - plus that comma in type="text", get rid of that. Plus, check the column's type if it's an int or varchar. Strings are varchar. Check the length also. Commented Feb 12, 2015 at 16:28
  • 1
    @Fred-ii- or you can make it an answer so we can properly upvote you. :) Commented Feb 12, 2015 at 16:32
  • @JohnConde Done as requested ;-) Commented Feb 12, 2015 at 16:32

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Another happy customer :)
@JohnConde Indeed John. I'm in this to put a smile on a person's face ;-) if some of my jokes don't, some of my answers hopefully will. lol
Sorry, how should we do this? You mean that the price should have integer data type? and also what about sss? Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.