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
", $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>
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
}
Returns TRUE if var exists and has value other than NULL, FALSE otherwise.
Your condition is TRUE and set it to 1
$nama= isset($_POST['nama']);
Just change to
if (isset($_POST['nama'])) {
$nama = $_POST['nama'];
$stmt = $conn->prepare("INSERT INTO test (nama) VALUES (?)");
$stmt->bind_param("s", $nama);
$stmt->execute();
if (!$stmt) {
printf("Errormessage: %s
", $mysqli->error);
}
$stmt->close();
$conn->close();
}