/    Sign up×
Community /Pin to ProfileBookmark

How to Insert Record with multiple rows?

Hello all,

i want to ask something… how to use insert record server behavior – php myysql, to insert multiple rows from textfiled?

thanks..

[img]http://img573.imageshack.us/img573/5616/prinscreen.png[/img]

here the screenshot of page n database

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@BIOSTALLNov 12.2010 — I'm guessing you'll need to do something like this: (untested)

[code=php]if ($_POST) { // has the form been submitted

$insert_values = array();
// add the first entries to the query if they exist
if ($_POST['subjectcode_1']!="" && $_POST['subjectname_1']!="") {
array_push($insert_values, "('".$_POST['username']."', '".$_POST['subjectcode_1']."', '".$_POST['subjectname_1']."')")
}
// add the second entries to the query if they exist
if ($_POST['subjectcode_2']!="" && $_POST['subjectname_2']!="") {
array_push($insert_values, "('".$_POST['username']."', '".$_POST['subjectcode_2']."', '".$_POST['subjectname_2']."')")
}
// add the third entries to the query if they exist
if ($_POST['subjectcode_3']!="" && $_POST['subjectname_3']!="") {
array_push($insert_values, "('".$_POST['username']."', '".$_POST['subjectcode_3']."', '".$_POST['subjectname_3']."')")
}
// add the fourth entries to the query if they exist
if ($_POST['subjectcode_4']!="" && $_POST['subjectname_4']!="") {
array_push($insert_values, "('".$_POST['username']."', '".$_POST['subjectcode_4']."', '".$_POST['subjectname_4']."')")
}
// add the fifth entries to the query if they exist
if ($_POST['subjectcode_5']!="" && $_POST['subjectname_5']!="") {
array_push($insert_values, "('".$_POST['username']."', '".$_POST['subjectcode_5']."', '".$_POST['subjectname_5']."')")
}

// build our query
$query = "INSERT INTO my_table_name (
username, subjectcode, subjectname
) VALUES ".join(",", $insert_values);
$result = mysql_query($query) or die(mysql_error());

}[/code]


Note: the above assumes that your 'Name' field is called username and the subject code and subject names are called 'subjectcode_x' and 'subjectname_x' respectively where x is the row number.

Like I say I haven't tested the above but I hope it points you in the right direction.

BIOSTALL
×

Success!

Help @skyline2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...