spinner0205 Posted December 16, 2010 Share Posted December 16, 2010 Alright so I am trying to code a very simple form that adds a user to my SQL database with certain information filled in and certain information kept hidden with default values in a form. My index.php file is as follows (not all of the code but just the form part): <?PHP $submitMessage = $_GET['value']; if ($submitMessage == success) { echo '<div class="congratulations"><font color="#84d20c"><strong>Congratulations:</strong></font> The user has been added.</div>'; }else if($submitMessage == NULL){ }else if($submitMessage == fail){ echo '<div class="error"><font color="#d3430c"><strong>Error:</strong></font> An error occurred please check the error details below:</div>'; echo 'ERROR DETAILS:'; echo mysql_error(); } ?> <h1><i>Add User</i></h1> <p>This page is where you would manually add a user to the donations databases (for example you or a member of the community with elevated status that does not need to pay).</p> <br/> <form enctype="multipart/form-data" action="scripts/admin_save_sm_db.php" method="post"> <input name="authtype" value="steam"/><br /><br /> <h3><i>STEAM ID:</i></h3> <input name="identity" value="STEAM_0:X:XXXXXXXX"/><br /> <div class="form_help">Steam ID of the player you wish to add. Must be in the format STEAM_0:X:XXXXXXXX</div><br /> <input name="flags" value="o"/><br /><br /> <input name="name" value="Donator"/><br /><br /> <input name="immunity" value="0"/><br /><br /> <h3><i>Your Email Address</i></h3> <input name="user_email"/><br /> <div class="form_help">Your Email Address for security purposes.</div><br /> <input name="Submit" type="submit" value="Submit" class="form_submit" /> </form> My admin_save_sm_db.php file is as follows: <?php session_start(); if(!isset($_SESSION["username"])) { header('Location: login.php'); exit; } @require("../sm_admin_db.php"); $authtype = $_POST['authtype']; $identity = $_POST['identity']; $flags = $_POST['flags']; $name = $_POST['name']; $immunity = $_POST['immunity']; $user_email = $_POST['user_email']; $link = @mysql_connect(_HOST,_USER,_PASS); @mysql_select_db(_DB); $sql = @mysql_db_query(_DB,"INSERT INTO "._TBL." (`authtype`, `identity`, `flags`, `name`, `immunity`, `user_email`) VALUES (NULL, '$authtype', '$identity', '$flags', '$name', '$immunity', '$user_email')"); $okay = @mysql_affected_rows(); if($okay > 0) { header( "Location: ../index.php?value=success" ); } else { header( "Location: ../index.php?value=fail" ); } @mysql_close($link); ?> And finally my sm_admin_db.php file is as follows: <?PHP //host loaction define("_HOST", "localhost", TRUE); //database username define("_USER", "thatsact_admin", TRUE); //database username`s password define("_PASS", "*********", TRUE); //database name define("_DB", "thatsact_sadadmins", TRUE); //database table name change this to install multiple version on same database define("_TBL", "sm_admins", TRUE); //This is the email that you will recieve emails when someone signs up. define("_EMAIL", "[email protected]", TRUE); ?> All of this together does not work and gives the echo error code, however the mysql_error() string does not return anything. Nothing is inserted into the database. Can I get some help on where I went wrong and please keep in mind I am VERY new to php and didn't write most of this script so just tell me what to edit and exactly how to do it please. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/221905-simple-php-form-to-insert-into-mysql/ Share on other sites More sharing options...
sloth456 Posted December 16, 2010 Share Posted December 16, 2010 Ok firstly: Your index.php file does not perform any sql operations, it doesn't do the database stuff so the echo mysql_error(); line won't show you anything. So you may as well delete it or comment it out like this //echo mysql_error(); Next, you need to see whats going wrong with your sql so in admin_save_sm_db.php we need to remove the '@' symbols. The '@' symbols supress errors, by removing them we'll see what's going wrong. So here's an updated version. <?php session_start(); if(!isset($_SESSION["username"])) { header('Location: login.php'); exit; } @require("../sm_admin_db.php"); $authtype = $_POST['authtype']; $identity = $_POST['identity']; $flags = $_POST['flags']; $name = $_POST['name']; $immunity = $_POST['immunity']; $user_email = $_POST['user_email']; $link = mysql_connect(_HOST,_USER,_PASS); mysql_select_db(_DB); $sql = mysql_db_query(_DB,"INSERT INTO "._TBL." (`authtype`, `identity`, `flags`, `name`, `immunity`, `user_email`) VALUES (NULL, '$authtype', '$identity', '$flags', '$name', '$immunity', '$user_email')") OR die(mysql_error()); $okay = mysql_affected_rows(); if($okay > 0) { header( "Location: ../index.php?value=success" ); } else { header( "Location: ../index.php?value=fail" ); } mysql_close($link); ?> you'll have noticed I also added some extra code on your insert query OR die(mysql_error()) Now if for some reason that insert query doesn't work the script will terminate and show you an error from which we can hopefully figure out your problem. Make these changes, run it all and post back what errors come up. Link to comment https://forums.phpfreaks.com/topic/221905-simple-php-form-to-insert-into-mysql/#findComment-1148401 Share on other sites More sharing options...
spinner0205 Posted December 17, 2010 Author Share Posted December 17, 2010 It returned with this: Column count doesn't match value count at row 1 Link to comment https://forums.phpfreaks.com/topic/221905-simple-php-form-to-insert-into-mysql/#findComment-1148432 Share on other sites More sharing options...
Pikachu2000 Posted December 17, 2010 Share Posted December 17, 2010 Remove NULL, from VALUES( NULL, '$authtype', etc. Link to comment https://forums.phpfreaks.com/topic/221905-simple-php-form-to-insert-into-mysql/#findComment-1148434 Share on other sites More sharing options...
spinner0205 Posted December 17, 2010 Author Share Posted December 17, 2010 Awesome, works perfect now thank you guys. Link to comment https://forums.phpfreaks.com/topic/221905-simple-php-form-to-insert-into-mysql/#findComment-1148436 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.