I don't know what I'm doing wrong here, but I'm having hard time trying to insert data from a form into the database.The $_session['username'] is set and the database is included and working

Here is my table

  CREATE TABLE IF NOT EXISTS `accounts` (
  `accid` int(11) NOT NULL AUTO_INCREMENT,
  `acc_number` varchar(50) NOT NULL,
  `cust_name` varchar(255) NOT NULL,
  `cust_tel` varchar(50) NOT NULL,
  `cust_address` varchar(255) NOT NULL,
  `cust_opendate` date NOT NULL,
  `cust_openamount` decimal(13,2) NOT NULL,
  `cust_balance` decimal(13,2) NOT NULL,
  `cust_message` text NOT NULL,
  `cust_openby` varchar(50) NOT NULL,
  PRIMARY KEY (`accid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

and here is my code

<?php
session_start();


    $success = array();
    $errors = array();

    include('header.php');

    if (isset($_SESSION['username'])) {
        include_once('libs/db-con.php');

        if (isset($_POST['submit'])) {

            $accNumber = $_POST['accnumber'];
            $custName = $_POST['custname'];
            $tel = $_POST['tel'];
            $addess = $_POST['address'];
            $openDate = $_POST['opendate'];
            $openAmount = $_POST['openamount'];
            $balance = $_POST['balance'];
            $message = $_POST['message'];
            $openBy = $_SESSION['username'];

        if (empty($accNumber) || empty($custName) || empty($tel) || empty($addess) || empty($openDate)  || empty($openAmount) || empty($balance) || empty($openBy)) {

            $errors[] = 'All fields are required';

        }else{

            $query = $pdo->prepare("INSERT INTO accounts(acc_number, cust_name, cust_tel, cust_address, cust_opendate, cust_openamount, cust_balance, cust_message, cust_openby)  VALUE(?,?,?,?,?,?,?,?,?)" ) or die(mysql_error());

            $query->bindValue(1,$accNumber);
            $query->bindValue(2,$custName);
            $query->bindValue(3,$tel);
            $query->bindValue(4,$address);
            $query->bindValue(5,$openDate);
            $query->bindValue(6,$openAmount);
            $query->bindValue(7,$balance);
            $query->bindValue(8,$message);
            $query->bindValue(9,$openBy);


        $inserted = $query->execute();
        $inserted ? $success[] = 'New account has been added' : $errors[] = 'New account has not been added';

        }

    }


            $query = $pdo->prepare("SELECT max(acc_number) FROM accounts");

                    $query->execute();

                   while($num = $query->fetchAll()){

                    if($num[0] == null){

                    $accnumber = "100001";

                    }else{

                    $accnumber = $num[0]++; 
                }    
            }       

?>
     <div class="today-acc">
        <h3>create new account</h3>
        <div id="form-newacc">

    <form action="" method="post">
            <table>
                <tr>
                <td><label for="accnumber">Account No:</label>
                    <input type="text" name="accnumber" value="<?php echo $accnumber[0]; ?>"></td>
                <td><label for="custname">Name:</label>
                    <input type="text" name="custname"></td>
                <td><label for="tel">Tel:  </label>
                    <input type="text" name="tel"></td>
                </tr>
                <tr>
                <td><label for="address">Address:</label>
                    <input type="text" name="address"></td>
                <td><label for="opendate">Open date:</label>
                    <input type="text" name="opendate" id="datepicker"></td>
                <td><label for="openamount">Open Amount:</label>
                    <input type="text" name="openamount"></td>
                </tr>
                <tr>
                <td><label for="balance">Balance:</label>
                    <input type="text" name="balance"></td>
                <td><label for="message">Message:</label>
                    <textarea name="message" id="message"></textarea></td>
                </tr>
                </table>
                    <input type="submit" name="submit" Value="Create">
                    <input type="reset" name="reset" Value="clear">      
                </form>

                    <?php foreach ($success as $successes) {
                        echo '<div id="success">'.$successes.'</div>';
                    }?>
                    <?php foreach ($errors as $error) {
                        echo '<div id="errors">'.$error.'</div>';
                    }?>
    </div>
    </div>


<?php
}
else{

        header("location: login.php");
    }
?>   

Recommended Answers

All 8 Replies

I'm having hard time trying to insert data

Do you get an error? What is happening?

The following is wrong, as you are mixing PDO and MySQL here. PDO raises exceptions on errors.

$query = $pdo->prepare("INSERT INTO accounts(acc_number, cust_name, cust_tel, cust_address, cust_opendate, cust_openamount, cust_balance, cust_message, cust_openby)  VALUE(?,?,?,?,?,?,?,?,?)" ) or die(mysql_error());

Remove the or die() as it will fail if it is called.

Are all your bound parameters strings?

The only error i get is on line 45

$errors[] = 'New account has not been added';

I removed the Mysql or die() and its still the same.

Are you sure PDO is installed on the server?

yes PDO is installed and working.. it seems that i cant grab the values from the $_POST variable. for some reason if i add the value directly to the bindValue it works like $query->bindValue(1,'100006');
but if i do this $query->bindValue(1,$accNumber); it does not work.

That's perhaps because the default binding type is string. Perhaps you should a the third parameter to indicate the right type.

you see im new to php/mysql, how do i do that? adding the third parameter.

thank you, i will try that.

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.