Jump to content

comparing input


scmeeker

Recommended Posts

In my form, I want to compare passwords to each other before inserting a new record.  I can't seem to get this going.

 

Here is the code from the form:

 

<tr>
    <td><span class="blackfont">Password:</span></td>
    <td><input type="password" name="password" /> </td>
  </tr>
  <tr>
    <td><span class="blackfont">Reenter Password:</span> </td>
    <td><input type="password" name="checkpassword" /></td>
  </tr>
  <tr>

 

And here is the form's "<form action="insert5.php" method="post">"  the "insert5.php" code that goes with the form:

 

<?php
$con = mysql_connect("","","", "");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$test_name = "password";
$sub_name = "checkpassword";

while ($test_name == $sub_name) {

mysql_select_db("lery", $con);

$sql="INSERT INTO artist (username, password, email, terms)
VALUES
('$_POST[username]','$_POST[password]','$_POST[email]', '$_POST[terms]')";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
}


mysql_close($con)
?> 

Link to comment
https://forums.phpfreaks.com/topic/205475-comparing-input/
Share on other sites

You don't want to do this:

<?php
$test_name = "password";
$sub_name = "checkpassword";

while ($test_name == $sub_name) {
?>

do this instead:

<?php
$test_name = $_POST['password'];
$sub_name = $_POST['checkpassword'];

if($test_name == $sub_name) {
?>

 

You should also use the function mysql_real_escape_string on any data you insert into the database. Plus you should store the password in plain text in the database:

 

<?php
$sql="INSERT INTO artist (username, password, email, terms) VALUES ('" . mysql_real_escape_string($_POST[username]) . "','" . md5($_POST[password]) . "','" . mysql_real_escape_string($_POST[email]) . "', '" . mysql_real_escape_string($_POST[terms]) . "')";
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/205475-comparing-input/#findComment-1075274
Share on other sites

you could do something like this:

 

 

//this goes on your insert5.php page

<?php

session_start();

 

if ( $_POST['password'] != $_POST['checkpassword'] )

{

    $_SESSION['passfail'] = 1;

    header("Location: http://www.yoursite.com/formpage.php");

}

 

?>

 

//this is your form page

<?php

session_start();

 

if ( $_SESSION['passfail'] != '' )

{

  echo 'Passwords don't match.';

  unset($_SESSION['passfail']);

}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/205475-comparing-input/#findComment-1075297
Share on other sites

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.