Solved

Inserting values in mysql database

Posted on 2012-12-21
7
463 Views
Last Modified: 2012-12-22
Hello Experts,

I would like to insert the values from the PHP page into MYSQL database where the values were in the form of ...

<?php 
 for ($i = 0; $i < count($_POST['chkbx']); $i++) {
            $chkd = $_POST['chkbx'][$i];
            $filename = $_POST['myfilename'][$i];
            $src = $_POST['mysource'][$i];
            $tgt = $_POST['mytarget'][$i];
            
}
?>

Open in new window


For example, there are 5 rows on php and user has selected any of the rows...that row data from php needs to inserted into mysql database that many rows.

Can you suggest the query for this or any references?

Thanks,
Shail
0
Comment
Question by:ShaileshShinde
  • 3
  • 3
7 Comments
 
LVL 17

Expert Comment

by:Kent Dyer
ID: 38715117
You mean something like..
<?php
   $conn = mysql_connect ("localhost","login","password");
   $db = mysql_select_db('database',$conn);

$LIBRARY_NO=$_POST['LIBRARY_NO'];
$TITLE=mysql_real_escape_string($_POST['TITLE']);
$LYRICIST=$_POST['LYRICIST'];
$VOICING=$_POST['VOICING'];
$NO_OF_COPIES=$_POST['NO_OF_COPIES'];

// Update the database if form posted 
if ($_POST['cmdadd'] != null)
{ 
$sql = "INSERT INTO Music(";
if ($LIBRARY_NO != null) {$sql=$sql."`LIBRARY_NO`";}
if ($TITLE != null) {$sql=$sql.", `TITLE`";}
if ($LYRICIST != null) {$sql=$sql.", `LYRICIST`";}
if ($VOICING != null) {$sql=$sql.", `VOICING`";}
if ($NO_OF_COPIES != null) {$sql=$sql.", `NO_OF_COPIES`";}
$sql=$sql.")"; 
//echo $sql;
$result = mysql_query($sql) or die("SQL Update failed"); 
  if ( ! $result )
    die ("Couldn't update: ".mysql_error());
  print "<h1>Table updated ". mysql_affected_rows() . 
  " row(s) changed</h1><p>";
}
?>

Open in new window

HTH,

Kent
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38715138
<?php 
 for ($i = 0; $i < count($_POST['chkbx']); $i++) {
            $chkd = $_POST['chkbx'][$i];
            $filename = mysql_real_escape_string($_POST['myfilename'][$i]);
            $src = mysql_real_escape_string($_POST['mysource'][$i]);
            $tgt = mysql_real_escape_string($_POST['mytarget'][$i]);
            $query = "INSERT INTO table (filename, src, tgt) VALUES ('$filename', '$src', '$tgt')";
            //execution code here 
}
?>

Open in new window


then execute the query with mysql, mysqli or PDO. Keep in mind that now the use of mysql is discouraged and it is recommended the use of mysqli or PDO

If you want, you can post the markup of the form so I can be a bit more precise.

Cheers
0
 
LVL 1

Author Comment

by:ShaileshShinde
ID: 38715149
Hello Expert,

The markup is something like this...

echo "<tr class=\"d" . ($i & 1) . "\">
                        <td><input class=textboxa type=text style=\"font-style: Italic;font-weight: bold;\" value=\"$i\" /><br/></td>
                        <td><input name=test_$i class=textboxc type=text style=\"font-style: Italic;font-weight: bold;\" value=\"" . $row['Delivery'] . "\" title=Delivery /><br/></td>
                        <td><input name=subtest_$i class=textboxc type=text style=\"font-style: Italic;font-weight: bold;\" value=\"" . $row['quality'] . "\" title=quality /><br/></td></tr>";

Open in new window

0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38715182
I don't see checkboxes: how user selects rows? In your first snipet you used $_POST['chkbx'] so I presumed you had a checkbox per row in your form...
0
 
LVL 1

Author Comment

by:ShaileshShinde
ID: 38715200
Hello expert,

Yes, there is an checkbox each row...

<td><input name=chk[] type=checkbox value=$i /><br/></td>

Open in new window

0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 100 total points
ID: 38715221
Well, then you have to use 'chk' instead of 'chkbx':

<?php 
 for ($i = 0; $i < count($_POST['chk']); $i++) {
            $chkd = $_POST['chkbx'][$i];
            $filename = mysql_real_escape_string($_POST['myfilename'][$i]);
            $src = mysql_real_escape_string($_POST['mysource'][$i]);
            $tgt = mysql_real_escape_string($_POST['mytarget'][$i]);
            $query = "INSERT INTO table (filename, src, tgt) VALUES ('$filename', '$src', '$tgt')";
            mysql_query($query) or die(mysql_error()); 
}
?>

Open in new window

0
 
LVL 1

Author Closing Comment

by:ShaileshShinde
ID: 38715445
Thanks Experts!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now