hellonoko Posted November 4, 2008 Share Posted November 4, 2008 I have a simple query that updates a field by 1 every time a user upload a file. mysql_query("UPDATE users SET uploads = uploads +1 WHERE login = '$user'"); When I run this query in myPhpAdmin replacing $user with a user name it works correctly. However when I use it in the script it does not work. The $user variable is used early and is working. Full code below. <?php session_start(); error_reporting(E_ALL); ini_set('display_errors', '1'); //////////////////////////////////////////////// /////////////////////////////////////////////// $username = "sharingi_music"; $password = "music"; $hostname = "localhost"; $db_connect = mysql_connect( $hostname , $username, $password) or die ("Unable to connect to database"); mysql_select_db( "sharingi_music" , $db_connect); ///////////////////////////////////////////////// //////////////////////////////////////////////// //include '../menu.php'; echo "<br>"; $user = $_SESSION['login']; // // echo "<title>Share it!</title>"; echo '<table width="100%" align="center" border="0">'; echo "<tr>"; echo '<td bgcolor="#FFFFFF">'; echo '<font face="Arial" color="red" size="36">SHARING </font>'; echo "</td>"; echo "</tr>"; echo "</table>"; //copies all files uploaded into the users personal folder. if ($handle = opendir("/home2/sharingi/public_html/subdomains/$user/tmp/")) { //list all files and copies to users folder. while (false !== ($file = readdir ($handle))) { if ($file !== '.' && $file !== '..' && $file !== '.s' ) { //CLEAN UP FILE NAME $clean_name = str_replace( " ", "_" , $file); $hiddenname = date('Y-m-d') . "-" . rand( 1000,5000) . ".data"; /////////////////////////////////////////////////////// mysql_query("INSERT INTO songs ( SONG , USER, HIDDENNAME) VALUES ( '$clean_name' , '$user' , '$hiddenname' )"); /////////////////////////////////////////////////////// copy ("/home2/sharingi/public_html/subdomains/$user/tmp/$file" , "/home2/sharingi/public_html/subdomains/$user/filez/$hiddenname"); } } closedir ($handle); } /////////////////////////////////// mysql_close ( $db_connect); //////////////////////////////////// //////////////////////////////////// #Adds to the count of how many files the user had uploaded //////////////////////////////////// //////////////////////////////////// $db_connect = mysql_connect( $hostname , $username, $password) or die ("Unable to connect to database"); mysql_select_db( "sharingi_simpleauth" , $db_connect); //////////////////////////////////// //copies all files uploaded into the users personal folder. if ($handle = opendir("/home2/sharingi/public_html/subdomains/$user/tmp/")) { //list all files and copies to users folder. while (false !== ($file = readdir ($handle))) { if ($file !== '.' && $file !== '..' && $file !== '.s' ) { mysql_query("UPDATE users SET uploads = uploads +1 WHERE login = '$user'"); } } closedir ($handle); } /////////////////////////////////// mysql_close ( $db_connect); //////////////////////////////////// if ($handle = opendir("/home2/sharingi/public_html/subdomains/$user/tmp/")) { //echo "<b>Copying to master directory...<b><br>"; while (false !== ($file = readdir ($handle))) { if ($file !== '.' && $file !== '..' && $file !== '.s' ) { // CLEAN UP FILE NAME $clean_name = str_replace( " ", "_" , $file); //echo "$clean_name<br>"; copy ("/home2/sharingi/public_html/subdomains/$user/tmp/$file" , "/home2/sharingi/public_html/REPOSITORY/$hiddenname"); } } closedir ($handle); } //echo "<b>Files copied...<b>"; //DELETES TEMP FILES if ($handle = opendir("/home2/sharingi/public_html/subdomains/$user/tmp/")) { while (false !== ($file = readdir ($handle))) { if ($file !== '.' && $file !== '..' && $file !== '.s' ) { unlink("/home2/sharingi/public_html/subdomains/$user/tmp/$file"); //echo "$file<br>"; //copy ("files/$file" , "/home2/sharingi/public_html/REPOSITORY/$file"); } } closedir ($handle); } echo '<head>'; echo '<meta HTTP-EQUIV="REFRESH" content="4; url=http://www.sharingizcaring.com/REPOSITORY/">'; echo '</head>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/131421-solved-mysql-update-query/ Share on other sites More sharing options...
.josh Posted November 4, 2008 Share Posted November 4, 2008 If you are sure that $user has the value it's supposed to have, try adding this to the end: mysql_query("UPDATE users SET uploads = uploads +1 WHERE login = '$user'") or die(mysql_error()); see if it's spitting out any errors. If it's not spitting out any errors, I'd assume that that's not the problem, and move on to looking at your conditions and loops for errors. Quote Link to comment https://forums.phpfreaks.com/topic/131421-solved-mysql-update-query/#findComment-682546 Share on other sites More sharing options...
AndyB Posted November 5, 2008 Share Posted November 5, 2008 $db_connect = mysql_connect( $hostname , $username, $password) or die ("Unable to connect to database"); mysql_select_db( "sharingi_music" , $db_connect); That looks illogical. At the second of those lines, $dbconnect will be either true or false. And what I'd expect to work is: mysql_select_db( "sharingi_music");// assuming that's the db name Quote Link to comment https://forums.phpfreaks.com/topic/131421-solved-mysql-update-query/#findComment-682620 Share on other sites More sharing options...
corbin Posted November 5, 2008 Share Posted November 5, 2008 $db_connect = mysql_connect( $hostname , $username, $password) or die ("Unable to connect to database"); mysql_select_db( "sharingi_music" , $db_connect); That looks illogical. At the second of those lines, $dbconnect will be either true or false. And what I'd expect to work is: mysql_select_db( "sharingi_music");// assuming that's the db name Return Values Returns a MySQL link identifier on success, or FALSE on failure. It's valid. Quote Link to comment https://forums.phpfreaks.com/topic/131421-solved-mysql-update-query/#findComment-682631 Share on other sites More sharing options...
.josh Posted November 5, 2008 Share Posted November 5, 2008 $db_connect = mysql_connect( $hostname , $username, $password) or die ("Unable to connect to database"); mysql_select_db( "sharingi_music" , $db_connect); That looks illogical. At the second of those lines, $dbconnect will be either true or false. And what I'd expect to work is: mysql_select_db( "sharingi_music");// assuming that's the db name You can assign the resource id to a var and mysql_select_db and mysql_query will accept that var as an optional 2nd argument (useful if working with multiple connections). Quote Link to comment https://forums.phpfreaks.com/topic/131421-solved-mysql-update-query/#findComment-682639 Share on other sites More sharing options...
AndyB Posted November 5, 2008 Share Posted November 5, 2008 I'd never seen that construction. Learn something new every day Quote Link to comment https://forums.phpfreaks.com/topic/131421-solved-mysql-update-query/#findComment-682642 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.