Jump to content

[SOLVED] mysql update query


hellonoko

Recommended Posts

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>';

?>

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

   $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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.