Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 26th, 2013, 05:06 AM
Registered User
 
Join Date: Feb 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default php mysql insert multiple row data problem

i have a big problem this script seems working properly but data base looks so badly. just like his
-------------------------
|word |initial |logic|
-------------------------
|ansion | 1 | a |
|ncel | 1 | a |
|tree | 1 | a |
| bandege | 1 | a |
-------------------------
but i want words mansion, cancel, bigtree, bandage. every time no problem how many rows here last row working exactly what i want. please help me. am using wamp php 5.4 mysql 5.5 and apache 2.22.22.


//php script
<?php
$words= array('mansion','cancel','bigtree','bandege');

foreach( $words as $key => $word ){
$initial = "1";
$logic = "a";
$obj = new en_word( array(
"word"=> isset( $word ) ? (string) $word :"",
"initial"=> isset( $initial ) ? (string) $initial :"",
"logic"=> isset( $logic ) ? (string) $logic :""
) );
$obj->insert();
}
?>

//en_word class
<?php
class en_word extends DataObject {
protected $data = array(
"word" => "",
"initial" => "",
"logic" => ""
);
public function insert() {
$conn = parent::connect();
$sql = "INSERT INTO " . TBL_EN_WORD . " (
word,
initial,
logic
) VALUES (
:word,
:initial,
:logic
)";
try {
$st = $conn-> prepare( $sql );
$st-> bindValue( ":word", $this->data["word"], PDO::PARAM_STR );
$st-> bindValue( ":initial", $this->data["initial"], PDO::PARAM_STR );
$st-> bindValue( ":logic", $this->data["logic"], PDO::PARAM_STR );
$st-> execute();
parent::disconnect( $conn );
} catch ( PDOException $e ) {
parent::disconnect( $conn );
die( "Query failed: " . $e-> getMessage() );
}
}
}
?>

//dataobject class
<?php
abstract class DataObject {
protected $data = array();

public function __construct( $data ) {
foreach ( $data as $key => $value ) {
if ( array_key_exists( $key, $this-> data ) ) $this-> data[$key] = $value;
}
}

protected static function connect() {
try {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$conn-> setAttribute( PDO::ATTR_PERSISTENT, true );
$conn-> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
die( "Connection failed: " . $e-> getMessage() );
}
return $conn;
}

protected static function disconnect( $conn ) {
$conn = "";
}
} ?>

//mysql en_word table structure
word VARCHAR(50) NOT NULL UNIQUE,
initial VARCHAR(25) NOT NULL,
logic TEXT NOT NULL





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL/PLSQL QUERY TO INSERT MULTIPLE ROW VALUES? ratheesh88 Oracle 0 August 30th, 2012 07:20 AM
Multiple Row Insert using Linq arivarasuk ASP.NET 3.5 Professionals 1 January 9th, 2009 11:21 AM
Insert multiple row in server. Somesh .NET Framework 2.0 1 June 1st, 2007 08:03 AM
PHP-MySQL Insert Data problem forexen PHP Databases 1 March 13th, 2007 12:58 AM
How can i delete multiple row through PHP coding taha_elearner PHP Databases 1 October 28th, 2005 05:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.