0

I am querying table in a while loop and has a button to update data for each row when clicked. But the update isn't happening .

The table column "Enroll" is to be updated based on clicking the submit button for that row.

<?php
$connection = mysql_connect('localhost', 'root', ''); 
mysql_select_db('mydata');

$query = "SELECT * FROM internship";
$result = mysql_query($query); 
while($row = mysql_fetch_array($result)){  
    if($row['Enroll']=="unenroll")
    { 
        echo '<div class="intern">';
        echo '<h1>'.$row['course'] . "</h1>" ;
        echo '<h2>'.$row['compay'] . "</h2>";
        echo "<h3>Stipend:Rs." . $row['stipend'] ."<h3>";
        echo "<h3>Duration:".$row['duration']."month</h3>";
        echo "<h3>Start-date:".$row['start date']."</h3>";
        echo '</div>';
        echo '<form method="post" action="">';
        echo '<button name="add_to_cart" type="submit" ><h2>Enroll</h2></button>';
        echo '</form>';
        if(isset($_POST["add_to_cart"]))
        {
            $selectedProduct =  $row["ID"];
            $sql='UPDATE internship SET Enroll="enrolled" WHERE ID="$selectedProduct"';
            mysql_query($connection,$sql);    
            header("location:enrolled.php");            
         }
     }
}
mysql_close($connection);
?>

I am not able to update. Please help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    Don't use `mysql_*` functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`pdo`](http://php.net/manual/en/book.pdo.php) instead. [**And this is why you shouldn't use `mysql_*` functions**](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – Rajdeep Paul Apr 20 '19 at 17:37
  • they are working fine for me as i am using xampp server – Shaurya Nigam Apr 20 '19 at 17:41
  • Your update is happening for the __first__ record with `unenroll` status in a table. – u_mulder Apr 20 '19 at 17:48
  • even the first record is not getting updated – Shaurya Nigam Apr 20 '19 at 17:50
  • Possible duplicate of [Why shouldn't I use mysql\_\* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Dharman Jun 26 '19 at 09:00

1 Answers1

0

There are few issues with your code, such as:

  1. You haven't included any ID value in the button of each row.

    echo '<button name="add_to_cart" type="submit" ><h2>Enroll</h2></button>';
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --> no value="..."
    
  2. You are not using the selected row i.e. the row for which the button has been clicked to update the table row with enrolled.

    $selectedProduct =  $row["ID"];
                        ^^^^^^^^^^
    

So your code should be like this:

<?php
    $connection = mysql_connect('localhost', 'root', ''); 
    mysql_select_db('mydata');

    if(isset($_POST["add_to_cart"])){
        $selectedProduct = $_POST['add_to_cart'];
        $sql ="UPDATE internship SET Enroll = 'enrolled' WHERE ID = '$selectedProduct'";
        mysql_query($connection,$sql);    
        header("location:enrolled.php");   
        exit();
    }

    $query = "SELECT * FROM internship";
    $result = mysql_query($query); 
    while($row = mysql_fetch_array($result)){  
        if($row['Enroll']=="unenroll"){ 
            ?>
            <div class="intern">
                <h1><?php echo $row['course']; ?></h1>
                <h2><?php echo $row['compay']; ?></h2>
                <h3>Stipend:Rs <?php echo $row['stipend']; ?><h3>
                <h3>Duration: <?php echo $row['duration']; ?> month</h3>
                <h3>Start-date: <?php echo $row['start date']; ?></h3>
            </div>
            <form method="post" action="">
                <button name="add_to_cart" value="<?php echo $row['ID']; ?>" type="submit"><h2>Enroll</h2></button>
            </form>
            <?php
        }
    }

    mysql_close($connection);
?>

Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • I suppose there's no need to generate table if POST is set. Because `header` will redirect immediately. – u_mulder Apr 20 '19 at 17:57
  • Not sure why you put curly braces around the variable being interpolated. That doesn't do anything in this case. – tadman Apr 20 '19 at 18:00
  • @u_mulder Yes, I missed that part, moved `if(isset($_POST["add_to_cart"])){...}` at the top of the page. – Rajdeep Paul Apr 20 '19 at 18:05
  • @tadman Cannot recall but somewhere I had read some benefit of using curly braces to enclose variable in SQL statements. Never mind, removed `'`s from the SQL statement. – Rajdeep Paul Apr 20 '19 at 18:07
  • Variables *do not belong* in SQL statements and giving them little wings for protection seems more like superstition than anything useful. In regular PHP string interpolation they are required for *complex interpolations*, where `"You're {$person->age} years old!"` is a valid use. – tadman Apr 20 '19 at 18:09
  • actually i am querying an internship portfolio with a button to enroll into the internship and on submitting it should update from unenroll to enrolled and then view it in the enrolled.php page.The ID is the column of the table not the id element – Shaurya Nigam Apr 20 '19 at 18:47
  • @ShauryaNigam The proposed solution does what you said above^. After getting redirected to *enrolled.php* page, just query the table and display the results. What is not working for you now? – Rajdeep Paul Apr 20 '19 at 18:51
  • The updation should happen for the selected row but the updation is not happening at all. – Shaurya Nigam Apr 20 '19 at 18:55
  • @ShauryaNigam I missed to use `` in the form, updated my answer. Please check again. In case it's not working again, please paste the output of `var_dump($_POST['add_to_cart']);` in [pastebin.com](https://pastebin.com/) and give it's link here. By the way, write `var_dump($_POST['add_to_cart']);` at the top of your page and capture it's output after you hit the *Enroll* button. – Rajdeep Paul Apr 20 '19 at 19:07