Jump to content

[SOLVED] Add new user to database


MadFly
 Share

Recommended Posts

Hi everyoneI have a pretty standard html form, on which i want to add new users to a sql database. Everything works fine, and i am able to add new users. But my problem is with the insert.php file, I would like for it to redirect to index.html after inserting all the data from the form into the database. Now i have done some searching about redirecting a php page, and found that i could use the following

header("Location: index.html");

and a few other variants... But every time i use any thing that might or is suppose to redirect it back to index.html, it gives me an error on the page, and nothing happens.I refreshed the page, but still nothing happens. I tried moving the header code to the top of the insert.php page, and even the very bottom.Here is my new user form, and insert.php code:FORM inside newuser.html

	<form action="insert.php" method="post">Firstname: <input type="text" name="firstname" /> <br />Lastname: <input type="text" name="lastname" /> <br />Age: <input type="text" name="age" /> <br />Adres: <input type="text" name="adres" /> <br />Stad: <input type="text" name="city" /> <br />Epos: <input type="text" name="email" /> <br />Telefoon: <input type="text" name="telefoon" /> <br /><input type="submit" value="Voer data in"/><input type="reset" value="Oopsie"></form>

INSERT.PHP

<?php$con = mysql_connect("localhost","user","passw");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("my_db", $con);$sql="INSERT INTO Clients (FirstName, LastName, Age, Adres, City, Email, Telefoon)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[adres]','$_POST[city]','$_POST[email]','$_POST[telefoon]')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }echo "1 Client succesfully added to database";mysql_close($con)header("Location: index.html");?>

Any ideas on how i will be able to redirect the insert.php page after echoing 1 Client succesfully added to database?And then another question, how can i get the form so that i have to insert a certain password for it to be sucessfull? If no or wrong password is inserted, it does not add anything into database?

Edited by MadFly
Link to comment
Share on other sites

You're missing a semi-colon on mysql_close($con) . Also you cannot echo something out then use header(). You will have to use header before ANYTHING is echoed.If it's just going to be a password that never changes you can code it directly into the PHP code. If your password changes often for different members, then you'll have to use a database.Something like:

<input type="password" name="txtPass" />

PHP:

 if ($txtPass == "MYPASSWORD") {  // do whatever you want}else {  echo "You didn't enter in the correct password.";}

Link to comment
Share on other sites

Thanks, the password option is working.Now just trying to figure out where to put the header in order for it to pause 2 or 5 seconds before it redirects to index.html.the redirect worked when i placed it just before the echo of client added successful, but after i added your password code, and moved the redirect header to infront of that, it gives me error again.and even if i insert wrong code, it still echoes the "1 client added succesfully".How can i make it so that if password correct, it only shows 1 client added... and if password wrong, it ONLY shows that you entered wrong password, but not both.EDIT: I just noticed, that even with wrong password, it still adds the new client into database :-(

Edited by MadFly
Link to comment
Share on other sites

Edited insert.php

<?php$con = mysql_connect("localhost","user","password");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("my_db", $con);$sql="INSERT INTO Kliente (FirstName, LastName, Age, Adres, City, Email, Telefoon)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[adres]','$_POST[city]','$_POST[email]','$_POST[telefoon]')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }header("Location: index.html");echo "1 Client added";mysql_close($con);?>

This works just fine. As it adds the new client, but then does not show the echo of client added. and it redirects IMMEDIATELY.Password effect:

<?php$con = mysql_connect("localhost","user","password");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("my_db", $con);$sql="INSERT INTO Kliente (FirstName, LastName, Age, Adres, City, Email, Telefoon)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[adres]','$_POST[city]','$_POST[email]','$_POST[telefoon]')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }header("Location: index.html");if ($txtPass == "MYPASSWORD") {  // do whatever you want}else {  echo "You didn't enter in the correct password.";}echo "1 Client added";mysql_close($con);?>

This is with you password option in. Now it inserts the new client even with wrong password, but not showing that i entered wrong password or 1 client added

Edited by MadFly
Link to comment
Share on other sites

I would highly discourage the use of waiting for an automatic redirect. Instead, why don't you echo out a link while telling them the user has been added?

if (!mysql_query($sql,$con)) {  {  die('Error: ' . mysql_error());  }else {  echo "1 Client added, Please go <a href=\"index.html\">back</a>.";//  header("Location: index.html");}

To check the password. You need to post the contents from "txtPass".

$txtPass = $_POST['txtPass'];

From the looks of the code, the password check will not do anything. Try something like this instead.

<?php$txtPass = $_POST['txtPass'];if ($txtPass == "MYPASSWORD") {  $con = mysql_connect("localhost","","");  if (!$con)  {	die('Could not connect: ' . mysql_error());  }  mysql_select_db("my_db", $con);  $sql="INSERT INTO Kliente (FirstName, LastName, Age, Adres, City, Email, Telefoon) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[adres]','$_POST[city]','$_POST[email]','$_POST[telefoon]')";  if (!mysql_query($sql,$con))  {	die('Error: ' . mysql_error());  }  else {	echo "1 Client added, Please go <a href=\"index.html\">back</a>.";  }  mysql_close($con);}else {  echo "You didn't enter in the correct password.";}?>

Edited by Err
Link to comment
Share on other sites

Ok, im gonna try that now.would you please remove the username and password for my db from you last post? I forgot to remove it before posting it.i just edited mine now.

Edited by MadFly
Link to comment
Share on other sites

Thank you Plenty!Seems to be working perfectly now! Will later try to turn that back text into a button thingy.but for now its good!And it only inserts the new client if password is correct! :-)thanks again

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...