-2

First of all, ive been informed that ive asked a duplicate question here, but no. Same piece of code yes, but a different problem ive run into. Secondly, ill also add my HTML form, which i didnt think might contribute to my error. None of my form data, apart from fname/lname, is saving/showing in my database,when i view it through wamp server SQL console. Additionally, i am only allowed to add one row of data to my table, then my table query continuously dies when i attempt to add more rows. Any suggestions where im going wrong?

<?php 

 require_once 'log.php';

 $salt1 = "qm&h*";
 $salt2 = "pg!@";

 $user = isset($_POST['user']) ? $_POST['user'] : '';
 $password = isset($_POST['password']) ? $_POST['password'] : '';
 $vpassword = isset($_POST['vpassword']) ? $_POST['vpassword'] : '';
 $email = isset($_POST['email']) ? $_POST['email'] : '';
 $fname = isset($_POST['fname']) ? $_POST['fname'] : '';
 $lname =isset($_POST['lname']) ? $_POST['lname'] : '';
 $gend = isset($_POST['gend']) ? $_POST['gend'] : '';

 $token  = md5("$salt1$password$salt2");

$query = "INSERT INTO members(user,password,vpassword,fname,lname,email,gend) VALUES('$user','$password','$vpassword','$fname','$lname','$email','$gend')";
$result = mysqli_query($conn, $query);
if (!$result) die ("Database access failed: " . mysql_error());
echo "You have been successfully registered";
echo '<br /> <br /><a href ="index.php" >Return to home page</a>';

mysqli_close($conn);

?>

HTML page

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

  <title>Registration</title>
  <link type="text/css" rel="stylesheet" href="style.css" />
  <script type="text/javascript" src="twitch_val.js"></script>
</head>
<body>
 <div id="d1">
         <img src="bann.png" style="float:right;" alt="ban"/><br/>
         <h1>Chaguramas <br/>Twitchers <br/>Collective</h1>
    </div>
 <div id="d4">
  <ul>
  <li><a href="index.php">Home</a></li>
  <li><a href="Login.php">Login</a></li>
  <li><a href="Regis.php">Register</a></li>
  <li><a href="contact.php">Contact Us</a></li>
  <li> <form method="post" action="" name="Search" id="search" ><fieldset>
  <input type="text" name="sitesearch"/></fieldset><!--Search bar included in ul to keep inline with div container-->
</form></li>
</ul>
 </div> 
 <div id="d5">
         <div id="f4"> <h1>Register  <img src="fea2.png" alt="fe"/></h1></div>
         <form method="post" action="Regis_populate.php" name="Regis" id="regis" onsubmit="validation();"><fieldset>
         <br/>
         <h2>Account Information</h2>   <!--Registration split between account items and personal items-->
Username:<input type="text" name="user" id="user"/>
<br/><br/>
Password:<input type="password" name="password" id="password"/>
<br/><br/>
Verify Password:<input type="password" name="vpassword" id="vpassword"/>
<br/><br/>
Email:<input type="text" name="email" id="email"/>
<br/><br/></fieldset>
</form>
    </div>
<div id="d6">
       <form method="post" action="Regis_populate.php" name="Regis" id="regis" onsubmit="validation();"><fieldset>
         <h2>Personal Information</h2>
First name:<input type="text" name="fname" id="fname"/> Last name:<input type="text" name="lname" id="lname"/>
<br/><br/>
Gender:<input type="radio" name="sex1" id="gend"/>Male
<input type="radio" name="sex2" id="gend"/>Female
<br/><br/>


<img src="cap.jpg" id="captchaimg" alt="cpa"/>  <!--CAPCHA code borrowed from  http://webdesignpub.com/html-contact-form-captcha/-->
<br/>
<label for="message">Enter the code above here to complete registration:</label><br/>
<input id="letters_code" name="letters_code" type="text"/>
<br/><br/>
<input type="submit" value="Submit" id="Submit" onclick="validation();"/>
<br/><br/></fieldset>
 </form>
</div>





</body>
</html>

Javascript

function validation(){

    var us = document.getElementById("user").value; 
    var ps = document.getElementById("password").value;
    var vps = document.getElementById("vpassword").value;
    var em = document.getElementById("email").value;    
    var fn = document.getElementById("fname").value;
    var ln = document.getElementById("lname").value;


    var us_names= new RegExp (/[a-zA-Z0-9._-]$/);
    var pass = new RegExp (/[a-zA-Z0-9._-]$/);
    var names = new RegExp (/[a-zA-Z]$/);
    var email = new RegExp (/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/);

    var u = document.forms["Regis"]["user"].value;
    if (u == null || u == "") {
        alert("Please enter a user name");
        return false;
    }
    var user = us_names.test(us);
    if (user == false){
        alert("Invalid user name. Please re-enter.");
        return false;
    }

    var p = document.forms["Regis"]["password"].value;
    if (p == null || p == "") {
        alert("Please enter a password");
        return false;
    }
    var passwrd = pass.test(ps);
    if (passwrd == false){
        alert("Invalid password. Please re-enter.");
        return false;   
    }

    var vp = document.forms["Regis"]["vpassword"].value;
    if (vp == null || vp == "") {
        alert("Please enter identical password");
        return false;
    }
    if (vps != ps){
        alert("Passwords not identical. Please re-enter");
        return false;   
    }

    var e = document.forms["Regis"]["email"].value;
    if (e == null || e == "") {
        alert("Please enter an email address");
        return false;
    }


    var f = document.forms["Regis"]["fname"].value;
    if (f == null || f == ""){
        alert("Please enter your first name");
        return false;
    }
    var fname = names.test(fn);
    if (fname == false){
        alert(" First name entered cannot be recorded. Please re-enter");
        return false;
    }

    var l = document.forms["Regis"]["lname"].value;
    if (l == null || l == ""){
        alert("Please enter your last name");
        return false;
    }
    var lname = names.test(nn);
    if (lname == false){
        alert("Last name entered cannot be recorded. Please re-enter");
        return false;
    }
    if(document.Regis.sex1.checked == false && document.Regis.sex2.checked == false){
        alert("Please select a gender. Either male or female");
        return false;
    }
}
Sean Marc
  • 13
  • 3
  • 1
    `echo` out `$query`, and see what's query is running on the database, and make sure it's expected. Add a `print_r($_POST)` near the top, and make sure the form is returning the data you want to answer. – Blue May 19 '15 at 01:37
  • Please learn to use prepares statements instead of substituting variables. – Barmar May 19 '15 at 01:38
  • you posted this already http://stackoverflow.com/q/30314687/ please don't do that. the duplicate question contains answers you can base yourself on to fix your code. – Funk Forty Niner May 19 '15 at 01:38
  • @Fred-ii- That question was about the undefined index errrors, which he fixed by adding all the `isset()` checks. – Barmar May 19 '15 at 01:39
  • Same piece of code, but a different problem i ran into, i apologize if it comes across as a bother however. – Sean Marc May 19 '15 at 01:40
  • 1
    sidenote: this `mysql_error()` doesn't help you. it needs to be `mysqli_error($conn)` – Funk Forty Niner May 19 '15 at 01:41
  • If you want to be able to send multiple rows of data in your form, you need to give the form fields names like `name=user[]`. The `[]` tells PHP to make an array out of all the inputs, and then you need to use a loop in your PHP. – Barmar May 19 '15 at 01:41
  • 1
    @SeanMarc Please update the question with the HTML of the form. Then maybe we can reopen this question. Right now, there's just not enough detail to be able to help you. – Barmar May 19 '15 at 01:44
  • additionally, you're not doing anything with `$token` adding more mystery to your question – Funk Forty Niner May 19 '15 at 01:45
  • Ive made edits, please let me know if anything else is needed. But like i said, same piece of script, but different error. I wouldnt ask the same question twice, id prefer not to risk being banned. – Sean Marc May 19 '15 at 01:46
  • I've reopened the question. Next time, please post all relevant code. thank you – Funk Forty Niner May 19 '15 at 01:49
  • for one thing, check your radio button name attributes; they're wrong or there's nothing in your JS like the others ones. Id's are unique `id="gend"` you have 2 – Funk Forty Niner May 19 '15 at 01:55
  • *"Additionally, i am only allowed to add one row of data to my table"* - then, this tells me you've set a constraint somewhere (UNIQUE, etc.). Using `mysqli_error($conn)` rather than what you have now `mysql_error()` (which doesn't help you, nor does it work with `mysqli_`), will signal the possible errors. You've enough to debug your code now. Plus, instead of jumping in the deep end right away, it's best to slowly build your code, rather than relying on immediate success. Good luck. – Funk Forty Niner May 19 '15 at 02:04
  • I gave them separate names to account for them being checked in the validation JS. ' if(document.Regis.sex1.checked == false && document.Regis.sex2.checked == false){ alert("Please select a gender. Either male or female"); return false; }' Or am i wrong in understanding that document.Regis.sex1/sex2.checked, doesnt require the name attribute. – Sean Marc May 19 '15 at 02:05

1 Answers1

2

After all this time, I finally found what the problem is.

You're using two seperate forms (3 technically), which some of the elements are getting lost, being an out of scope issue.

Place all your form elements inside one set of <form></form> tags.

  • You're also mixing APIs using mysql_error() which should read as mysqli_error($conn) and failing to see where probable DB errors may be occurring.
  • If you've set a constraint somewhere, then that will be the reason why you are unable to add more rows.

Sidenote: Your present code is open to SQL injection. Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.


Regarding password storage:

I noticed you are using MD5. This hashing method is old and considered broken and is no longer considered safe to use.

I recommend you use CRYPT_BLOWFISH or PHP 5.5's password_hash() function. For PHP < 5.5 use the password_hash() compatibility pack.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Oh my god, thank you so much. I was losing my mind here. Seriously losing it. Also i do apologize for the earlier fuss. It wasnt my intention. – Sean Marc May 19 '15 at 02:24