jimlawrnc Posted November 2, 2007 Share Posted November 2, 2007 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; Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 2, 2007 Share Posted November 2, 2007 You never really stated the problem. Whats up with this in your query? `TechHours`.`members` What exactly is the tables name? You also have a bunch of backslashes in there that don't need to be there. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 2, 2007 Share Posted November 2, 2007 whats the issue? Quote Link to comment Share on other sites More sharing options...
jimlawrnc Posted November 2, 2007 Author Share Posted November 2, 2007 when i browsethe adduser.php file i enter a name and password this should call the addusertodb.php and insert the user into the table hours I see i also did add include('db.php'); to my addusertodb.php file Quote Link to comment Share on other sites More sharing options...
jimlawrnc Posted November 2, 2007 Author Share Posted November 2, 2007 Database name TechUsers Tablename members Quote Link to comment Share on other sites More sharing options...
peranha Posted November 2, 2007 Share Posted November 2, 2007 what is with the techhours in your example though Quote Link to comment Share on other sites More sharing options...
jimlawrnc Posted November 2, 2007 Author Share Posted November 2, 2007 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; ?> Quote Link to comment Share on other sites More sharing options...
atlanta Posted November 2, 2007 Share Posted November 2, 2007 the way he wrote the query is just the correct longer way of writing the sql query thats all Quote Link to comment Share on other sites More sharing options...
peranha Posted November 2, 2007 Share Posted November 2, 2007 Database name TechUsers Tablename members Here he put TechUsers as database name, just woundering if this is correct, or a mistake. Quote Link to comment Share on other sites More sharing options...
jimlawrnc Posted November 2, 2007 Author Share Posted November 2, 2007 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 Quote Link to comment Share on other sites More sharing options...
jimlawrnc Posted November 2, 2007 Author Share Posted November 2, 2007 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 Quote Link to comment Share on other sites More sharing options...
marcus Posted November 2, 2007 Share Posted November 2, 2007 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. Quote Link to comment Share on other sites More sharing options...
revraz Posted November 2, 2007 Share Posted November 2, 2007 $query = "INSERT INTO members (ID, username, user_password) VALUES (NULL, '$_POST[username]','$_POST[password]' )"; Remove ID and NULL from this *seconds too late Quote Link to comment Share on other sites More sharing options...
jimlawrnc Posted November 2, 2007 Author Share Posted November 2, 2007 well then interesting that no database is selected When run INSERT INTO members (ID, username, user_password) VALUES ('poky1','poky' )The insert query failed because: No database selected Quote Link to comment Share on other sites More sharing options...
marcus Posted November 2, 2007 Share Posted November 2, 2007 I don't see you including the files on your add user page. Quote Link to comment Share on other sites More sharing options...
jimlawrnc Posted November 2, 2007 Author Share Posted November 2, 2007 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? Quote Link to comment Share on other sites More sharing options...
marcus Posted November 2, 2007 Share Posted November 2, 2007 ... or die(mysql_error()); Use that instead of saying it failed. Quote Link to comment Share on other sites More sharing options...
revraz Posted November 2, 2007 Share Posted November 2, 2007 And the members Table contains the rows username and user_password? Check for typos Quote Link to comment Share on other sites More sharing options...
jimlawrnc Posted November 2, 2007 Author Share Posted November 2, 2007 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 Quote Link to comment Share on other sites More sharing options...
jimlawrnc Posted November 2, 2007 Author Share Posted November 2, 2007 ok here is something weird the users are getting inserted into the table but the error message still appears. Second the password is not getting encryped with sha1 encryption I guess i need to add a function to do that? Quote Link to comment Share on other sites More sharing options...
jimlawrnc Posted November 2, 2007 Author Share Posted November 2, 2007 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 Quote Link to comment Share on other sites More sharing options...
jimlawrnc Posted November 2, 2007 Author Share Posted November 2, 2007 This is so weird that error above but the account is added seconf the sha1 is not encrypting the password. d i need to add something? Quote Link to comment Share on other sites More sharing options...
revraz Posted November 2, 2007 Share Posted November 2, 2007 $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. Quote Link to comment Share on other sites More sharing options...
jimlawrnc Posted November 3, 2007 Author Share Posted November 3, 2007 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"; 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.