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

and a table with a column
. 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
. All the rows display the same figure. Can any one guide me through this?


* Start the session.

require 'connect-test.php';

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

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



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

Answer Source


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


$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