Linda May Linda May - 5 years ago 114
PHP Question

When I insert data, it shows '1' in database - php,sql

I have a strange problem. I'm learning prepared statements. I created a simple page with 1 field

nama
and a table with a column
nama
. I just want to see the data goes in. When I press the submit button, there is data in the table, but everytime I insert data it shows
1
. All the rows display the same figure. Can any one guide me through this?

<?php

/**
* Start the session.
*/
session_start();

require 'connect-test.php';

$nama= isset($_POST['nama']);

$stmt=$conn->prepare("INSERT INTO test (nama) VALUES (?)");
$stmt->bind_param("s", $nama);
$stmt->execute();
if (!$stmt)
{ printf("Errormessage: %s\n", $mysqli->error);}

$stmt->close();
$conn->close();

?>

<html>
<body>
<form name="form2" method="post" action="test-deletenanti.php">
<p>Name :
<input type="text" name="nama" id="nama">
</p>
<p>
<input type="submit" name="button" id="button" value="Submit">
</p>
</form>
</body>
</html>

Answer Source

Change

$nama = isset($_POST['nama']);

to:

$nama = $_POST['nama'];

isset returns true or false depending on whether the variable is set, so that's what you're putting into $nama, instead of the actual value of the input.

You should use isset() in an if statement around the whole thing:

if (isset($_POST['nama'])) {
    $nama = $_POST['nama'];

    // rest of code to insert into DB
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download