Jump to content

php mysql insert multiple row data problem


wpManoj

Recommended Posts

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.