Jump to content

Problem with UPDATE using submit button


bajerocks

Recommended Posts

    Basically I have a mysql table with 9 columns in it. However I am only going to display 7, and out of those seven I am going to color the ones that are set indifferent. Also I am going to give a button to each row. Now I am stuck there. What I basically want is, when the button is clicked I want the value of a single column of that row to be changed, nothing else. I tried using following codes to do that, but am not being able to get the result. All I get is when the button is clicked, every value of that column changes, rather than only that row.

 

    Please help me out, this is my project for finals, I am desperate.

 

 


<?php
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","root"); 
    
//select which database you want to edit
mysql_select_db("DATABASE"); 


$count = 0;

//get the mysql and store them in $result
//change whatevertable to the mysql table you're using
//change whatevercolumn to the column in the table you want to search
$result = mysql_query("SELECT * FROM table");
$self = $_SERVER['PHP_SELF'];
$form = "<form method=\"post\" action=\"$self\"><input type=\"submit\" name=\"submit\" value=\"change_value\"></form>"; 





echo "<table border=1>";
echo "<tr>";
        echo "<td></td>";
        echo "<td>col1</td>";
           echo "<td>col2</td>";



           echo "<td>col3</td>";
           echo "<td>col4</td>";
        echo "<td>col5</td>";
           echo "<td>col6</td>";

           echo "<td>col7</td>";
echo "</tr>";


//grab all the content
while($r = mysql_fetch_array($result))
{    
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
  

   $col1=$r["col1"];
   $col2=$r["col2"];
   $col3=$r["col3"];
   $col4=$r["col4"];
   $col5=$r["col5"];
   $col6=$r["col6"];
    $col7=$r["col7"];
    $col8=$r["col8"];
   $col9=$r["col9"];
//there are a total of 9 columns in the table


       if(($col9 == "y") || ($col9 == "Y"))
    {
        if(($col8 == "Y") || ($col8 == "y"))
        {
            echo "<tr>";
            echo "<td></td>";
               echo '<td bgcolor="#ffff00">';
            echo "$col1";
            echo '</td>';
               echo '<td bgcolor="#ffff00">';
            echo "$col2";
            echo '</td>';
               echo '<td bgcolor="#ffff00">';
            echo "$col3";
            echo '</td>';
               echo '<td bgcolor="#ffff00">';
            echo "$col4";
            echo '</td>';
            echo '<td bgcolor="#ffff00">';
            echo "$col5";
            echo '</td>';
               echo '<td bgcolor="#ffff00">';
            echo "$col6";
            echo '</td>';
               echo '<td bgcolor="#ffff00">';
            echo "$col7";
            echo '</td>';
               echo "</tr>";
   //display the row

    }
    else{
        echo "<tr>";
        print "<td>$form</td>"; 
           echo "<td>$col1</td>";
           echo "<td>$col2</td>";

           echo "<td>$col3</td>";
           echo "<td>$col4</td>";
        echo "<td>$col5</td>";
           echo "<td>$col6</td>";

           echo "<td>$col7</td>";
           echo "</tr>";

    }
               if(isset($_POST['submit']))
        {
            mysql_query("UPDATE table SET col8='Y' ");
    }
   $count = $count + 1;

        }
   
}
echo "</table><br />";


echo "only found $count number of datas";
?>

Link to comment
https://forums.phpfreaks.com/topic/206432-problem-with-update-using-submit-button/
Share on other sites

You need to identify the row by a unique field, normally an id.  So in the form set a hidden input to the value of the id, like:

$id = $r['id'];
echo '<input type="hidden" name="id" value="' . $id . '">';

 

Then get the id from $_POST and use it in the query:

$id = (int)$_POST['id'];
mysql_query("UPDATE table SET col8='Y' WHERE id='$id'");

You need to identify the row by a unique field, normally an id.  So in the form set a hidden input to the value of the id, like:

$id = $r['id'];
echo '<input type="hidden" name="id" value="' . $id . '">';

 

Then get the id from $_POST and use it in the query:

$id = (int)$_POST['id'];
mysql_query("UPDATE table SET col8='Y' WHERE id='$id'");

 

 

Thanks for the reply,

 

Now I have a unique id column named "id", like you wrote.

However the problem persists,

 

The code now look like,

 



<?php
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","root"); 
    
//select which database you want to edit
mysql_select_db("DATABASE"); 


$count = 0;

//get the mysql and store them in $result
//change whatevertable to the mysql table you're using
//change whatevercolumn to the column in the table you want to search
$result = mysql_query("SELECT * FROM table");
$self = $_SERVER['PHP_SELF'];
$form = "<form method=\"post\" action=\"$self\"><input type=\"submit\" name=\"submit\" value=\"change_value\"></form>"; 





echo "<table border=1>";
echo "<tr>";
        echo "<td></td>";
        echo "<td>col1</td>";
           echo "<td>col2</td>";



           echo "<td>col3</td>";
           echo "<td>col4</td>";
        echo "<td>col5</td>";
           echo "<td>col6</td>";

           echo "<td>col7</td>";
echo "</tr>";


//grab all the content
while($r = mysql_fetch_array($result))
{    
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
  
$id = $r['id'];
   $col1=$r["col1"];
   $col2=$r["col2"];
   $col3=$r["col3"];
   $col4=$r["col4"];
   $col5=$r["col5"];
   $col6=$r["col6"];
    $col7=$r["col7"];
    $col8=$r["col8"];
   $col9=$r["col9"];
//there are a total of 9 columns in the table


       if(($col9 == "y") || ($col9 == "Y"))
    {
        if(($col8 == "Y") || ($col8 == "y"))
        {
            echo "<tr>";
            echo "<td></td>";
               echo '<td bgcolor="#ffff00">';
            echo "$col1";
            echo '</td>';
               echo '<td bgcolor="#ffff00">';
            echo "$col2";
            echo '</td>';
               echo '<td bgcolor="#ffff00">';
            echo "$col3";
            echo '</td>';
               echo '<td bgcolor="#ffff00">';
            echo "$col4";
            echo '</td>';
            echo '<td bgcolor="#ffff00">';
            echo "$col5";
            echo '</td>';
               echo '<td bgcolor="#ffff00">';
            echo "$col6";
            echo '</td>';
               echo '<td bgcolor="#ffff00">';
            echo "$col7";
            echo '</td>';
               echo "</tr>";
   //display the row

    }
    else{
        echo "<tr>";
        print "<td>$form</td>"; 
echo '<input type="hidden" name="id" value="' . $id . '">';
           echo "<td>$col1</td>";
           echo "<td>$col2</td>";

           echo "<td>$col3</td>";
           echo "<td>$col4</td>";
        echo "<td>$col5</td>";
           echo "<td>$col6</td>";

           echo "<td>$col7</td>";
           echo "</tr>";

    }
               if(isset($_POST['submit']))
        {
$id = (int)$_POST['id'];
mysql_query("UPDATE table SET col8='Y' WHERE id='$id'");
            mysql_query("UPDATE table SET col8='Y' ");
    }
   $count = $count + 1;

        }
   
}
echo "</table><br />";


echo "only found $count number of datas";
?>

 

 

So now when I click the button "change_value" nothing happens. Whereas in what I want is for the value of col8 to change to "Y" but only of that row. So please I am desperate here, help me.

One problem at least is this (you only want the first one):

mysql_query("UPDATE table SET col8='Y' WHERE id='$id'");
            mysql_query("UPDATE table SET col8='Y' ");

Secondly, do you really have a column called id in the table that has a unique id for each row?

One problem at least is this (you only want the first one):

mysql_query("UPDATE table SET col8='Y' WHERE id='$id'");
            mysql_query("UPDATE table SET col8='Y' ");

Secondly, do you really have a column called id in the table that has a unique id for each row?

 

I removed it and only have the first one however still the same result and yes I do have the column named "id" , just added, which has unique value. :'(

Man this is getting on my nerve, feel like punching my computer but thats gonna hurt myself and I am too much of a pussy to do that, so please help me.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.