Jump to content

PHP with MYSQL


magcr23

Recommended Posts

Hi guys, i need your help.

 

the gold is when the allow button is pressed, in database, where visivel=0 become visivel=1.
 
<?php
require("bd/conexao.php");       
$sql = "SELECT * FROM produtos";
//$qr = mysql_query($sql) or die(mysql_error());
//$ln = mysql_fetch_assoc($qr); 
$resultado = mysql_query($sql);
$row = mysql_fetch_array($resultado);
 
if ($row{"visivel"}== 0){
$update= "UPDATE produtos SET visivel = 1 WHERE visivel = 0";
echo $update;
}
else {
echo "o produto ja esta visivel";
}   
?>
 
when i run that, i can see the echo but $update don't work, what am i doing wrong?
Link to comment
https://forums.phpfreaks.com/topic/296815-php-with-mysql/
Share on other sites

You missed mysql_query() for UPDATE query, also SELECT query should be below UPDATE query, so you will get updated data

<?php
	require("bd/conexao.php");       
	if ($row{"visivel"}== 0)
	{
		$update= "UPDATE produtos SET visivel = 1 WHERE visivel = 0";
		mysql_query($update);
		echo $update;
	}
	else
	{
		echo "o produto ja esta visivel";
	}   

	$sql = "SELECT * FROM produtos";
	$resultado = mysql_query($sql);
	$row = mysql_fetch_array($resultado);
?>
Link to comment
https://forums.phpfreaks.com/topic/296815-php-with-mysql/#findComment-1513886
Share on other sites

You missed mysql_query() for UPDATE query, also SELECT query should be below UPDATE query, so you will get updated data

 

That solution isn't going to quite work since $row{"visivel"} comes from the first query.

 

 

@magcr23 - That first query probably is not necessary. The update query will only change visivel when it is equal to zero.

 

If you need to show the message about the product already being shown, you could use mysql_affected_rows(). More information about the function can be found here:

http://php.net/manual/en/function.mysql-affected-rows.php

Link to comment
https://forums.phpfreaks.com/topic/296815-php-with-mysql/#findComment-1513889
Share on other sites

That solution isn't going to quite work since $row{"visivel"} comes from the first query.

 

 

@magcr23 - That first query probably is not necessary. The update query will only change visivel when it is equal to zero.

 

If you need to show the message about the product already being shown, you could use mysql_affected_rows(). More information about the function can be found here:

http://php.net/manual/en/function.mysql-affected-rows.php

 

oh sorry, i missed that

Link to comment
https://forums.phpfreaks.com/topic/296815-php-with-mysql/#findComment-1513892
Share on other sites

The message about the product already being shown was just for test. With your help i made it work, thx for that.

But now i saw a new problem, i can't do

$update= "UPDATE produtos SET visivel = 1 WHERE visivel = 0"; because it will update all visivel = 0, and i want update only 1.

rrux86pwm7sdqku1q5.png

As you can see i have a list of products, when i click allow on the first it will update all the others to visivel=1 when i only want 1 of them. 

Any sugestions to solve that?

 

btw, in my data base i have id, name, description, imgPath, price, visivel(allowed).

Link to comment
https://forums.phpfreaks.com/topic/296815-php-with-mysql/#findComment-1513895
Share on other sites

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.