Jump to content

[SOLVED] sorry to bother you guys again


ronnie88

Recommended Posts

I'm having trouble inserting data into a table.

When I run this script it'll update the first table bcuz it only has 2 columns for which the script uses to check username and password but it won't update the second one bcuz I have about 5 different  columns in the virtual table. just gives me an error but when I delete the other columns except the username in the second table(virtual) works fine......any ideas?

PARTIAL:

function addNewUser($username, $password){
   global $conn;
   $q = "INSERT INTO users VALUES ('$username', '$password')";
   return mysql_query($q,$conn);

}

/**
* adds user to other table
*/

function useradd($username, $password){
   global $conn;
   $query  = "UPDATE virtual SET username = '$username' WHERE username = '{$_SESSION['username']}'";
   return mysql_query($q,$conn);

}

 

ENTIRE SCRIPT:

<?
session_start(); 
include("database.php");

/**
* Returns true if the username has been taken
* by another user, false otherwise.
*/
function usernameTaken($username){
   global $conn;
   if(!get_magic_quotes_gpc()){
      $username = addslashes($username);
   }
   $q = "select username from users where username = '$username'";
   $result = mysql_query($q,$conn) or trigger_error(mysql_error(), E_ALL);
   return (mysql_numrows($result) > 0);
}

/**
* Inserts the given (username, password) pair
* into the database. Returns true on success,
* false otherwise.
*/
function addNewUser($username, $password){
   global $conn;
   $q = "INSERT INTO users VALUES ('$username', '$password')";
   return mysql_query($q,$conn);

}

/**
* adds user to other table
*/

function useradd($username, $password){
   global $conn;
   $query  = "UPDATE virtual SET username = '$username' WHERE username = '{$_SESSION['username']}'";
   return mysql_query($q,$conn);

}

/**
* Displays the appropriate message to the user
* after the registration attempt. It displays a 
* success or failure status depending on a
* session variable set during registration.
*/
function displayStatus(){
   $uname = $_SESSION['reguname'];
   if($_SESSION['regresult']){
?>

<h1>Registered!</h1>
<p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p>

<?
   }
   else{
?>

<h1>Registration Failed</h1>
<p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>,<?echo $result;?> could not be completed.<br>
Please try again at a later time.</p>

<?
   }
   unset($_SESSION['reguname']);
   unset($_SESSION['registered']);
   unset($_SESSION['regresult']);
}

if(isset($_SESSION['registered'])){
/**
* This is the page that will be displayed after the
* registration has been attempted.
*/
?>

<html>
<title>Registration Page</title>
<body>

<? displayStatus(); ?>

</body>
</html>

<?
   return;
}

/**
* Determines whether or not to show to sign-up form
* based on whether the form has been submitted, if it
* has, check the database for consistency and create
* the new account.
*/
if(isset($_POST['subjoin'])){
   /* Make sure all fields were entered */
   if(!$_POST['user'] || !$_POST['pass']){
      die('You didn\'t fill in a required field.');
   }

   /* Spruce up username, check length */
   $_POST['user'] = trim($_POST['user']);
   if(strlen($_POST['user']) > 30){
      die("Sorry, the username is longer than 30 characters, please shorten it.");
   }

   /* Check if username is already in use */
   if(usernameTaken($_POST['user'])){
      $use = $_POST['user'];
      die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
   }

   /* Add the new account to the database */
   $md5pass = md5($_POST['pass']);
   $_SESSION['reguname'] = $_POST['user'];
   $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass);
   $_SESSION['regresult'] = useradd($_POST['user'], $md5pass);
   $_SESSION['registered'] = true;
   echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[php_SELF]\">";
   return;
}
else{
/**
* This is the page with the sign-up form, the names
* of the input fields are important and should not
* be changed.
*/
?>

<html>
<title>Registration Page</title>
<body>
<h1>Register</h1>
<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr>
</table>
</form>
</body>
</html>


<?
}
?>

Link to comment
Share on other sites

I have to be honest, I didn't read through your code because I don't really understand your question/problem. I did note you mention you get an error, might help if you tell us what it is.

 

Also, I noticed you assign your query to the $query variable within useradd() but then pass mysql_query() an undefined $q variable.

 

$query  = "UPDATE virtual SET username = '$username' WHERE username = '{$_SESSION['username']}'";
return mysql_query($q,$conn);

Link to comment
Share on other sites

well the script works fine.... its hard to explain it lol

 

Here it goes again...

 

the function for the first mysql insert works fine...

 

In my phpmyadmin the table cannot have any columns except for the username this is for the table virtual if i add another column it does not add anymore columns....when i try to register with more columns in the virtual table i just get the registration failed message

 

Link to comment
Share on other sites

[quopte]well the script works fine.... its hard to explain it lol

 

Why post it then?

 

In my phpmyadmin the table cannot have any columns except for the username this is for the table virtual if i add another column it does not add anymore columns....

 

That I'm afraid makes no sense at all.

 

if i add another column it does not add anymore columns

 

That part in particular is a contradiction.

Link to comment
Share on other sites

lol sorry i'm tired Okay step by step here it goes...

 

I have the column username in the virtual table...thats 1 column in the table... but if i add another column in the virtual table it doesn't insert the username column, only inserts in the users table...

Link to comment
Share on other sites

Here's your problem.

function useradd($username, $password){
   global $conn;
   $query  = "UPDATE virtual SET username = '$username' WHERE username = '{$_SESSION['username']}'";
   return mysql_query($q,$conn);

}

If you are going to add a user your query needs to be:

function useradd($username, $password){
   global $conn;
$query = "INSERT INTO virtual VALUES('$username', '$password')";
return mysql_query($query, $conn);
}

Link to comment
Share on other sites

oops i put the old one in there it's doing the same thing your close btw.

 

If I add another column in the virtual table it does not insert but if I leave username column/key the only one in the table its give registration failed..

 

<?
session_start(); 
include("database.php");

/**
* Returns true if the username has been taken
* by another user, false otherwise.
*/
function usernameTaken($username){
   global $conn;
   if(!get_magic_quotes_gpc()){
      $username = addslashes($username);
   }
   $q = "select username from users where username = '$username'";
   $result = mysql_query($q,$conn) or trigger_error(mysql_error(), E_ALL);
   return (mysql_numrows($result) > 0);
}

/**
* Inserts the given (username, password) pair
* into the database. Returns true on success,
* false otherwise.
*/
function addNewUser($username, $password){
   global $conn;
   $q = "INSERT INTO users VALUES ('$username', '$password')";
   return mysql_query($q,$conn);

}

/**
* adds user to other table
*/

function useradd($username){
   global $conn;
   $q = "INSERT INTO virtuala VALUES ('$username')";
   return mysql_query($q,$conn);

}

/**
* Displays the appropriate message to the user
* after the registration attempt. It displays a 
* success or failure status depending on a
* session variable set during registration.
*/
function displayStatus(){
   $uname = $_SESSION['reguname'];
   if($_SESSION['regresult']){
?>

<h1>Registered!</h1>
<p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p>

<?
   }
   else{
?>

<h1>Registration Failed</h1>
<p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>,<?echo $result;?> could not be completed.<br>
Please try again at a later time.</p>

<?
   }
   unset($_SESSION['reguname']);
   unset($_SESSION['registered']);
   unset($_SESSION['regresult']);
}

if(isset($_SESSION['registered'])){
/**
* This is the page that will be displayed after the
* registration has been attempted.
*/
?>

<html>
<title>Registration Page</title>
<body>

<? displayStatus(); ?>

</body>
</html>

<?
   return;
}

/**
* Determines whether or not to show to sign-up form
* based on whether the form has been submitted, if it
* has, check the database for consistency and create
* the new account.
*/
if(isset($_POST['subjoin'])){
   /* Make sure all fields were entered */
   if(!$_POST['user'] || !$_POST['pass']){
      die('You didn\'t fill in a required field.');
   }

   /* Spruce up username, check length */
   $_POST['user'] = trim($_POST['user']);
   if(strlen($_POST['user']) > 30){
      die("Sorry, the username is longer than 30 characters, please shorten it.");
   }

   /* Check if username is already in use */
   if(usernameTaken($_POST['user'])){
      $use = $_POST['user'];
      die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
   }

   /* Add the new account to the database */
   $md5pass = md5($_POST['pass']);
   $_SESSION['reguname'] = $_POST['user'];
   $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass);
   $_SESSION['regresult'] = useradd($_POST['user'], $md5pass);
   $_SESSION['registered'] = true;
   echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[php_SELF]\">";
   return;
}
else{
/**
* This is the page with the sign-up form, the names
* of the input fields are important and should not
* be changed.
*/
?>

<html>
<title>Registration Page</title>
<body>
<h1>Register</h1>
<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr>
</table>
</form>
</body>
</html>


<?
}
?>

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.