Link to home
Start Free TrialLog in
Avatar of ShaileshShinde
ShaileshShindeFlag for India

asked on

Inserting values in mysql database

Hello Experts,

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

 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?

Avatar of Kent Dyer
Kent Dyer
Flag of United States of America image

You mean something like..
   $conn = mysql_connect ("localhost","login","password");
   $db = mysql_select_db('database',$conn);


// 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`";}
//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


 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.

Avatar of ShaileshShinde


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

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...
Hello expert,

Yes, there is an checkbox each row...

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

Open in new window

Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Experts!