Question

Hi there i'm having a lot of problems trying to get past such a basic problem I am sure using mysqli in the object oriented style. I have followed numerous guides and checked the php manual. But the only database operation I seem to be able to carry out with mysqli is retrieving data from tables. I am trying to create a registration page and am just putting post data into a database. I have done this many times with the older style msql_connect but the object oriented style is troubling me. Here is my code.

$db = new mysqli(info is all corrected as i tested database connectivity);

 if ($db->connect_errno > 0){
die('unable to connect to database [' . $db->connect_error . ']');
}
else {
    echo('conenction successful');
}

    $fname = $_POST['fname'];  
    $lname = $_POST['lname']; 
    $uname = $_POST['uname']; 
    $email = $_POST['email']; 
    $email1 = $_POST['emai1']; 
    $ad1 = $_POST['ad1']; 
    $ad2 = $_POST['ad2']; 
    $town = $_POST['town']; 
    $city = $_POST['city']; 
    $postcode = $_POST['postcode'];
    $mphone =  $_POST['mphone'];
    $hphone = $_POST['hphone'];
    $password ="hh";
    if (!($stmt->$db->prepare("INSERT INTO `users` (`username`, `fname`, `lname`, 
                            `password`, `email`, `address1`, `address2`, `town`, 
                            `city`, `postcode`, `mphone`, `hphone`)
                            VALUES
                            (?,?,?,?,?,?,?,?,?,?,?,?)")))
    {
        echo "Prepare failed: (" . $db->errno . ")" . $db->error;
        }

    if (!$stmt->bind_param("ssssssssssii",  $uname, $fname, $lname, $password, $email, $ad1, $ad2, $town, $city, $postcode, $mphone, $hphone))
    {
        echo "binding parameters failed: (" . $stmt->errno . ")" . $stmt->error;
    }               
    if (!$stmt->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}

The Database schema for the users table is this.

CREATE TABLE `users` (

id int(11) unsigned NOT NULL AUTO_INCREMENT, username varchar(10) NOT NULL DEFAULT '', fname varchar(11) NOT NULL, lname varchar(11) NOT NULL, password varchar(50) NOT NULL DEFAULT '', email varchar(40) NOT NULL DEFAULT '', access int(8) NOT NULL DEFAULT '1', address1 varchar(50) DEFAULT NULL, address2 varchar(50) DEFAULT NULL, town varchar(25) DEFAULT NULL, city varchar(20) DEFAULT NULL, postcode varchar(8) DEFAULT NULL, mphone int(11) DEFAULT NULL, hphone int(11) DEFAULT NULL, salt varchar(50) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

Can someone please point me in the right direction with where I am going wrong.

Was it helpful?

Solution

Try this (adjust to your needs):

<?php

if (!$stmt = $db->prepare(/* I'm not gonna write all this down :P */)) {
    // Error output (or better log them somewhere, users don't have to see this stuff)
    return;
}
$stmt->bind_param("ssssssssssii", $uname, $fname, $lname, $password, $email, $ad1, $ad2, $town, $city, $postcode, $mphone, $hphone);
if (!$stmt->execute()) {
    // Error output (again, better to log them somewhere)
    return;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top