Jump to content

[SOLVED] add user to table


jimlawrnc

Recommended Posts

2 php files 

 

one gets the info  the other is suposed to add to the table 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Member Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<form name="login-form" id="login-form" method="post" action="addusertodb.php">
<fieldset>
	<legend>Add User</legend>
	<dl>
		<dt><label title="Username">Username: <input tabindex="1" accesskey="u" name="username" type="text" maxlength="100" id="username" /></label></dt>
	</dl>
	<dl>
		<dt><label title="Password">Password: <input tabindex="2" accesskey="p" name="password" type="password" maxlength="14" id="password" /></label></dt>
	</dl>
	<dl>
		<dt><label title="Submit"><input tabindex="3" accesskey="l" type="submit" name="submit" value="Login" /></label></dt>
	</dl>
</fieldset>
</form>

</body>
</html>

 

<?php
function add_user() {


$result = mysql_query(INSERT INTO `TechHours`.`members` (`username` ,`user_password`)VALUES (\'$_POST[\'username\']\', \'$_POST[\'user_password\']\')or die (mysql_error());

print $result
endif;

} // end user_info
?>

 

i know i should check for empty fields but i would like to get it to work 

 

table schema

 

CREATE TABLE IF NOT EXISTS `members` (

`ID` mediumint(5) UNSIGNED NOT NULL AUTO_INCREMENT,

`username` varchar(100) NOT NULL DEFAULT "",

`user_password` char(40) NOT NULL DEFAULT "",

PRIMARY KEY (`ID`, `username`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Link to comment
Share on other sites

its just the name of the database 

 

db.php

 

<?php

define('SQL_USER', 'techuser');

define('SQL_PASS', 'password');

define('SQL_DB',  'TechHours');

 

// Create a link to the database server

$link = mysql_connect('localhost', SQL_USER, SQL_PASS);

if(!$link) :

die('Could not connect: ' . mysql_error());

endif;

 

// Select a database where our member tables are stored

$db = mysql_select_db(SQL_DB, $link);

if(!$db) :

die ('Can\'t connect to database : ' . mysql_error());

endif;

?>

Link to comment
Share on other sites

Here is what I have

 

<form name="login-form" id="login-form" method="post" action="addusertodb.php">
<fieldset>
	<legend>Add User</legend>
	<dl>
		<dt><label title="Username">Username: <input tabindex="1" accesskey="u" name="username" type="text" maxlength="100" id="username" /></label></dt>
	</dl>
	<dl>
		<dt><label title="Password">Password: <input tabindex="2" accesskey="p" name="password" type="password" maxlength="14" id="password" /></label></dt>
	</dl>
	<dl>
		<dt><label title="Submit"><input tabindex="3" accesskey="l" type="submit" name="submit" value="Login" /></label></dt>
	</dl>
</fieldset>
</form>

 

enter  addusertodb.php 

<?php
include('db.php');
function add_user() {


$result = mysql_query(INSERT INTO `TechHours`.`members` (`username` ,`user_password`)VALUES (\'$_POST[\'username\']\', \'$_POST[\'user_password\']\')or die (mysql_error());

print $result
endif;

} // end add_user
?>

 

db.php is listed above

 

 

Link to comment
Share on other sites

Ok I tried some other stuff here but no progress

 

config.php

<?php

$dbhost = 'localhost';
$dbuser = 'techuser';
$dbpass = 'password';
$dbname = 'TechUsers';
?>

opendb.php

<?php
// Connect to the DB 
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>

 

The form to add the user

<?php
if(isset($_POST['add']))
{

include 'config.php';
include 'opendb.php';

$username = $_POST['username'];
$password = $_POST['password'];

$query = "INSERT INTO members (ID, username, user_password) VALUES (NULL, '$_POST[username]','$_POST[password]' )";

echo $query;
mysql_query($query) or die('Error, insert query failed');


$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed');

include 'library/closedb.php';
echo "New MySQL user added";
}
else
{
?>
<form method="post">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr> 
<td width="100">Username</td>
<td><input name="username" type="text" id="username"></td>
</tr>
<tr> 
<td width="100">Password</td>
<td><input name="password" type="text" id="password"></td>
</tr>
<tr> 
<td width="100"> </td>
<td> </td>
</tr>
<tr> 
<td width="100"> </td>
<td><input name="add" type="submit" id="add" value="Add New User"></td>
</tr>
</table>
</form>
<?
}
?>
</body>
</html>

 

When i run the adduser.php 

this gets printed to the screen

INSERT INTO members (ID, username, user_password) VALUES (NULL, 'poky1','poky' )Error, insert query failed

 

I'm at a lost here

Link to comment
Share on other sites

If the field ID is a primary key or auto_increment, don't bother put it in the insert query.

 

$query = "INSERT INTO `members` (`username`,`user_password`) VALUES('poky1','poky');";
mysql_query($query) or die("The insert query failed because: " . mysql_error());

 

give that a go.

Link to comment
Share on other sites

Fixed that  wrong db name  opps 

 

now after that fix i get 

 

INSERT INTO members (username, user_password) VALUES ('poky1','poky' )Error, insert query failed

 

I build my insert query with phpmyadmin  and modify it to fit in php syntax.  maybe thats the problem?

Link to comment
Share on other sites

Table fields

 

Field            Type                  Collation      Attributes  Null  Default  Extra

  ID            mediumint(5)                      UNSIGNED        No          auto_increment 

username      varchar(100)                               latin1_swedish_ci  No

user_password char(40)                               latin1_swedish_ci  No

Link to comment
Share on other sites

spoke to soon...

 

<?php

if(isset($_POST['add']))

{

 

include 'config.php';

include 'opendb.php';

include 'sha1.php';

 

$username = $_POST['username'];

$password1 = $_POST['password'];

$password = sha1($password1);

$query = "INSERT INTO members (username, user_password) VALUES ('$_POST[username]','$_POST[password]' )";

 

echo $query;

mysql_query($query) or die(mysql_error());

 

 

$query = "FLUSH PRIVILEGES";

mysql_query($query) or die(mysql_error());

include 'closedb.php';

echo "New  user added";

}

else

{

?>

 

No errors and no users added how fun...

 

update  now i get

 

INSERT INTO members (username, user_password) VALUES ('humpdy','dumpty' )Access denied; you need the RELOAD privilege for this operation

Link to comment
Share on other sites

$query = "INSERT INTO members (username, user_password) VALUES ('$_POST[username]','$_POST[password]' )";

 

You are inserting the POST variables and not your $variables.

 

$query = "INSERT INTO members (username, user_password) VALUES ($username, $password)";

 

 

Remove or comment this code

 

$query = "FLUSH PRIVILEGES";

mysql_query($query) or die(mysql_error());

 

I think that is where your error is coming from.

Link to comment
Share on other sites

That did the trick

 

I even added password hashing on my own too

 

$username = $_POST['username'];

$password = $_POST['password'];

$passwordHash = sha1($_POST['password']);

 

//Add the new user to the table

$query = "INSERT INTO members (username, user_password) VALUES ('$username', '$passwordHash')";

 

//echo $query;

mysql_query($query) or die (mysql_error());

 

 

 

include 'closedb.php';

echo "New  user added";

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.