Current location: Hot Scripts Forums » Programming Languages » PHP » help, =( issues on foreach and insert


help, =( issues on foreach and insert

Reply
  #1 (permalink)  
Old 03-07-10, 09:59 PM
nadiakasnan nadiakasnan is offline
New Member
 
Join Date: Mar 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation help, =( issues on foreach and insert

Hi,

I've trying this for more than a week, so, i hope you guys can share with me if you know something.
I have an X value for a textfield which need it to be saved in the database.

the textfield :
<input name="name[ ]" type="text" id="name1" size="40"/>
*the text field can be add up according to the users

then i post it to another page called reg_post.php
$s = $_POST['name'];[/PHP]f[PHP]oreach ($s as $testing) { ?>
<?
echo '<table border=1><tr><td>';
echo $testing;
echo '</td></tr></table>';
$session = session_id();
$sql="INSERT INTO part (sid,name) VALUES ('$session','$testing')";
if (!mysql_query($sql,$conn)) { die('Error: ' . mysql_error());} else { echo ""; } }

}
if (mysql_query($sql,$conn))
{
include ("reg/reg_p.php");}

else {echo $row_sub_menu['content']; }


However, when i put 3 values at the textfiled which are;
value 1 = a
value 2 = b
value 3 = c

when its inserted into the database, the record have extra data which become;
value 1 = a
value 2 = b
value 3 = c
value 4 = c

looking forward to get to know from you guys.
thanks in advanced
Reply With Quote
  #2 (permalink)  
Old 03-08-10, 06:33 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
The second if is adding the last value a second time.
Reply With Quote
  #3 (permalink)  
Old 03-08-10, 10:51 AM
CodeRobber's Avatar
CodeRobber CodeRobber is offline
Newbie Coder
 
Join Date: Feb 2010
Location: Leicestershire UK
Posts: 19
Thanks: 0
Thanked 2 Times in 2 Posts
wirehopper is right - this last line is performing the query again

if (mysql_query($sql,$conn))
{
include ("reg/reg_p.php");}

else {echo $row_sub_menu['content']; }

If you wanted to check it like that - you should maybe store the result in a variable rather than straight-check for a query.

eg.

$sql = "INSERT INTO part (sid,name) VALUES ('$session','$testing')";
$res = mysql_query($sql)or die(mysql_error());

if($res){

} else {

}

or...

if(!$res){

- the different is - you are perfoming the query only once - and then comparing the result multiple times.
Reply With Quote
Reply

Bookmarks

Tags
foreach, insert, loop


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
insert $_POST and name Hamed PHP 4 07-24-08 05:40 AM
foreach function acting up soloWebDev PHP 5 08-07-07 10:20 AM
FOreach loop and SQL PainFades PHP 1 09-26-06 11:29 AM


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