C'mon
C'mon

Reputation: 51

Incorrect value is inserted in database

I have this code:

<form action="#" method="post">
    <input type="text" name="famname"/>
    <input type="submit" name="submit"/>
</form>
<?php 
if(isset($_POST['submit'])){
    if(!isset($_POST['famname'])){
        echo "Please set Family Name.";
        die();
    }
    $mysqli = new mysqli('localhost', 'root', 'marais19', 'famlink');

    if($mysqli->connect_errno) {
        echo "Connection Failed (".$mysqli->connect_errno.") : ".$mysqli->connect_error;
        die();
    }
    $e = 'yes';
    $stmt = $mysqli->prepare("INSERT INTO families(famname) VALUES (?)");
    $stmt->bind_param('d', $e);
    $stmt->execute();
}
?>

But if I type Smith into the input box it only puts 0 into the database. The SQL code is as followed:

CREATE TABLE IF NOT EXISTS `families` (
    `famname` varchar(30) CHARACTER SET utf8 NOT NULL DEFAULT 'NOFAM',
    KEY `famname` (`famname`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

And:

INSERT INTO `families` (`famname`) VALUES
('0');

How can I make it to put Smith into the database and not 0

Upvotes: 0

Views: 72

Answers (1)

John Conde
John Conde

Reputation: 219804

You are casting it to a number (actually a double) when you bind it to the query. You need to use s instead of d:

$stmt->bind_param('d', $e);

should be

$stmt->bind_param('s', $e);

See the manual on types:

i   corresponding variable has type integer
d   corresponding variable has type double
s   corresponding variable has type string
b   corresponding variable is a blob and will be sent in packets

Upvotes: 4

Related Questions