fripradrocae fripradrocae - 2 months ago 8
MySQL Question

PHP OOP Query Does Not Insert a New Row Into DB

I'm working on a custom CMS using PHP OOP and now I have faced a really bad issue ! Basically I have a form like this:

<form role="form" method="POST" action="">
<div class="box-body">
<div class="form-group">
<label>Menu name</label>
<input type="text" class="form-control" placeholder="Enter menu name" name="mname" required>
</div>
<div class="form-group">
<label>Menu link numbers</label>
<input type="number" class="form-control" placeholder="Number of menu items" min="1" max="9" name="mnumbers" required>
</div>
</div>
<div class="box-footer">
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
</div>
</form>


And as the action I coded this:

<?php
if(isset($_POST['submit'])){
$menu_name = $_POST['mname'];
$menu_numbers = $_POST['mnumbers'];
$nav = new Navigation();
$nav->NewMenu($menu_name,$menu_numbers);
}
?>


So as you can see I have called a class named
Navigation
. This class goes like this:

<?php
class Navigation
{
private $db;
public function __construct()
{
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function NewMenu($menu_name,$menu_numbers)
{
if(!empty($menu_name)&&!empty($menu_numbers))
{
$ins = $this->db->prepare("INSERT INTO menu_nav (menu_name, menu_items) VALUES ('?', '?')");
$ins->bindParam(1,$menu_name);
$ins->bindParam(2,$menu_numbers);
$ins->execute();
}else{
header("Location: maint/php/includes/errors/009.php");
exit();
}
}
}
?>


So everything looks great and the data must be inserted to the table but the problem is that ,whenever I run this code nothing added into the db except the ? mark and zero number ! Here's the print screen of my MySQL database:
click to see

So what's going wrong here, can someone tell me what's the problem ?

Answer

Your INSERT statement won't be prepared for binding. Just remove the ' before and after the ?s. Like so:

$ins = $this->db->prepare("INSERT INTO menu_nav (menu_name, menu_items) VALUES (?, ?)");
                                                                                ^  ^