Jump to content

PHP with MYSQL


magcr23
Go to solution Solved by PravinS,

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
Share on other sites

  • Solution

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
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
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
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).

Edited by magcr23
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.