Jump to content

Mysql/PHP Count Insert


Kristian_C

Recommended Posts

Just wondred about a little thing, what is the easyest way to separate id's with comma?Lets say i want to add users to my e-mail notice system. But dont want to do it 15 times, how can i separate the users id with 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15.Anyone that can help?->Kritsian_C.

Link to comment
Share on other sites

$id_list = array();//add your IDs in the list however you want to$id_list[] = 7;$id_list[] = 10;$id_list[] = 1337;if (count($id_list) > 0){  $id_str = implode(",", $id_list);  $sql = "SELECT * FROM table WHERE id IN($id_str)";}

Link to comment
Share on other sites

$id_list = array();//add your IDs in the list however you want to$id_list[] = 7;$id_list[] = 10;$id_list[] = 1337;if (count($id_list) > 0){  $id_str = implode(",", $id_list);  $sql = "SELECT * FROM table WHERE id IN($id_str)";}

Thanks, but how do i do it like :i have a textfield and i write : 1,2,3,4,5,6,7,8,9,10,23 in there. then click add to list should that be $id_list[] = $_POST['id'];or whould it be better for me to make like 5 - 10 extra textfileds that i can use if i want just use js to make it look better for my users. so they write like 12 in a filed then it popsup 12 extra fileds whould i then use $id_list[] = $_POST['id1'];$id_list[] = $_POST['id2'];$id_list[] = $_POST['id3'];$id_list[] = $_POST['id4'];$id_list[] = $_POST['id5'];$id_list[] = $_POST['id6'];$id_list[] = $_POST['id7'];$id_list[] = $_POST['id8'];$id_list[] = $_POST['id9'];$id_list[] = $_POST['id10'];$id_list[] = $_POST['id11'];$id_list[] = $_POST['id12'];but as i am just a neewbie in this world i then whould need to do like :if($extra=="2"){$id_list[] = $_POST['id1'];$id_list[] = $_POST['id2'];$id_list[] = $_POST['id3'];elseif($extra=="3"){$id_list[] = $_POST['id1'];$id_list[] = $_POST['id2'];$id_list[] = $_POST['id3'];$id_list[] = $_POST['id4'];+++++++if you now understand what i meen? whould that be better for me you think? thanks for helping me out all the time guys :)->Kristian_CEdit :wow now i really feel stupid :) dident see the inplode or what you had there.. thanks :)But what do the :$id_list[] = 7;$id_list[] = 10;$id_list[] = 1337;meen? is that like the max thing? ->Kristian_C Edited by Kristian_C
Link to comment
Share on other sites

It was just a few example IDs. It's better to have a field for each one, you don't want to have a form where you just put what they type in into the SQL statement, it would be too easy for someone to get something wrong and mess it up, or if they wanted to they could mess with your database. You'll probably want to do something like this:

$id_list[] = $_POST['id1'];for ($i = 2; $i <= $extra + 1; $i++)  $id_list[] = $_POST['id' . $i];

And then use the implode to put them all together. If all your IDs are numbers, you will want to include a check to make sure people aren't trying to type in words there:

$id_list[] = intval($_POST['id1']);for ($i = 2; $i <= $extra + 1; $i++)  $id_list[] = intval($_POST['id' . $i]);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...