magcr23 Posted June 15, 2015 Share Posted June 15, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/296815-php-with-mysql/ Share on other sites More sharing options...
Solution PravinS Posted June 15, 2015 Solution Share Posted June 15, 2015 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/296815-php-with-mysql/#findComment-1513886 Share on other sites More sharing options...
cyberRobot Posted June 15, 2015 Share Posted June 15, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/296815-php-with-mysql/#findComment-1513889 Share on other sites More sharing options...
PravinS Posted June 15, 2015 Share Posted June 15, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/296815-php-with-mysql/#findComment-1513892 Share on other sites More sharing options...
magcr23 Posted June 15, 2015 Author Share Posted June 15, 2015 (edited) 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. 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 June 15, 2015 by magcr23 Quote Link to comment https://forums.phpfreaks.com/topic/296815-php-with-mysql/#findComment-1513895 Share on other sites More sharing options...
cyberRobot Posted June 15, 2015 Share Posted June 15, 2015 You could pass along a row ID with the button clicks. Then you just need to modify the update query to include the ID in the WHERE clause. Quote Link to comment https://forums.phpfreaks.com/topic/296815-php-with-mysql/#findComment-1513896 Share on other sites More sharing options...
magcr23 Posted June 15, 2015 Author Share Posted June 15, 2015 You could pass along a row ID with the button clicks. Then you just need to modify the update query to include the ID in the WHERE clause. Something like that? $id=$row{"id"}; Quote Link to comment https://forums.phpfreaks.com/topic/296815-php-with-mysql/#findComment-1513899 Share on other sites More sharing options...
Barand Posted June 15, 2015 Share Posted June 15, 2015 Something like $update= "UPDATE produtos SET visivel = 1 WHERE id = $requiredID"; Quote Link to comment https://forums.phpfreaks.com/topic/296815-php-with-mysql/#findComment-1513921 Share on other sites More sharing options...
cyberRobot Posted June 15, 2015 Share Posted June 15, 2015 Something like that? $id=$row{"id"}; Just to clarify, are you asking how to attach an ID to the allow button? Or are you looking for help with modifying the update query? Quote Link to comment https://forums.phpfreaks.com/topic/296815-php-with-mysql/#findComment-1513925 Share on other sites More sharing options...
magcr23 Posted June 15, 2015 Author Share Posted June 15, 2015 cyberRobot forget that xD I made it by url, i think it would be easier Quote Link to comment https://forums.phpfreaks.com/topic/296815-php-with-mysql/#findComment-1513932 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.