By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

pls can any one show me how i can add more array here

P: 7
already i've an array id, i want to add addional into the row.
pls help!
[PHP]$query = 'INSERT INTO related_articles (id, referer) VALUES (' .
implode(", $ref), (", $_POST['id']) . ", $ref)";
$result = mysql_query($query)
or die('Could not execute INSERT query');[/PHP]
Oct 19 '08 #1
Share this Question
Share on Google+
10 Replies


Atli
Expert 5K+
P: 5,058
Hi.

What errors are you getting?
How does the query look like when you execute it? (Try echoing it before you call mysql_query)
Oct 19 '08 #2

P: 7
Thank you.
with this code, it works ok.
[PHP]ref = $_POST['referer'];

$insertSQL = 'INSERT INTO aco (pin, test) VALUES (' .
implode(", $ref), (", $_POST['id']) . ", $ref)";




mysql_select_db($database_schools, $schools);
$Result1 = mysql_query($insertSQL, $schools) or die(mysql_error());
if (!$Result1) {
echo 'Sorry';
}else {
echo 'Thank you';} [/PHP]

but if i should add any additional array say;


[PHP]ref = $_POST['referer'];

$insertSQL = 'INSERT INTO aco (pin, test, adm) VALUES (' .
implode(", $ref), (", $_POST['id'], $_POST['class']) . ", $ref)";




mysql_select_db($database_schools, $schools);
$Result1 = mysql_query($insertSQL, $schools) or die(mysql_error());
if (!$Result1) {
echo 'Sorry';
}else {
echo 'Thank you';} [/PHP]

it say:
Warning: Wrong parameter count for implode() in c:\inetpub\wwwroot\phcn\test\test5.php on line 8
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 123)' at line 1
Oct 19 '08 #3

Atli
Expert 5K+
P: 5,058
That would be because implode only takes two parameters. The string you want to use to glue each element together, and a array to be imploded.

If you need to add a second array, you can't just throw it in there as a third parameter.
You are going to have to merge the two arrays using array_merge first, and use the result from that as the array in you implode call.

Like, for example:
Expand|Select|Wrap|Line Numbers
  1. echo implode(", ", array_merge($array1, $array2));
  2.  
P.S.
Please use [code] tags when posting your code examples. (See How to ask a question)

[code=php] ...PHP code goes here... [/code]

Thank you.
Moderator
Oct 19 '08 #4

P: 7
Once more, thank you. i'll work on it and get back to you.
You're trull helpful.
Oct 19 '08 #5

P: 7
I've tried over the night but cant reach further, pls, what i need to understan is this;

Expand|Select|Wrap|Line Numbers
  1. $ref = $_POST['referer'];
  2. $query = 'INSERT INTO related_articles (id, referer) VALUES (' .
  3. implode(", $ref), (", $_POST['id']) . ", $ref)";
  4. $result = mysql_query($query)
  5. or die('Could not execute INSERT query');
  6.  
  7. \\Here's the query that is going to be executed:
  8.  
  9. INSERT INTO related_articles (id, referer)
  10. VALUES (32, 123), (59, 123);
  11.  
  12.  
the code above i got from the forum, it says;

let's assume the user checked articles #32 and #59. These
values will be available to insert.php as fields in $_POST['id'], which

in this case will be an array. So in insert.php you can write:

Here's the query that is going to be executed:

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO related_articles (id, referer)
  2. VALUES (32, 123), (59, 123);
This query will create two records in related_articles, one linking
article #32 to article #123, the other linking article #59 to article
#123.

Mine, what i need to understand is how to add additional array say;

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO related_articles (id, referer)
  2. VALUES (32, 35, 123), (59, 34, 123);
if say one linking article #32, #35 to article #123 in that order.
turlly appreciating your help
Oct 20 '08 #6

Atli
Expert 5K+
P: 5,058
I've tried over the night but cant reach further, pls, what i need to understan is this;
[...]
Mine, what i need to understand is how to add additional array say;

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO related_articles (id, referer)
  2. VALUES (32, 35, 123), (59, 34, 123);
if say one linking article #32, #35 to article #123 in that order.
turlly appreciating your help
This is an invalid query, obviously. The number of values per row don't match the columns they are meant to be inserted into.

I don't really understand what you are asking.
Are you simply trying to sort the array before using it, so the integers get inserted sequentially, lowest to highest?

If so, you could try the sort function.
It can be a little confusing, because it alters the input data rather then return the new array.
You can use it like so:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $arr1 = array(1, 3);
  3. $arr2 = array(2, 4);
  4. $merged = array_merge($arr1, $arr2);
  5.  
  6. // $merged is now (1, 3, 2, 4)
  7.  
  8. sort($merged);
  9.  
  10. // $merged is now (1, 2, 3, 4)
  11.  
  12. ?>
  13.  
If this isn't what you are looking for, please try to explain what it is you are after a bit clearer.
Oct 20 '08 #7

P: 7
I will stop say thank you, b'cos i'm really learning more, so, thank you again.
let me explain what i want to do.
i've a form which post to a php file which will handle the insert process. the form is as below

Expand|Select|Wrap|Line Numbers
  1. <form method="POST" action="insert.php">
  2. <input type="checkbox" name="id[]" value="32">Article #32<br>
  3. <input type="checkbox" name="id[]" value="38">Article #38<br>
  4. <input type="checkbox" name="id[]" value="45">Article #45<br>
  5. <input type="checkbox" name="id[]" value="59">Article #59<br>
  6. <input type="hidden" name="referer" value="123">
  7. <input type="text" name="class[]"  value="3" />
  8. <input type="text" name="class[]"  value="4" />
  9. <input type="text" name="class[]"  value="6" />
  10. <input type="text" name="class[]"  value="7" />
  11. <!-- This is the ID of the "referring" article -->
  12. <input type="Submit">
  13. </form>
  14.  
now the code below handles the processs
Expand|Select|Wrap|Line Numbers
  1. $ref = $_POST['referer'];
  2. $query = 'INSERT INTO related_articles (id, referer) VALUES (' .
  3. implode(", $ref), (", $_POST['id']) . ", $ref)";
  4. $result = mysql_query($query)
  5. or die('Could not execute INSERT query');
  6.  
  7.  
this codes does it perfectly since the other array of calss is not included in the insert line.
the code above executed the following

INSERT INTO related_articles (id, referer)
VALUES (32, 123), (59, 123);

now what i want to do is to add the class array in the same row to have the code do the following when
executed;


INSERT INTO related_articles (id, code, referer)
VALUES (32, 6, 123), (59, 4, 123);

i hope this will enable you help me. thanks
Oct 20 '08 #8

Atli
Expert 5K+
P: 5,058
Ahh ok. Then it gets a little bit more complicated.

You will somehow have to associate a specific 'class' to a specific 'id' in your Form, which is hard to do with the dynamically created arrays you are currently using.

One thing tho, do you want each article ID to be associated with a specific class number, or do you want to be able to select a class for each checked article ID?

That is, will article #32 always be associated with class number 6, or do you want to give the user the ability to choose a class number for it?
Oct 21 '08 #9

P: 7
Atli, thank you very mucg for your effort. you made me read well.
i finaly got my solution. here it is for ref;
Expand|Select|Wrap|Line Numbers
  1. foreach ($_POST['code'] as $row=>$code)  
  2. {
  3.  
  4. $username = mysql_real_escape_string($code); 
  5. $op = mysql_real_escape_string($_POST['op'][$row]); 
  6. $cl = mysql_real_escape_string($_POST['cl'][$row]); 
  7. $time = ($_POST['time']); 
  8.  
  9. $insertSQL =('INSERT INTO `trans` (code, op, cl, date) VALUES("' . $username . '", "' . $op . '", "' . $cl . '", "' . $time . '")') ; 
  10.  
  11.  
  12.   mysql_select_db($database_drin, $drin);
  13.   $Result1 = mysql_query($insertSQL, $drin) or die(mysql_error());
  14.  
Oct 28 '08 #10

Atli
Expert 5K+
P: 5,058
I'm glad you found a solution.
Thanks for sharing it with us.
Oct 28 '08 #11

Post your reply

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