neverforget98 Posted November 3, 2012 Share Posted November 3, 2012 I just made a topic a little big ago about another error I was getting but now I'm getting another one...well...this time I'm not even getting any error, no matter what I do. The script says that the "User was sucessfully added" but when I check the database and return to the user list, the new user doesn't show up and isn't actually in the database. Can somebody help? I have included the code below! Thanks. <?php session_start(); require("inc/mysql.php"); require("inc/Membership.class.php"); $Member = new Membership($DBH); require("inc/membership.php"); //test user permissions if(!$Member->test_perms(2)) { //No perms, echo error or forward or something header( 'Location: nopermission.php' ) ; } ?> <html> <head> <title>Users | OUB's Integrated Services</title> <meta name="description" content=""> <meta name="author" content="Brandin Arsenault, OUB"> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <script src="./ajax/libs/jquery/1/jquery.min.js"></script> <link rel="stylesheet" href="css/960.css" /> <link rel="stylesheet" href="style.css" /> <script src="js/scripts.js"></script> <script type="text/javascript" language="javascript" src="jquery-1.2.6.min.js"></script> <div id="message_box"> <?php { echo "Hello, " . $Member->get_field("real_name"); }?> </div> </head> <body> <br /><br /><br /><br /> <?php include 'header.php'; ?> <br /><br /> <center><h5>INTEGRATED SERVICES USERS</center> <br /> <center> <?php //including the database connection file include_once("inc/usersdb.php"); ?> <form method="post"> <table> <tr> <td>Username:</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="passwordHash" /></td> </tr> <tr> <td>Real Name (Last, First):</td> <td><input type="text" name="real_name" /></td> </tr> <tr> <td>Position:</td> <td><select name="position"> <option value="Founding President">Founding President</option> <option value="Relief President">Relief President</option> <option value="Social Media Manager">Social Media Manager</option> <option value="Other">Other</option></td> </tr> <tr> <td>Email:</td> <td><input type="text" name="email" /></td> </tr> <tr> <td>Alternate Email:</td> <td><input type="text" name="altemail" /></td> </tr> <tr> <td>Home Number (XXX-XXX-XXXX):</td> <td><input type="text" name="hnumber" /></td> </tr> <tr> <td>Mobile Number (XXX-XXX-XXXX):</td> <td><input type="text" name="mnumber" /></td> </tr> <tr> <td>System Username:</td> <td><input type="text" name="sysusername" /></td> </tr> <tr> <td>System Password:</td> <td><input type="password" name="syspassword" /></td> </tr> <tr> <td>Non-Disclosure Agreement:</td> <td><select name="nondisclosure"> <option value="Yes">Yes</option> <option value="No">No</option></td> </tr> <tr> <td>Proper Conduct Agreement:</td> <td><select name="properconduct"> <option value="Yes">Yes</option> <option value="No">No</option></td> </tr> <tr> <td>Terms of Use:</td> <td><select name="termsofuse"> <option value="Yes">Yes</option> <option value="No">No</option></td> </tr> <tr> <td>Permissions (ROOT USER ONLY):</td> <td><input type="text" name="perms" value="1.0.1" /></td> </tr> <tr> <td> </td> <td><br/ ><input type="submit" name="submit" value="Add User" /></td> </tr> </table> </form> <?php Echo mysql_error() ?> <?php if(isset($_POST['submit'])) { $username=$_POST['username']; $passwordHash=$_POST['passwordHash']; $real_name=$_POST['real_name']; $position=$_POST['position']; $email=$_POST['email']; $altemail=$_POST['altemail']; $hnumber=$_POST['hnumber']; $mnumber=$_POST['mnumber']; $sysusername=$_POST['sysusername']; $syspassword=$_POST['syspassword']; $nondisclosure=$_POST['nondisclosure']; $properconduct=$_POST['properconduct']; $termsofuse=$_POST['termsofuse']; $perms=$_POST['perms']; // checking empty fields if(empty($username) || empty($passwordHash) || empty($real_name)) { //if name field is empty if(empty($username)) { echo "<font color='red'>Username field is empty.</font><br/>"; } //if age field is empty if(empty($passwordHash)) { echo "<font color='red'>Password field is empty.</font><br/>"; } //if email field is empty if(empty($real_name)) { echo "<font color='red'>Name field is empty.</font><br/>"; } //link to the previous page echo "<br/><a href='javascript:self.history.back();'>Go Back</a>"; } else // if all the fields are filled (not empty) { //insert data to database $result=mysql_query("INSERT INTO users(username,passwordHash,real_name,position,email,altemail,hnumber,mnumber,sysusername,sysnumber,nondisclosure,properconduct,termsofuse,perms) VALUES('$username','$passwordHash','$real_name','$position','$email','$altemail','$hnumber','$mnumber','$sysusername','$syspassword','$nondisclosure','$properconduct','$termsofuse','$perms')"); //display success message echo "<font color='green'>User account added successfully."; echo "<br/><a href='ausers.php'>Return to Users List</a>"; } } ?> </center> <?php include 'logout.php'; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/270230-using-php-to-insert-into-database-no-data-going/ Share on other sites More sharing options...
JohnTipperton Posted November 3, 2012 Share Posted November 3, 2012 you use only mysql_query without a connection to database like using mysql_connect Quote Link to comment https://forums.phpfreaks.com/topic/270230-using-php-to-insert-into-database-no-data-going/#findComment-1389814 Share on other sites More sharing options...
neverforget98 Posted November 3, 2012 Author Share Posted November 3, 2012 (edited) you use only mysql_query without a connection to database like using mysql_connect There is a line in the script (50-53): <?php //including the database connection file include_once("inc/usersdb.php"); ?> That opens the config file for the database connection...so that launches the connection at the beginning of the file. Shouldn't this be enough? (If I'm wrong, please correct me and let me know what I have to add, thanks!) Edited November 3, 2012 by neverforget98 Quote Link to comment https://forums.phpfreaks.com/topic/270230-using-php-to-insert-into-database-no-data-going/#findComment-1389815 Share on other sites More sharing options...
JohnTipperton Posted November 3, 2012 Share Posted November 3, 2012 (edited) try this $result="INSERT INTO users(username,passwordHash,real_name,position,email,altemail,hnumber,mnumber,sysusername,sysnumber,nondisclosure,properconduct,termsofuse,perms) VALUES('$username','$passwordHash','$real_name','$position','$email','$altemail','$hnumber','$mnumber','$sysusername','$syspassword','$nondisclosure','$properconduct','$termsofuse','$perms')"; mysql_query($result,$con); the $con is the connection from the database its up to you on how to declare it Edited November 3, 2012 by JohnTipperton Quote Link to comment https://forums.phpfreaks.com/topic/270230-using-php-to-insert-into-database-no-data-going/#findComment-1389817 Share on other sites More sharing options...
neverforget98 Posted November 3, 2012 Author Share Posted November 3, 2012 try this $result="INSERT INTO users(username,passwordHash,real_name,position,email,altemail,hnumber,mnumber,sysusername,sysnumber,nondisclosure,properconduct,termsofuse,perms) VALUES('$username','$passwordHash','$real_name','$position','$email','$altemail','$hnumber','$mnumber','$sysusername','$syspassword','$nondisclosure','$properconduct','$termsofuse','$perms')"; mysql_query($result,$con); the $con is the connection from the database its up to you on how to declare it So, how would I represent $con in my file? If I'm including another file how would I represent it? How would I represent it if I had it IN the file? Sorry, I'm not really good with this. /: As much as I think I am, haha. Quote Link to comment https://forums.phpfreaks.com/topic/270230-using-php-to-insert-into-database-no-data-going/#findComment-1389818 Share on other sites More sharing options...
PFMaBiSmAd Posted November 3, 2012 Share Posted November 3, 2012 You are unconditionally outputting the success message without testing if the query did anything. Since you are assigning the result from the query to $result, add the following code to test what the query actually did - if(!$result){ // query failed with an error of some kind, do some error reporting here - die('INSERT query failed. ' . mysql_error()); } else { // query executed, test if it actually inserted the row if(!mysql_affected_rows()){ // row was not inserted. This is rare and would generally require the use of the IGNORE keyword in the query to cause a query error to become a warning only. die('INSERT query ran, but did not insert the row.'); } else { // query ran and did insert the row - echo "<font color='green'>User account added successfully."; echo "<br/><a href='ausers.php'>Return to Users List</a>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/270230-using-php-to-insert-into-database-no-data-going/#findComment-1389819 Share on other sites More sharing options...
JohnTipperton Posted November 3, 2012 Share Posted November 3, 2012 (edited) $con = mysql_connect("localhost","mysql_user","mysql_pwd");if (!$con) { die('Could not connect: ' . mysql_error()); } Edited November 3, 2012 by JohnTipperton Quote Link to comment https://forums.phpfreaks.com/topic/270230-using-php-to-insert-into-database-no-data-going/#findComment-1389820 Share on other sites More sharing options...
PFMaBiSmAd Posted November 3, 2012 Share Posted November 3, 2012 P.S. Your login check/permission code is not secure and won't stop anyone from accessing your 'protected' pages. You need an exit; statement after the header() redirect to prevent the rest of the code on your page from running while the browser is requesting the new page in the the redirect. Quote Link to comment https://forums.phpfreaks.com/topic/270230-using-php-to-insert-into-database-no-data-going/#findComment-1389821 Share on other sites More sharing options...
neverforget98 Posted November 3, 2012 Author Share Posted November 3, 2012 $con = mysql_connect("localhost","mysql_user","mysql_pwd");if (!$con) { die('Could not connect: ' . mysql_error()); } I have included both of your codes and PFMaBiSmAd's and it's still saying success but it's not going in... Quote Link to comment https://forums.phpfreaks.com/topic/270230-using-php-to-insert-into-database-no-data-going/#findComment-1389827 Share on other sites More sharing options...
PFMaBiSmAd Posted November 3, 2012 Share Posted November 3, 2012 ^^^ I don't believe you What is your current code? Quote Link to comment https://forums.phpfreaks.com/topic/270230-using-php-to-insert-into-database-no-data-going/#findComment-1389837 Share on other sites More sharing options...
neverforget98 Posted November 3, 2012 Author Share Posted November 3, 2012 ^^^ I don't believe you What is your current code? I'm getting no database selected error How do I select a database in one of my codes? This is my connection code: $con = mysql_connect("localhost","root","");if (!$con) { die('Could not connect: ' . mysql_error()); } And this is the other part of the code: <?php if(isset($_POST['submit'])) { $username=$_POST['username']; $passwordHash=$_POST['passwordHash']; $real_name=$_POST['real_name']; $position=$_POST['position']; $email=$_POST['email']; $altemail=$_POST['altemail']; $hnumber=$_POST['hnumber']; $mnumber=$_POST['mnumber']; $sysusername=$_POST['sysusername']; $syspassword=$_POST['syspassword']; $nondisclosure=$_POST['nondisclosure']; $properconduct=$_POST['properconduct']; $termsofuse=$_POST['termsofuse']; $perms=$_POST['perms']; if(empty($name) || empty($age) || empty($email)) if(empty($username)) { echo "<font color='red'>Username field is empty.</font><br/>"; } if(empty($password)) { echo "<font color='red'>Password field is empty.</font><br/>"; } if(empty($real_name)) { echo "<font color='red'>Name field is empty.</font><br/>"; } else { $result=mysql_query("INSERT INTO users(username,passwordHash,real_name,position,email,altemail,hnumber,mnumber,sysusername,sysnumber,nondisclosure,properconduct,termsofuse,perms) VALUES('$username','$passwordHash','$real_name','$position','$email','$altemail','$hnumber','$mnumber','$sysusername','$syspassword','$nondisclosure','$properconduct','$termsofuse','$perms')"); if(!$result){ // query failed with an error of some kind, do some error reporting here - die('INSERT query failed. ' . mysql_error()); } else { // query executed, test if it actually inserted the row if(!mysql_affected_rows()){ // row was not inserted. This is rare and would generally require the use of the IGNORE keyword in the query to cause a query error to become a warning only. die('INSERT query ran, but did not insert the row.'); } else { // query ran and did insert the row - echo "<font color='green'>User account added successfully."; echo "<br/><a href='ausers.php'>Return to Users List</a>"; } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/270230-using-php-to-insert-into-database-no-data-going/#findComment-1389841 Share on other sites More sharing options...
neverforget98 Posted November 3, 2012 Author Share Posted November 3, 2012 I was able to fix it! Thank you guys for ALL of your help. Quote Link to comment https://forums.phpfreaks.com/topic/270230-using-php-to-insert-into-database-no-data-going/#findComment-1389846 Share on other sites More sharing options...
JohnTipperton Posted November 3, 2012 Share Posted November 3, 2012 I was able to fix it! Thank you guys for ALL of your help. no problem mate Quote Link to comment https://forums.phpfreaks.com/topic/270230-using-php-to-insert-into-database-no-data-going/#findComment-1389885 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.