Jump to content

Recieving Data from a form


Zoud

Recommended Posts

I have a form were people can create accounts on my site. When I enter the page, it will insert a user as ("", "") before any information is submitted to the page.

Basicly what I need is a small add-on to this code that will not let it activate without information being submitted to it first.

[code]<?php
$con = mysql_connect("-","-","-");
if (!$con)
  {
  die('Error: ' . mysql_error());
  }

mysql_select_db("members", $con);

$sql="INSERT INTO person (username, password)
VALUES
('$_POST[regun]','$_POST[regpw]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Your account has been created.";

mysql_close($con)
?>

<form action="register.php" method="post">
Username: <input type="text" name="regun" />
Password: <input type="text" name="regpw" />
<input type="submit" />
</form>[/code]

On another note, could someone tell me how to encrypt passwords on the DB when submitted?
Link to comment
https://forums.phpfreaks.com/topic/34263-recieving-data-from-a-form/
Share on other sites

You need to correct a few things like this:

[code]
<?php
$con = mysql_connect("-","-","-");
if (!$con)
  {
  die('Error: ' . mysql_error());
  }

mysql_select_db("members", $con);
if(isset($_POST[submit])){
$sql="INSERT INTO person (username, password)
VALUES
('$_POST[regun]','$_POST[regpw]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Your account has been created.";
}else{
echo "Didnt work!";
}
mysql_close($con)
?>

<form action="register.php" method="post">
Username: <input type="text" name="regun" />
Password: <input type="text" name="regpw" />
<input type="submit" name="submit" />
</form>
[/code]
There are two ways to crypt password with md5() or crypt()... md5() is a bit more safe. but here you go:

[code]
<?php
$con = mysql_connect("-","-","-");
if (!$con)
  {
  die('Error: ' . mysql_error());
  }

mysql_select_db("members", $con);
if(isset($_POST[submit])){
$crypted_pass = md5($_POST[regpw]);
$sql="INSERT INTO person (username, password)
VALUES
('$_POST[regun]','$crypted_pass')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Your account has been created.";
}else{
echo "Didnt work!";
}
mysql_close($con)
?>

<form action="register.php" method="post">
Username: <input type="text" name="regun" />
Password: <input type="text" name="regpw" />
<input type="submit" name="submit" />
</form>
[/code]

that should do it.

Archived

This topic is now archived and is closed to further replies.

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