Hi,
Below code is regarding to insert an array values into mysql db using php.
i dont know why the code is not inserting array values into database properly.
If is there any thing wrong with the code, Please let me know what is that!.
Here $final is an array containg the values of "ID,Name,Phone" one by one.
array will be like $final=array(id1,name1,ph1,id2,name2,ph2.......... ...)
[PHP]
for($i=0;$i<count($final);$i++)
{
if($i%3==0)
{
$query="insert into cane (ID) values('$final[$i]')";
$v=$final[$i];
}
elseif($i%3==1)
$query="update cane set Name='$final[$i]' where ID='$v'";
else
$query="update cane set phone='$final[$i]' where ID='$v'";
$result=mysql_query($query)or die("Query failed : " . mysql_error());
}
[/PHP] Sanjeev 7 42668
I entered those array values to my table successfully.See the changes and alter it against to your table.
[PHP]<?
include 'dbcon.php';
$final=array(id1,name1,ph1,id2,name2,ph2,id3,name3 ,ph3);
for($i=0;$i<count($final);$i++)
{
if($i%3==0)
{
$query='insert into products (p_id2) values("'.$final[$i].'")';
$v=$final[$i];
}
elseif($i%3==1)
$query='update products set p_name="'.$final[$i].'" where p_id2="'.$v.'"';
else
$query='update products set p_image="'.$final[$i].'" where p_id2="'.$v.'"';
$result=mysql_query($query)or die("Query failed : " . mysql_error());
}
?>[/PHP]
Atli 5,058
Expert 4TB
Hi,
Please follow this site's Posting Guidelines when posting in this forum
That said, the problem might be that the fields 'Name' and 'Phone' do not have a default value, therefore you can not insert into the table without giving the values.
Also, you are manually inserting into a 'ID' field, which I'm assuming is the table's primary key? In which case you can not insert the same value twice in a table.
Besides that, your code is querying the database for each field in each row. This is not a good way to insert data into a database.
A better way would be to insert all your data into a single query.
Consider this alternetive method to your code.
[PHP]
// Set up the array
$final = array(1, "Atli", "5555555", 2, "John", "5885522", 3, "Doe", "6669999");
// Connect to the database
$DB = mysql_connect("localhost", "<user>", "<pw>");
mysql_select_db("myDB", $DB);
// Create the qyery string
$query = "INSERT INTO cane(ID, Name, Phone) VALUES";
// Loop through the array
for($i = 0, $c = count($final); $i < $c; $i += 3) {
// Add the next batch of values to the query string
$query .= "({$final[$i]}, '{$final[$i + 1]}', '{$final[$i + 2]}')";
// Add a comma is this is not the last batch
if($i + 3 < $c) {
$query .= ", ";
}
}
// Execute the query
$RESULT = mysql_query($query, $DB);
// Check the results
if(!!$RESULT) {
echo "Query was successfull";
}
else {
echo "Query failed
<blockquote>". $query ."</blockquote>
<blockquote>". mysql_error($DB) ."</blockquote>";
}
[/PHP]
This method creates a query string with all the data and executes only the one query on the database.
This is how the query would look like: -
INSERT INTO table(ID, Name, Phone) VALUES
-
(1, "Atli", "5555555"),
-
(2, "John", "5885522"),
-
(3, "Doe", "6669999")
-
I hope this helps.
Hi,
Please follow this site's Posting Guidelines when posting in this forum
That said, the problem might be that the fields 'Name' and 'Phone' do not have a default value, therefore you can not insert into the table without giving the values.
Also, you are manually inserting into a 'ID' field, which I'm assuming is the table's primary key? In which case you can not insert the same value twice in a table.
Besides that, your code is querying the database for each field in each row. This is not a good way to insert data into a database.
A better way would be to insert all your data into a single query.
Consider this alternetive method to your code.
[PHP]
// Set up the array
$final = array(1, "Atli", "5555555", 2, "John", "5885522", 3, "Doe", "6669999");
// Connect to the database
$DB = mysql_connect("localhost", "<user>", "<pw>");
mysql_select_db("myDB", $DB);
// Create the qyery string
$query = "INSERT INTO cane(ID, Name, Phone) VALUES";
// Loop through the array
for($i = 0, $c = count($final); $i < $c; $i += 3) {
// Add the next batch of values to the query string
$query .= "({$final[$i]}, '{$final[$i + 1]}', '{$final[$i + 2]}')";
// Add a comma is this is not the last batch
if($i + 3 < $c) {
$query .= ", ";
}
}
// Execute the query
$RESULT = mysql_query($query, $DB);
// Check the results
if(!!$RESULT) {
echo "Query was successfull";
}
else {
echo "Query failed
<blockquote>". $query ."</blockquote>
<blockquote>". mysql_error($DB) ."</blockquote>";
}
[/PHP]
This method creates a query string with all the data and executes only the one query on the database.
This is how the query would look like: -
INSERT INTO table(ID, Name, Phone) VALUES
-
(1, "Atli", "5555555"),
-
(2, "John", "5885522"),
-
(3, "Doe", "6669999")
-
I hope this helps.
Hi,Thanks for ur code ,,Really it is very helpful.All was good,but,Your code is not working when i build an array by splitting a string.n It is working when we define the array in code itself..... Sanjeev
Hi,
Below code is regarding to insert an array values into mysql db using php.
i dont know why the code is not inserting array values into database properly.
If is there any thing wrong with the code, Please let me know what is that!.
Here $final is an array containg the values of "ID,Name,Phone" one by one.
array will be like $final=array(id1,name1,ph1,id2,name2,ph2.......... ...)
Please refer to the last line of your Original post, you didn't mention about splitting the array.
Atli 5,058
Expert 4TB
Hi,Thanks for ur code ,,Really it is very helpful.All was good,but,Your code is not working when i build an array by splitting a string.n It is working when we define the array in code itself..... Sanjeev
Then there must be a problem with the code you use to split your string. If you post the code thats giving you trouble and any error messages we can try to help you spot the problem.
Atli 5,058
Expert 4TB
I have edited the theads title, to better descripe the topic of this thread.
Guidlines on how to correctly name you threads can be found in the Posting Guidlines
MODERATOR
Thanx a lot Atli & Sanjeevcis your codes were very helpful.
thanks again.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Geoff Cox |
last post by:
Hello,
I have a series of pages each of which creates an array of values.
How do I keep all the array values until the last page?
Cheers
Geoff
|
by: DavidB |
last post by:
I am working with a database and I would like to be able to populate
an array at run time and then use the values that were pushed into the
array as criteria for a select query. It seems that this...
|
by: radhak |
last post by:
how to insert array?using php mysql
|
by: idorjee |
last post by:
hello,
i'm trying to write a perl cgi script to insert some values that i get from the my html form. i could manage to get the params from the html and process them (that part is not included in...
|
by: ddtpmyra |
last post by:
I don’t know if I posted my question on the right forum if not my bad, but I’m having trouble how to create a php script that will insert data on mysql
Scenario:
I have 3 checkbox on my form and...
|
by: Geethu03 |
last post by:
Hi
i have a array values in hidden format
<form name="form2" method="post" action="test.php">
<input type="hidden" name="date_val" value="<%= date %>">
<input type="hidden" name="name_val"...
|
by: brianrpsgt1 |
last post by:
I am attempting to insert data from a HTML form using a .psp script.
I can not find how to link the data that is inserted into the form to
the variables in the .psp script to then insert into the...
|
by: cnivas |
last post by:
Good Morning All,
I'm doing a small application using python and MySQL. I'm connecting the database and also creating a table using python application. But, I cannot Insert the values into the...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |