Database Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesDatabase Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Laplink Gold 2008 - $15 Off with Coupon Code CJM9NE
  #1  
Old January 19th, 2004, 03:37 PM
charlielha charlielha is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 10 charlielha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Multiple Records insert from Database

Hi,

I am working on a page that needs to insert multiple records when the user checks them.

Explanation:

The user can add more users to his/her account. Then when he goes to the inquiry page, he can choose all the users he want, that he has in the accout:

User 1........ check
User 2........ not check
User 3.......check

In this example, I will need to add User1 Id to the database as well as User 3

I already have a recordset and a repeat region action that works fine

the code for the checkbox is the following

<input name="addUser[]" type="checkbox" id="addUser[]" value="<?php echo $row_rsAuthUsers['USER_ID']; ?>"

This works great, when looking at the source I see each user Id.

The problem is that only the last user checked get insrted in the database.

Thanks for any help.

Reply With Quote
  #2  
Old January 19th, 2004, 07:32 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
try somethign along the lines of:
[note, this is off the top of my head and may contain minor errors]

PHP Code:
foreach ($_GET['check'] AS $value) {
    
$sql "INSERT INTO table (field) VALUES ('$value')";
    
mysql_query ($sql);


Reply With Quote
  #3  
Old January 19th, 2004, 08:32 PM
charlielha charlielha is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 10 charlielha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Didn't Worked

That didn;t worked.

To explain better:

First of all, I added a hidden field next to each checkbox to store the users id.

I need to add all the users that are checked.

I have two other fields that will have the same information for all users, and one that will have their ID.

Codes:

Form:

<table width="100%" border="1" cellspacing="0" cellpadding="3">
<tr class="tdSubTitles">
<td width="295">User</td>
<td width="232"> <p>Type</p></td>
<td width="217">Add</td>
</tr>
<?php do { ?>
<tr class="tdContentLast">
<td><?php echo $row_rsAuthUsers['FNAME']; ?> <?php echo $row_rsAuthUsers['LNAME']; ?></td>
<td>
<?php $type = $row_rsAuthUsers['TYPE'];?>
<?php if ($type == "3")
echo "Witness" ?>
<?php if ($type == "4")
echo "Editor" ?>
</td>
<td><input name="addUser[]" type="checkbox" id="addUser[]" value="Yes">
<input name="authId" type="hidden" id="authId" value="<?php echo $row_rsAuthUsers['USER_ID']; ?>"> </td>
</tr>
<?php } while ($row_rsAuthUsers = mysql_fetch_assoc($rsAuthUsers)); ?>
</table>

PHP:

if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "INQUIRY")) {
for($i=1; $<=sizeof($addUser[]); ; $i++){
$insertSQL = sprintf("INSERT INTO authUser (INQUIRY_ID, COMP_ID, USER_ID) VALUES (%s, %s, %s)",
GetSQLValueString($HTTP_POST_VARS['INQUIRY_ID'], "text"),
GetSQLValueString($HTTP_POST_VARS['COMP_ID'], "text"),
GetSQLValueString($HTTP_POST_VARS['authId'], "text"));

mysql_select_db($database_inquiries, $inquiries);
$Result1 = mysql_query($insertSQL, $inquiries) or die(mysql_error());
}
}



Thanks again for any help in this matter

Reply With Quote
  #4  
Old January 19th, 2004, 08:45 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
For the form, couldn't you do something like this?
PHP Code:
<input name="addUser[]" type="checkbox" id="addUser[]" value="<?php echo $row_rsAuthUsers['USER_ID']; ?>"


Then simply handle it in the php as follows:
[loosely based on my previous example, and your code]
PHP Code:
 mysql_select_db($database_inquiries$inquiries);
foreach (
$HTTP_POST_VARS['addUser'] AS $value) { 
    
$sql "INSERT INTO authUser (INQUIRY_ID, COMP_ID, USER_ID) VALUES (".$HTTP_POST_VARS['INQUIRY_ID'].", ".$HTTP_POST_VARS['COMP_ID'].", ".$HTTP_POST_VARS['authId'].")";
    
mysql_query ($sql) or die(mysql_error()); 



Where's INQUIRY_ID and COMP_ID coming from? I don't see that in your form anywhere... similarly, what does GetSQLValueString() do?

Reply With Quote
  #5  
Old January 19th, 2004, 08:52 PM
charlielha charlielha is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 10 charlielha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Inquiry_id and comp_id are in the table, just not posted here. They get their values the same way as the user id does, only it is the same value for all of them

I triewd the script, It lloks like it is set to work but it is not working, now:

Do I have to have my php code in a separte page instead of the same page as the form?

Reply With Quote
  #6  
Old January 19th, 2004, 09:09 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
I don't think i properly understand your question...

The form code (the <input> box) would change on the page you have the form... the other code I posted (the foreach) would be in the page that the form is posted to...

Reply With Quote
  #7  
Old January 19th, 2004, 10:11 PM
charlielha charlielha is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 10 charlielha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
MadCowDzz, Thanks for your help.

I am going to paste the complete finished code so you can look at it and identify my problem.

Form Page:

<form action="usersForInquiry.php" method="post" name="addSuplier">
<table width="700" border="0" cellpadding="3">
<tr>
<td class="titles">Add Suppliers to this Inquiry: <?php echo $row_rsInquiry['PROJECT_NAME']; ?> Inquiry#: <?php echo $row_rsInquiry['INQUIRY_ID']; ?></td>
</tr>
<tr>
<td><a href="#" onClick="MM_openBrWindow('addSuppliersPop.php?USER_ID=<?php echo $row_rsUSer['USER_ID']; ?>&COMP_ID=<?php echo $row_rsUSer['COMP_ID']; ?>','main','width=500,height=400')">Add
More Suppliers</a></td>
</tr>
<tr>
<td class="tableBorderGen"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="145" class="tdSubTitles">Company Name</td>
<td width="150" class="tdSubTitles">Main Activity</td>
<td width="221" class="tdSubTitles">Contact Name</td>
<td width="115" class="tdSubTitles">Email</td>
<td width="242" class="tdSubTitles">Add<a href="#" class="link2" onClick="MM_openBrWindow('what.php?myId=activeSupplier','wh at','scrollbars=yes,width=300,height=400')"></a></td>
</tr>
<?php do { ?>
<tr>
<td class="tdContent"><?php echo $row_rsSupplier['COMP_NAME']; ?></td>
<td class="tdContent">&nbsp;<?php echo $row_rsSupplier['ACTIVITY']; ?> </td>
<td class="tdContent"><?php echo $row_rsSupplier['FNAME']; ?> <?php echo $row_rsSupplier['LNAME']; ?></td>
<td class="tdContent"><?php echo $row_rsSupplier['EMAIL']; ?></td>
<td class="tdContent">
<input name="addSupplier[]" type="checkbox" id="addSupplier[]" value="<?php echo $row_rsSupplier['USER_ID']?>">
<input name="INQUIRY_ID" type="hidden" id="INQUIRY_ID" value="<?php echo $HTTP_GET_VARS['INQUIRY_ID']?>">
<input name="USER_ID2" type="hidden" id="COMP_ID" value="<?php echo $HTTP_GET_VARS['COMP_ID']?>"></td>
</tr>
<?php } while ($row_rsSupplier = mysql_fetch_assoc($rsSupplier)); ?>
</table></td>
</tr>
<tr>
<td><hr></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="ADD SUPPLIERS" class="btn"></td>
</tr>
</table>
</form>



usersForInquiry.php:

<?php
mysql_select_db($database_inquiries, $inquiries);
foreach ($HTTP_GET_VARS['addSupplier'] AS $value) {
$sql = "INSERT INTO supplier (INQUIRY_ID, COMP_ID, USER_ID) VALUES (".$HTTP_GET_VARS['INQUIRY_ID'].", ".$HTTP_GET_VARS['COMP_ID'].", ".$HTTP_GET_VARS['addSupplier'].")";
mysql_query ($sql) or die(mysql_error());
}

?>

Thanks again for your help. I know we are close to making it happen

Reply With Quote
  #8  
Old January 19th, 2004, 10:49 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
What does happen exactly?
Are you getting an error? (if so, is it php, or mysql related?)

[i've lost track]

Reply With Quote
  #9  
Old January 19th, 2004, 10:54 PM
charlielha charlielha is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 10 charlielha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I don't get an error, but the data is not inserted in the table.

Reply With Quote
  #10  
Old January 21st, 2004, 02:38 PM
charlielha charlielha is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 10 charlielha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Almost there

The form is about to work, the only problem I am getting now is this:

Unknown column '2cqarhf6p56n8vew' in 'field list'

that number is the first useer ID. If I uncheck the firtst user, I'll get the same error but with the Second User Id and so on.


I also believe that I need a statment in case none of the users is checked, or when only one is checked.

Please help me finish this, Thanks

Reply With Quote
  #11  
Old January 21st, 2004, 06:14 PM
cbunting cbunting is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 19 cbunting User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hello,
I really know nothing about php or mysql. Only remember stuff that I have read on the forums and tutorials but, from reading your post about inserting multiple records into a field or column, Wouldn't you do this as an array or with using ENUM or SET in the database?

I am only posting because nothing here mentions how your db is setup or if you are using arrays to either add to or select from the addUser field.

I'm confused but maybe this will give you an idea or something.
Chris

Here is a link to a solution with a simular issue. Hope this helps.
http://www.webmasterworld.com/forum88/2565.htm

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesDatabase Development > Multiple Records insert from Database


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |