problem in inserting data in Database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

problem in inserting data in Database

Post by eshban »

Hi,

I am facing a little problem, plz help.
i am posting an array from a page, and all my array values are stored in $final variable(see below code). I confirm this by print_r command.

In this variable, i got 5 different ID's, i want to store that id's in database, thats why i put my query in loop.

But the problem i am facing is that i don't know that how to insert $fianl variable value in database. Also it gives error in query.

Please correct this query syntax, so that this code will insert ID's (get from $final variable) in database. Here is a little code

Code: Select all

$final= unserialize(base64_decode($_POST['allsizes']));   

for($c=0;$c<=count($final);$c++){

mysql_query("insert into travelagent_history (requestid,userid_travelagent)   
values(' ','$final[$c]')",$db)or die(mysql_error());

}
Hoping for a positive response
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

What you are trying to do is this:

Code: Select all

for($i=0,$j=count($final);$i<$j;$i++) {
    $q= 'INSERT INTO travelagent_history (requestid, userid_travelagent) VALUES (null,' . $final[$c] . ')';
    mysql_query($q) or die(mysql_error() . $q);
}
Note the improvements:
  • for uses variable $i: convention short for index
  • $j assigned the count of $final as part of the first portion of the for loop, this means that the array is counted only once at the start of the loop rather than every iteration.
  • $i<$j not $i<=$j, otherwise you miss out the last element
  • Query is assigned to variable before being used in mysql_query, this allows you to process/output it later should you need to
  • SQL uses block capitals for keywords, makes it a lot easier to read.
  • SQL is created using single quotes, learn the difference between double and single quoted strings
  • I've changed "...es(' ','$fi..." to "...es(null ,'$fi..." because I couldn't possibly see why you would want to insert a space as a value into your database.
  • the value of $final[$c] is concatinated into the query not the name "$final[$c]" itself
  • Should the query fail the query text itself is outputted. During development this is very useful but once in production is a security hazard, so you should remove it later.
What you are actually doing though is issue a whole separate query for each element of the array when you can actually insert the entire array with a single query:

Code: Select all

$q = 'INSERT INTO travelagent_history (userid_travelagent) VALUES (' . implode(',', $final) . ')';
mysql_query($q) or die(mysql_error() . $q);
In this example I have completely remove the requestid portion, again because I couldn't see why you were inserting a space.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Sorry second code block should be this:

Code: Select all

$q = 'INSERT INTO travelagent_history (userid_travelagent) VALUES (' . implode('),(', $final) . ')';
mysql_query($q) or die(mysql_error() . $q);
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Post by eshban »

this code cannot runs, means the query get not get the 'ID'. when i print the query it just have a NULL values

any solution
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

any solution
Modify it to suit your needs. If you can't get it to work post your best attempt here.
Post Reply