0

In my table each and every row has a cell with a submit button.

Here is my code

    <?php

 # Init the MySQL Connection
  mysql_connect("localhost", "root", "") or die(mysql_error()) ; 
 mysql_select_db("selfie") or die(mysql_error()) ; 

 # Prepare the SELECT Query
  $selectSQL = 'SELECT * FROM `image_upload`  INNER JOIN user_table
ON image_upload.user_id=user_table.user_id WHERE flag="0" ORDER BY timestamp DESC';
 # Execute the SELECT Query
  if( !( $selectRes = mysql_query( $selectSQL ) ) ){
    echo 'Retrieval of data from Database Failed - #'.mysql_errno().': '.mysql_error();
   }else{
    ?>
<table border="2">

  <thead id="head">
    <tr>
      <th id="head">User name</th>
      <th>Category</th>
      <th>Description</th>
      <th>Image</th>
      <th>Location</th>
      <th>Status</th>
    </tr>
  </thead>
  <tbody>
    <?php

    if (isset($_GET['submit'])) {

                        $mobile = $_GET['dmobile'];

                        $query = mysql_query("update image_upload set
  flag='$mobile' " );
                    }

                    if (isset($_GET['submit'])) {
                   header("Location: imageManagement.php");
                   }
        if( mysql_num_rows( $selectRes )==0 ){
        echo '<tr><td colspan="4">No Rows Returned</td></tr>';
      }else{
        while( $row = mysql_fetch_assoc( $selectRes ) ){
          echo "<tr>
             <td>{$row['user_name']}</td>
             <td>{$row['category']}</td>
             <td>{$row['description']}</td>
             <td ><img src='uploads/".$row['image']."'width=300px height=200px></td>
             <td>{$row['location']}</td>
             <td><form class=\"form\" method=\"get\"><label></label><br/>
             <input class=\"input\" type=\"text\" name=\"dmobile\" value=\"    {$row['flag']}\" />
           <br>
           <input class=\"submit\" type=\"submit\" name=\"submit\" value=\"update\" />
          </form></td>
          </tr>\n";
            }
      }
    ?>
  </tbody>
</table>
    <?php

In here when do changes and click on submit button of one row each and every rows are updated. How can I give unique value for each and every submit button.

Lanka
  • 29
  • 2
  • 11
  • what type of value you want to give? – itachi Nov 12 '14 at 05:18
  • Add a hidden value related to an "id" column for example in your form, then using a WHERE clause based on that. – Funk Forty Niner Nov 12 '14 at 05:19
  • When I click on that submit button I want to only change the value of that row and I want to add where clause to sql update query. But i dont know how to do it. – Lanka Nov 12 '14 at 05:21
  • you __completely changed the question__ with your last comment! – itachi Nov 12 '14 at 05:26
  • 2
    Add `` to your form, then add `$theid = $_POST['the_id'];` then `$query = mysql_query("update image_upload set flag='$mobile' WHERE id = '$theid' " );` that should work. You may need to play around with it a bit, in the hidden input that is. This is based on having an "id" column of course. – Funk Forty Niner Nov 12 '14 at 05:27

2 Answers2

2

Comment to answer, since OP said it works.

OP: "It's work. Thank you very much.... :) – Lanka"


Add this to your form:

<input type=\"hidden\" name=\"the_id\" value=\"{$row['id']}\" />

then add:

$theid = $_POST['the_id'];

then,

$query = mysql_query("update image_upload set flag='$mobile'  
WHERE id = '$theid' " );

You may need to play around with it a bit, in the hidden input that is.

This is based on having an "id" column of course.


N.B.:

  • You should validate the user input (even if it's a hidden field)

Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.

As it stands, you are using a deprecated MySQL library, which leaves you open to SQL injection.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0
else{
    $counter=0 ;
    while( $row = mysql_fetch_assoc( $selectRes ) ){
      $value="Update ".$counter ;
      $counter++ ;
      echo "<tr>
         <td>{$row['user_name']}</td>
         <td>{$row['category']}</td>
         <td>{$row['description']}</td>
         <td ><img src='uploads/".$row['image']."'width=300px height=200px></td>
         <td>{$row['location']}</td>
         <td><form class=\"form\" method=\"get\"><label></label><br/>
         <input class=\"input\" type=\"text\" name=\"dmobile\" value=\"    {$row['flag']}\" />
       <br>
       <input class=\"submit\" type=\"submit\" name=\"submit\" value=\"".$value."\" />
      </form></td>
      </tr>\n";
        }
  }

I do not think the question is clearly framed but by replacing the else part of your code with the above code you will get different values for submit button ie., update 0,update 1 and so on.. Hope this helps.

Arun
  • 111
  • 1
  • 1
  • 8