Jump to content

[SOLVED] Php and Html plus updating db table


acctman

Recommended Posts

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" )

{

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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\">";
}
?>

Link to comment
Share on other sites

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\">";
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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\">";
}

Link to comment
Share on other sites

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\" />";
        }

Link to comment
Share on other sites

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>";
}
}
?>

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.