i am getting that error when i try to insert a record. this is my code

<?php
include "config.php";
include "header.php";
?>
<a href="index.php" class="btn btn-success btn-md"><span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span> Back</a>
<?php
if(isset($_POST['bts'])):
  if($_POST['nm']!=null && $_POST['ln']!=null && $_POST['gd']!=null && $_POST['tl']!=null  && $_POST['ar']!=null){
     $stmt = $mysqli->prepare("INSERT INTO personal(name,lname,gender,telp,address) VALUES (?,?,?,?,?)");
     $stmt->bind_param('ssss', $nm, $ln, $gd, $tl, $ar);

     $nm = $_POST['nm'];
     $nm = $_POST['ln'];
     $gd = $_POST['gd'];
     $tl = $_POST['tl'];
     $ar = $_POST['ar'];

     if($stmt->execute()):
?>
<p></p>
<div class="alert alert-success alert-dismissible" role="alert">
  <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
  <strong>Berhasil!</strong> Silahkan tambah lagi, jika ingin keluar klik <a href="index.php">Home</a>.
</div>
<?php
     else:
?>
<p></p>
<div class="alert alert-danger alert-dismissible" role="alert">
  <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
  <strong>Gagal!</strong> Gagal total, Silahkan coba lagi!!!.<?php echo $stmt->error; ?>
</div>
<?php
     endif;
  } else{
?>
<p></p>
<div class="alert alert-warning alert-dismissible" role="alert">
  <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
  <strong>Gagal!</strong> Form tidak boleh kosong, tolong diisi.
</div>
<?php
  }
endif;
?>

        <p><br/></p>
        <div class="panel panel-default">
          <div class="panel-body">

        <form role="form" method="post">
          <div class="form-group">
            <label for="nm">Name</label>
            <input type="text" class="form-control" name="nm" id="nm" placeholder="Enter Name">
          </div>
          <div class="form-group">
            <label for="ln">lName</label>
            <input type="text" class="form-control" name="ln" id="ln" placeholder="Enter Name">
          </div>
          <div class="form-group">
            <label for="gd">Gender</label>
            <select class="form-control" id="gd" name="gd">
              <option>Male</option>
              <option>Female</option>
            </select>
          </div>
          <div class="form-group">
            <label for="tl">Phone</label>
            <input type="tel" class="form-control" name="tl" id="tl" placeholder="Enter Phone">
          </div>
          <div class="form-group">
            <label for="ar">Address</label>
            <textarea class="form-control" name="ar" id="ar" rows="3"></textarea>
          </div>
          <button type="submit" name="bts" class="btn btn-default">Submit</button>
        </form>
<?php
include "footer.php";
?>

and here is it is my database.

CREATE TABLE IF NOT EXISTS `personal` (
`id_personal` int(11) NOT NULL,
  `name` varchar(45) NOT NULL,
  `lname` varchar(45) NOT NULL,
  `gender` varchar(20) NOT NULL,
  `telp` varchar(25) NOT NULL,
  `address` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

Recommended Answers

All 4 Replies

Hi,

you're defining the variables after the binding, this will lead to an error, try:

$nm = $_POST['nm'];
$nm = $_POST['ln'];
$gd = $_POST['gd'];
$tl = $_POST['tl'];
$ar = $_POST['ar'];

$stmt->bind_param('ssss', $nm, $ln, $gd, $tl, $ar);

No its not working you way too, just with 4 parameters it is working example:

if(isset($_POST['bts'])):
  if($_POST['nm']!=null && $_POST['gd']!=null && $_POST['tl']!=null  && $_POST['ar']!=null){
     $stmt = $mysqli->prepare("INSERT INTO personal(name,gender,telp,address) VALUES (?,?,?,?)");
     $stmt->bind_param('ssss', $nm, $gd, $tl, $ar);

     $nm = $_POST['nm'];
     $gd = $_POST['gd'];
     $tl = $_POST['tl'];
     $ar = $_POST['ar'];

     if($stmt->execute()):

this database,

CREATE TABLE IF NOT EXISTS `personal` (
`id_personal` int(11) NOT NULL,
  `name` varchar(45) NOT NULL,
  `gender` varchar(20) NOT NULL,
  `telp` varchar(25) NOT NULL,
  `address` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

This is working perfect, but when i Add lastname it won't work

Oh that's correct: if you define four types, then submit four variables.

For example, with one variable to submit you would write:

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

To submit two variables:

$stmt->bind_param('ss', $nm, $gd);

And so on. s stands for string, you can check the types here:

By submitting an extra variable MySQLi will generate a warning because the definition ssss does not match the number of submitted variables.

Now, by defining the variables after the bind, PHP will also generate a notice for each undefined variable at line four, to avoid it you should define them before, as in my previous example.

Thank you sir, very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.