Current location: Hot Scripts Forums » Programming Languages » PHP » inserting multiple rows into mysql !


inserting multiple rows into mysql !

Reply
  #1 (permalink)  
Old 04-26-06, 06:54 AM
n3wb!e's Avatar
n3wb!e n3wb!e is offline
Wannabe Coder
 
Join Date: Mar 2006
Posts: 216
Thanks: 2
Thanked 0 Times in 0 Posts
Unhappy inserting multiple rows into mysql !

hi everyone, lemme tell my problem. i am doing a website which has a job search option. when a registered user enters the keyword and clicks on search(button), it should retrieve all the rows in job_posting table which has that keyword.(select * from job_posting where keyword='$keyword'. for every results displayed,there r 3 radio buttons (one radiobutton is for 'apply_for_the_job', second is for 'review' and the third is for 'not_interested')... i m using a radio button array. and lastly theres a submit button.

my question is, when i click on the submit button, i want to insert only those records whose radio button value is 'apply_for_the_job'. if there are 15 jobs listed, and the user selects 8 of those listed jobs, i want to insert only those 8 selected jobs which user have marked 'apply_for_the_job'.
i m confused. can anyone help me ?

lemme generalize the problem. is there any possible way to insert multiple rows into a table depending upon the selected radio button value ? how do i determine which radio button(from radio button array) is clicked ?
Reply With Quote
  #2 (permalink)  
Old 04-26-06, 07:02 AM
n3wb!e's Avatar
n3wb!e n3wb!e is offline
Wannabe Coder
 
Join Date: Mar 2006
Posts: 216
Thanks: 2
Thanked 0 Times in 0 Posts
and yup, thanks in advance.
Reply With Quote
  #3 (permalink)  
Old 04-26-06, 08:26 AM
Crassius Crassius is offline
Newbie Coder
 
Join Date: Mar 2006
Location: Melbourne, FL
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
I suggest using checkboxes instead of radio boxes, since radiobuttons only allow you to choose one at a time.
Create a variable column in your db, so for each job posting you'll have a number, put that number as the value in the checkboxes, then use that to keep track of what the user chooses.
__________________
~With great power comes great responsibility.~
Reply With Quote
  #4 (permalink)  
Old 04-26-06, 12:16 PM
Wuiqed Wuiqed is offline
Wannabe Coder
 
Join Date: Aug 2004
Location: Sverige
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
Crassius, so you think that people should be allowed to select both Not interested and Apply for the job? That doesn't make any sense

Anyway, to execute several statement, just separate them with a comma, like this:
INSERT INTO user VALUES('value1', 'value2'); INSERT INTO user VALUES('value3', 'value4')
Reply With Quote
  #5 (permalink)  
Old 04-26-06, 01:00 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
From a coding standpoint, it will be easier to put this into a loop as the number of rows to be inserted can vary by the number of jobs returned by the search.

If you want help with the code to form the INSERT query's, you will need to post the code you have now, showing the data for the jobs and the radio buttons and provide information/names for what you want inserted...
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #6 (permalink)  
Old 04-26-06, 05:59 PM
jfulton's Avatar
jfulton jfulton is offline
Community VIP
 
Join Date: Apr 2006
Location: Los Angeles, CA
Posts: 660
Thanks: 0
Thanked 0 Times in 0 Posts
Do you really need the 3 radio options? It would make life a lot easier if you didn't.

The most elegant way to do these types of things (IMHO) is to use checkboxes. You could have a checkbox next to each job; you give each checkbox the same name (ie. "jobid[]") and give each checkbox value the id of the job it is for. (You can't do this with radio buttons, because they are grouped by their "names". So it makes processing 'dynamic' radio buttons difficult.)

When you are processing the form in your php code, all of your job id's will be in the array $_POST['jobid'] (or $_GET[]). Then you can use implode() to simplify your query building.

Code:
<form action="process.php" method="post">
<input type="checkbox" name="jobid[]" value="2" />
...
<input type="checkbox" name="jobid[]" value="5" />
...
PHP Code:

//process.php

$query "INSERT INTO table ('col1') VALUES (" implode(","$_POST['jobid']) .")"
Hope that helps. If I need to explain it better, let me know.
Reply With Quote
  #7 (permalink)  
Old 04-27-06, 02:47 AM
n3wb!e's Avatar
n3wb!e n3wb!e is offline
Wannabe Coder
 
Join Date: Mar 2006
Posts: 216
Thanks: 2
Thanked 0 Times in 0 Posts
heres the code,

first of all, thanks alot for ur responses...
this is searchjob.php
---------------------------------------------------------------
<? ob_start(); ?>
<html>
<form method="POST" action="search.php">
<body bgproperties="fixed" background="c4.bmp">
<?php
$user=$_POST[user];
$conn=mysql_connect("localhost","username","passwo rd");
mysql_select_db("database",$conn);
$result = mysql_query("SELECT * FROM posting where job_keywords='$keyword'", $conn) or die("error querying database");\\posting is the table where the list of jobs are stored.
?>
<table border="2" cellspacing="1" width="100%">
<tr>
<th>Position</th>
<th>Job Responsibility</th>
<th>Job Level</th>
<th>Job location</th>
<th>Functional area</th>
<th>Salary</th>
<th>Apply</th>
<th>Review</th>
<th>Not Interested</th>
</tr>
<?
$i = 0;

while($result_ar = mysql_fetch_assoc($result)){
?>
<input="text" name="post_id[<? echo $i?>]" value="<?php echo $result_ar['post_id'];?>">
<tr>
<td><?php echo $result_ar['position']; ?></td> \\columns of table 'posting'
<td><?php echo $result_ar['job_responsibility'];?></td>
<td><?php echo $result_ar['job_level'];?></td>
<td><?php echo $result_ar['location_of_job'];?></td>
<td><?php echo $result_ar['job_functional_area'];?></td>
<td><?php echo $result_ar['job_salary'];?><br></td>
<td><input type="radio" value="apply" name="choice[<?echo $i?>]"></td>
<td><input type="radio" value="review" name="choice[<?echo $i?>]"></td>
<td><input type="radio" value="not_interested" name="choice[<?echo $i?>]"></td>
</tr>
<?php
$i+=1;
}
?>
</table>
</br></br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input type="hidden" value="<?echo $user?>" name="user">
<input type="hidden" value="<?echo $i?>" name="i">
<input type="Submit" value="Submit" name="Submit">
</form>
</html>
</body>
<? ob_end_flush(); ?>
--------------------------------------------------------------------
The reason i m using radiobuttons instead of checkboxes is, i dont want the user to select both 'not interested' checkbox and 'apply' checkbox. i have not written the 'search.php' script yet. because i m not sure how to insert the values to the table. table structure is as follows:
table name: search
username,post_id,choice,data_applied.

i want to insert only those records whose values is 'apply' . i m confused. how do i know if the button which is selected is 'apply'(either radio/checkbox). if the searchjob.php return list of 100 jobs and user "apply" for 80 jobs and click on submit, i want to insert those 80 jobs where the user has selected 'apply'. i m so **** confused with this.

Last edited by n3wb!e; 04-27-06 at 02:50 AM.
Reply With Quote
  #8 (permalink)  
Old 04-27-06, 04:01 PM
jfulton's Avatar
jfulton jfulton is offline
Community VIP
 
Join Date: Apr 2006
Location: Los Angeles, CA
Posts: 660
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by n3wb!e
The reason i m using radiobuttons instead of checkboxes is, i dont want the user to select both 'not interested' checkbox and 'apply' checkbox.
...
i want to insert only those records whose values is 'apply' . i m confused. how do i know if the button which is selected is 'apply'(either radio/checkbox).
If "not interested" and "apply" are dependent on each other and "review" is independent, then you most certainly can (and should) use checkboxes. (ie. can I apply and review? Or be disinterested but still review?) Then, if a user checks "apply" you'll know that they want to apply. If they don't check apply, then you know that they are "not interested".

Using checkboxes, only selected values will be available in the processing page. Using radio buttons, you'll have to iterate through them and check the value of each. To see all of the values passed through on your form, on your "search.php" page, use print_r($_POST);.

(BTW: I think some of my syntax is off on my previous post, but it's just a guideline.)
Reply With Quote
  #9 (permalink)  
Old 04-27-06, 11:36 PM
n3wb!e's Avatar
n3wb!e n3wb!e is offline
Wannabe Coder
 
Join Date: Mar 2006
Posts: 216
Thanks: 2
Thanked 0 Times in 0 Posts
hi, first of all, none of those radio buttons are dependent on each other. each one of them r independent. if the user selects 'apply' or 'not-interested' or 'review', their respective radiobutton value shud be inserted into the table. consider this example, the user has selected 'apply' for 3 jobs, 'not-interested' for 2. when the user logins after a day or two, he wudnt be interested to see the job that he has marked 'not-interested'. as in this example. A user(say, a software engineer) logs in and make a job search with keyword 'manager'. it returns a list of jobs with the keyword manager. it can be,
1. company manager
2. pizza hut manager
3. industry manager ....
so, its obvious that the user(software engineer, in this case) wudnt be interested to work as a manager for pizza hut or a steel industry.. he wud select 'not-interested' for those 2 records and select 'apply' for the company manager post. those values r stored in a user_search_job table with the value he has selected(apply, not-interested). so if the user come back after a day or two and make a search for manager post again, those records which he had marked 'not-interested' shudnt show up..

phew, i know its quite complicated. but my boss want me to do this kinda search !

thanks.
Reply With Quote
  #10 (permalink)  
Old 04-27-06, 11:43 PM
n3wb!e's Avatar
n3wb!e n3wb!e is offline
Wannabe Coder
 
Join Date: Mar 2006
Posts: 216
Thanks: 2
Thanked 0 Times in 0 Posts
thanks dude... print_r($_POST); was of great help.. but my problem remain unsolved..
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
UPDATE mysql from multiple POST rows wwanthony PHP 10 01-27-06 04:39 AM
MySQL inserting 2 rows! perleo PHP 1 05-17-05 02:33 PM
MySQL via ODBC won't advance rows Ryan F PHP 0 02-16-05 07:54 PM
Multiple form fields saved in one mysql field?? cebuy PHP 4 10-15-04 12:08 PM
can't edit multiple rows at the same time conundrum PHP 2 04-01-04 12:50 AM


All times are GMT -5. The time now is 07:11 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.