acctman Posted July 18, 2008 Share Posted July 18, 2008 Hi can someone help me make the coding below usable. Here's what i'm trying to do... User go to php page inputs there current Username and then the New Username they'd like then when they hit the submit button it checks to see if the Username is already taken if so the page will show and error and allow them to re-enter another name. If the name is available then the user db is updated with the new user name and this it logs the user off the site. I need to be able to display the HTML forum coding and process it all from 1 file. Current UserName: [ input box ] //input name Cuser New UserName: [ input box ] //NewUser [ submit button ] when user hits submit... //connect to db $cn1 = mysql_connect('localhost',$dbuser,$dbpass) or die(mysql_error()); mysql_select_db($db,$cn1) or die(mysql_error()); //check to see if "New UserName" exist" $query = "SELECT m_user FROM rate_members WHERE m_user = $NewUser"; // look for New user in DB $ident = mysql_query( $query, $cn1 ) or die( mysql_error( $cn1 ) ); $result = mysql_fetch_assoc( $ident ); if ( $myrow === false ) //the new username is not in use { mysql_query( "UPDATE m_user SET m_user = $NewUser". "WHERE m_user = $Cuser", $cn1 ) or die( mysql_error( $cn1 ) ); header("Location: http://www.ratemybottom.com/logout.php"); // log user out so they can re-enter with new name exit; } elseif ( $myrow === true ) die( "Username already in use, please select a another name. Link to update-name.php" ) { Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 18, 2008 Share Posted July 18, 2008 put your page in a if like this <?php if($_POST['submit']=='sent'){ //php }else{ //html form with <input type="submit" name="submit" value="sent"> } ?> the page is run twice once to display the form again to validate you can't check a form half throught the php like you wanted to. Scott. Quote Link to comment Share on other sites More sharing options...
acctman Posted July 21, 2008 Author Share Posted July 21, 2008 put your page in a if like this <?php if($_POST['submit']=='sent'){ //php }else{ //html form with <input type="submit" name="submit" value="sent"> } ?> the page is run twice once to display the form again to validate you can't check a form half throught the php like you wanted to. Scott. hi scott how would I add that to my code, i've been trying to figure it out for the past few days Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 21, 2008 Share Posted July 21, 2008 you have a page like this (Pseudocode) if(user press submit){ evaluate form }else{ print form with submit button } in php <?php if ($_POST['submit'] == "Submit") { //connect to db $cn1 = mysql_connect('localhost', $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($db, $cn1) or die(mysql_error()); //check to see if "New UserName" exist" $query = "SELECT m_user FROM rate_members WHERE m_user = $NewUser"; // look for New user in DB $ident = mysql_query($query, $cn1) or die(mysql_error($cn1)); $result = mysql_fetch_assoc($ident); if ($myrow === false) { //the new username is not in use mysql_query("UPDATE m_user SET m_user = $NewUser" . "WHERE m_user = $Cuser", $cn1) or die(mysql_error($cn1)); header("Location: http://www.ratemybottom.com/logout.php"); // log user out so they can re-enter with new name exit; } elseif ($myrow === true) { die("Username already in use, please select a another name. Link to update-name.php"); } }else{ echo "<form method=\"post\"><input type=\"text\" name=\"user\"><input type=\"submit\" name=\"submit\" value=\"Submit\">"; }?> Scott. Quote Link to comment Share on other sites More sharing options...
acctman Posted July 21, 2008 Author Share Posted July 21, 2008 Does this look like it'll work? I haven't been able to test it yet, I've just been reading over the coding and fixing anything i see wrong. Question in the inputbox name i have Cuser and NewUser, I think use $Cuser and $NewUser to insert the value for the sql lookup. Is that correct? Also does my sql update table method look correct? <?php if ($_POST['submit'] == "Submit") { // connect to db $cn1 = mysql_connect('localhost', $dbuser, $dbpass) or die(mysql_error()); mysql_select_db(ratemy_rater, $cn1) or die(mysql_error()); // check to see if "New UserName" exist" $query = "SELECT m_user FROM rate_members WHERE m_user = $NewUser"; // look for New user in DB $ident = mysql_query($query, $cn1) or die(mysql_error($cn1)); $result = mysql_fetch_assoc($ident); if ($result == false) { // the new username is not in use mysql_query("UPDATE m_user SET m_user = $NewUser" . "WHERE m_user = $Cuser", $cn1) or die(mysql_error($cn1)); header("Location: http://www.mysite.com/logout.php"); // log user out so they can re-enter with new name exit(); } elseif ($result == true) { die("Username already in use, please select a another name. Link to update-name.php"); } else { echo "<form method=\"post\">" echo "Current UserName: <input type=\"text\" name=\"Cuser\" value=\"\"><br>" echo "New UserName: <input type=\"text\" name=\"NewUser\" value=\"\"><br>" echo "<input type=\"submit\" name=\"submit\" value=\"Submit\">"; } ?> Quote Link to comment Share on other sites More sharing options...
marcus Posted July 21, 2008 Share Posted July 21, 2008 That won't work. Why don't you actually test what you've made? <?php if ($_POST['submit'] == "Submit") { // connect to db $cn1 = mysql_connect('localhost', $dbuser, $dbpass) or die(mysql_error()); mysql_select_db(ratemy_rater, $cn1) or die(mysql_error()); // check to see if "New UserName" exist" $query = "SELECT m_user FROM rate_members WHERE m_user = $NewUser"; // look for New user in DB $ident = mysql_query($query, $cn1) or die(mysql_error($cn1)); $result = mysql_fetch_assoc($ident); if ($result == false) { // the new username is not in use mysql_query("UPDATE m_user SET m_user = ".$NewUser." WHERE m_user = ".$CUser."") or die(mysql_error()); header("Location: http://www.mysite.com/logout.php"); // log user out so they can re-enter with new name exit(); } elseif ($result == true) { die("Username already in use, please select a another name. Link to update-name.php"); } } else { echo "<form method=\"post\">"; echo "Current UserName: <input type=\"text\" name=\"Cuser\" value=\"\"><br>"; echo "New UserName: <input type=\"text\" name=\"NewUser\" value=\"\"><br>"; echo "<input type=\"submit\" name=\"submit\" value=\"Submit\">"; } ?> Quote Link to comment Share on other sites More sharing options...
acctman Posted July 21, 2008 Author Share Posted July 21, 2008 I currently dont have access to my server from the location I'm at. I changed the Update code to this, based on examples I found on the net. mysql_query("UPDATE rate_members SET m_user = '".$_POST['Cuser']."' WHERE m_user = '".$_POST['NewUser']."'") or die(mysql_error($cn1)); Quote Link to comment Share on other sites More sharing options...
acctman Posted July 21, 2008 Author Share Posted July 21, 2008 I think i had the update reversed. SET is for the new update and WHERE is for locating the old correct? mysql_query("UPDATE rate_members SET m_user = '".$_POST['NewUser']."' WHERE m_user = '".$_POST['Cuser']."'") or die(mysql_error($cn1)); Quote Link to comment Share on other sites More sharing options...
acctman Posted July 22, 2008 Author Share Posted July 22, 2008 I'm getting an error Parse error: syntax error, unexpected T_BAD_CHARACTER, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/ratemy/public_html/update-name.php on line 22 } else { echo "<form method=\"post\">"; echo "Current UserName: <input type=\"text\" name=\"Cuser\" value=\"$_SESSION[\'user\']\"><br>"; echo "New UserName: <input type=\"text\" name=\"NewUser\" value=\"\"><br>"; echo "<input type=\"submit\" name=\"submit\" value=\"Submit\">"; } Quote Link to comment Share on other sites More sharing options...
Stephen Posted July 22, 2008 Share Posted July 22, 2008 Try replacing it with this: } else { echo "<form method=\"post\">"; echo "Current UserName: <input type=\"text\" name=\"Cuser\" value=\"".$_SESSION['user']."\" /><br />"; echo "New UserName: <input type=\"text\" name=\"NewUser\" value=\"\" /><br />"; echo "<input type=\"submit\" name=\"submit\" value=\"Submit\" />"; } Quote Link to comment Share on other sites More sharing options...
acctman Posted July 22, 2008 Author Share Posted July 22, 2008 thanks stephan, i'm not getting the error anymore. My next problem is that nothing is happening. it should show the Form when the page loads. Does anyone know what's wrong? <?php session_start(); if($_SESSION['user']=="") { header("Location: http://www.mysite.com/index.php?req=login&redirect=/update-name.php"); exit; } if ($_POST['submit'] == "Submit") { // connect to db $cn1 = mysql_connect('localhost', '', '') or die(mysql_error()); mysql_select_db(ra_rater, $cn1) or die(mysql_error()); // check to see if "New UserName" exist" $query = "SELECT m_user FROM rate_members WHERE m_user = $NewUser"; // look for New user in DB $ident = mysql_query($query, $cn1) or die(mysql_error($cn1)); $result = mysql_fetch_assoc($ident); if ($result == false) { // the new username is not in use mysql_query("UPDATE ra_members SET m_user = '".$_POST['NewUser']."' WHERE m_user = '".$_POST['Cuser']."'") or die(mysql_error($cn1)); header("Location: http://www.site.com/logout.php"); // log user out so they can re-enter with new name exit(); } elseif ($result == true) { die("Username already in use, please select a another name. Link to <a href='update-name.php'><b>Reload</b></a>"); } else { echo "<form method=\"post\">"; echo "Current UserName: <input type=\"text\" name=\"Cuser\" value=\"".$_SESSION['user']."\" /><br />"; echo "New UserName: <input type=\"text\" name=\"NewUser\" value=\"\" /><br />"; echo "<input type=\"submit\" name=\"submit\" value=\"Submit\" />"; echo "</form>"; } } ?> Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 22, 2008 Share Posted July 22, 2008 shift the else that displays the for down to the end of the if($_POST['submit'])... rather than your list of elseif statements. Scott. Quote Link to comment 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.