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.
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.
php mysql insert multiple row data problem
in PHP Coding Help
Posted
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