473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

php mysql multiple row insert problem, only last row inserts properly

am trying to insert multiple raw data using foreach and for loop. script work properly. am using pdo insert data to mysql.
insert work properly but have a big problem. no error reporting. php 5.4 mysql 5.5

Expand|Select|Wrap|Line Numbers
  1. var1 = 'man', var2 = '1', var3 = 'a'
  2. var1 = 'gt', var2 = '1', var3 = 'a'
  3. var1 = 'cook', var2 = '1', var3 = 'a'
  4.  
mysql table
Expand|Select|Wrap|Line Numbers
  1. var1 varchar(50) not null unique,
  2. var2 varchar(50) not null,
  3. var3 tinytext not null
  4.  
am suppose
Expand|Select|Wrap|Line Numbers
  1. var1      var2      var3
  2. man       1         a
  3. gt       1         a
  4. cook      1         a
  5.  
but its works like this <space> mean just space
Expand|Select|Wrap|Line Numbers
  1. var1      var2      var3
  2. <space>   1         a
  3. <space>   1         a
  4. cook      1         a
  5.  
last one inserting properly. others not working. help me
Feb 20 '13 #1
2 1993
Rabbit
12,516 Expert Mod 8TB
Can't really help without seeing your code.
Feb 20 '13 #2
//firstly thanks for rabit

//script php

Expand|Select|Wrap|Line Numbers
  1. $words = array('man','pan','can');
  2. foreach($words as $var1){
  3.     var2 = 1;
  4.     var3 = a;
  5.  
  6. $obj = new myclass( array(
  7.       "var1"=> isset( $var1 ) ? (string) $var1 :"",
  8.       "var2"=> isset( $var2 ) ? (string) $var2 :"",
  9.       "var3"=> isset( $var3 ) ? (string) $var3 :"",
  10.             ) );
  11.  
  12.         $obj->insert();
  13. }
  14.  
  15. //class myclass
  16.  
  17. class myclass extends DObject {
  18.   protected $data = array(
  19.     "var1" => "",
  20.     "var2" => "",
  21.     "var3" => ""
  22.   );
  23.  
  24. public function insert() {
  25. $conn = parent::connect();
  26. $sql = "INSERT INTO " . TBL_MYTBL . " (
  27. var1,
  28. var2,
  29. var3
  30. ) VALUES (
  31. :var1,
  32. :var2,
  33. :var3
  34. )";
  35. try {
  36.     $st = $conn-> prepare( $sql );
  37.     $st-> bindValue( ":var1", $this-> data["var1"], PDO::PARAM_STR );
  38.     $st-> bindValue( ":var2", $this-> data["var2"], PDO::PARAM_STR );
  39.     $st-> bindValue( ":var3", $this-> data["var3"], PDO::PARAM_STR );
  40.     $st-> execute();
  41.    parent::disconnect( $conn );
  42. } catch ( PDOException $e ) {
  43. parent::disconnect( $conn );
  44. die( "Query failed: " . $e-> getMessage() );
  45. }
  46. }
  47.  
  48. }
  49.  
  50. //dobject class
  51.  
  52. abstract class DObject {
  53. protected $data = array();
  54.  
  55. public function __construct( $data ) {
  56.   foreach ( $data as $key => $value ) {
  57.   if ( array_key_exists( $key, $this-> data ) ) $this-> data[$key] = $value;
  58.  }
  59. }
  60. protected static function connect() {
  61.  try {
  62.  $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
  63.  $conn-> setAttribute( PDO::ATTR_PERSISTENT, true );
  64.  $conn-> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  65. } catch ( PDOException $e ) {
  66.  die( "Connection failed: " . $e-> getMessage() );
  67. }
  68. return $conn;
  69. }
  70.  
  71. protected static function disconnect( $conn ) {
  72.     $conn = "";
  73. }
  74. }
Feb 20 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
0
by: Duane Winner | last post by:
Hello all - I'm having a small problem with the mysql startup script that ships with MySQL-3.23.56-1. I'm running on RedHat Linux. It works fine, but I have a backup server that runs a script...
0
by: Michael Schoen | last post by:
Hi there, i=B4m actually developing a performance criticial system where I need to index a huge amount of protocoll data. These data are already in a chronological order, so if I push the data...
0
by: gw | last post by:
I am new to MySQL and my problem is with storing a datetime value in a table. I created a table with a column designated as DATETIME. I fill it with the NOW() function. No matter whether I...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
2
by: Asha | last post by:
greetings, i'm doing a multiple insert into the db through a store proc. i'm very sure the sp has nothing wrong. below are my implementation code and error msg. thanks in advance for the help ...
1
by: wesley | last post by:
Can i make multiple inserts into a table using one statement. the statement: INSERT INTO table1 (UserName) VALUES (SELECT DISTINCT UserName FROM table2) Basically, i want to take out all the...
5
by: Brad Baker | last post by:
I'm trying to write a simple asp.net page which updates some data in a SQL database. At the top of the page I have the following code: <%@ Page Language="C#" Debug="true" %> <%@ import...
1
by: clayalphonso | last post by:
Here is the code: <% dim testArray, testArray2 dim Conn, rs dim sSQL, sConnString 'response.write request.form("sel1") 'testArray = split(request.form("sel1"),",") 'for each gidstuff In...
58
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.