Jump to content

Simple PHP Form to insert into MySQL


spinner0205

Recommended Posts

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", "admin@tag-clan.com", 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.  :)  :D

Link to comment
Share on other sites

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
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.