Page 1 of 1

mysql statments in php

Posted: Mon Jun 18, 2007 4:46 pm
by invisibled
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


ok so i'm building this content management system. And i have a function to add new users and update the users information.  Adding new users works fine, and when i test the script on my server (invisibled.com) updating works, but when i test it on my local host updating doesn't work. When i say updating i mean the sql query doesn't update the database. so basicly, i was wondering if anybody could shed some light as to why its not working on my localhost.

my localhost is running php 5 and so is my server (invisibled.com) and the functions get loaded from a class file. i'll paste in the functions i have for updating.

this code is in users.php and runs after you hit submit on the form in usersEdit.php (its all pretty straight forward)

Code: Select all

//UPDATE USERS FUNCTION
		if (isset ($_POST["updateID"])){
		  $updateID = $_POST["updateID"];
		
		  $realName = $_POST["realName"];
		
		  $realName = str_replace( "'", "'", $realName );

		  $queryUpd = " UPDATE ida_users SET realName='$realName' WHERE entryID='$updateID';";
		  $rsUpd = mysql_query($queryUpd) or die("<h1>Query Failed</h1> <b> $queryUpd</b>");
		}
		//UPDATE USERS FUNCTION

this is the function that loads on usersEdit.php

Code: Select all

function usersEdit($go){
		
		$updateID = $_GET["updateID"];
		$qSel = "SELECT * FROM ida_users WHERE entryID='$updateID';";
		$rsSel = mysql_query($qSel) or die("$qSel failed");
		if($res = mysql_fetch_array($rsSel)) {
			$realName =$res["realName"];
		}
?>
	<table>
		<form action="users.php" method="POST">
		<td><input type="hidden" name="updateID" value="<?=$updateID?>" /></td></tr>
		  <tr><td><input type="text" name="realName" value="<?php echo $realName?>" /></td></tr>

		  <tr><td width="10" align="right"><input type="submit" name="update" value="Save" /></td><td>&nbsp;</td></tr>
		</form>
	</table>
<?php	
	}//END usersEdit($go)


any help what so ever would be great!
-Shan


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jun 18, 2007 4:48 pm
by superdezign
Are you sure you're using the correct database credentials? What's the error?

Posted: Mon Jun 18, 2007 5:07 pm
by invisibled
yup i know the script works because it works totally fine on my webserver.

when i run it on my localhost there is no error, i just returns back to the users.php page (like it should) but it doesn't update anything in the database

Posted: Mon Jun 18, 2007 5:18 pm
by volka
try

Code: Select all

//UPDATE USERS FUNCTION
if (isset ($_POST["updateID"])){
	$updateID = $_POST["updateID"];
	$realName = $_POST["realName"];
	$realName = str_replace( "'", "'", $realName ); // see mysql_real_escape_string

	$queryUpd = " UPDATE ida_users SET realName='$realName' WHERE entryID='$updateID';";
	echo '<div>Debug ', __FILE__,'@',__LINE, ': ', htmlentities($queryUpd), "</div>\n";
	$rsUpd = mysql_query($queryUpd) or die("<h1>Query Failed</h1> <b> $queryUpd</b>");
	echo '<div>Debug ', __FILE__,'@',__LINE, ': affcted_rows=', mysql_num_rows($rsUpd), "</div>\n";
}
//UPDATE USERS FUNCTION

Posted: Mon Jun 18, 2007 5:19 pm
by superdezign
Well, that's what I'm saying. The database credentials on your server are probably different than locally. The default for local servers is 'root' as a username and an empty password.

And if that's not the case, are you sure that both databases have the same structure and data? It's possible that your aren't meeting your WHERE clause locally.

Posted: Mon Jun 18, 2007 5:22 pm
by Gente
Try to check your post before update. Print the query. Try to check it with phpMyAdmin.
Maybe your DB is broken.
I think it's the best way to find the problem or localize to make more strict question.

And BTW... Are you sure about this code? :)

Code: Select all

$realName = str_replace( "'", "'", $realName );

Posted: Tue Jun 19, 2007 2:48 pm
by invisibled
no the db credentials are fine, i know that.

it just won't run update queries on the localhost.... its so wierd

Posted: Tue Jun 19, 2007 2:51 pm
by superdezign
Updates are the only ones that don't work? What about inserts and selects?

Are you getting any errors?


And check the code that Gente pointed out. I'd suggest replacing it with mysql_real_escape_string.

Posted: Tue Jun 19, 2007 3:16 pm
by volka
And have you tried my code snippet?
What does it print?

Posted: Tue Jun 19, 2007 3:17 pm
by ReverendDexter
MySQL also has permissions for users that are seperate for localhost and everywhere else, you might double-check those in phpmyadmin, just make sure the users you're logging in as have permission for either all/any (I think it just comes up as a '%').

At least, that's what I immediately thought of when you said it works remotely but not locally.

Hope that helps!
-Dex