Jump to content

Problem with UPDATE using submit button

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
bajerocks

bajerocks

    Newbie

  • Members
  • Pip
  • 5 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";

?>




#2
Chessur

Chessur

    Newbie

  • Members
  • PipPip
  • 29 posts
<?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"; 
?>

I found your bug, you have been using wrong variables and operators in some places, but now it should work. Tell me how it goes.

Yours Truly,

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
You need to use a WHERE clause. And I'm not sure why you need your query in the foreach loop.
UPDATE table SET col8='Y' WHERE something=something