Jump to content

Help with Insert query


phpnewbphp

Recommended Posts

Hi, 

 

can anyone help me I'm new to coding and have been having trouble getting my code to insert into the database. I have checked the connection and that works fine but it says you're registered but it dosen't input it into the database.

 

This is the code i have in my script:

 

<?php

echo "<h1>Register</h1>";

 

$submit = $_POST ['submit'];

 

//form data

$fullname = strip_tags($_POST ['fullname']);

$username = strip_tags($_POST ['username']);

$password = strip_tags($_POST ['password']);

$repeatpassword = strip_tags($_POST ['repeatpassword']);

$date = date ("Y-m-d");

 

if ($submit)

{

 

//check for existance

if ($fullname&&$username&&$password&&$repeatpassword)

{

 

//check passwords match

if ($password==$repeatpassword)

{

 

//check char length of username and fullname

if (strlen($username)>25||strlen($fullname)>25)

{

echo "Maximum limit for username and fullname is 25 characters!";

}

else

{

 

//check password length

if (strlen($password)>25||strlen($password)<6)

{

echo "Your password must be between 6 and 25 characters!";

}

else

{

 

//register the user

 

//encrypt password

$password = md5 ($password);

$repeatpassword = md5 ($repeatpassword);

 

//open database

 

$connect = mysql_connect ("localhost","","") or die ("couldn't connect!");

 

mysql_select_db ("phplogin") or die ("couldn't find db!");

 

mysql_query ("INSERT INTO users VALUES ('','$fullname','$username','$password','$date')");

 

die ("You've been registered! <a href='index.php'>Click here</a> to return to login page");

}

 

}

 

}

else

echo "Your passwords do not match!";

 

 

}

else

echo "Please fill in <b>all</b> fields.";

 

}

 

 

?>

 

 

<html>

<form action='register.php' method='POST'>

<table>

<tr>

<td>Your Full name:</td>

<td><input type='text' name='fullname' value='<?php echo $fullname ?>'></td>

</tr>

<tr>

<td>Your Username:</td>

<td><input type='text' name='username' value='<?php echo $username ?>'></td>

</tr>

<tr>

<td>Your Password:</td>

<td><input type='password' name='password'></td>

</tr>

<tr>

<td>Repeat your password:</td>

<td><input type='password' name='repeatpassword'></td>

</tr>

</table>

<input type='submit' name='submit' value='Register'>

</form>

 

</html>

Link to comment
Share on other sites

 

Forgot to add it's hosted using MS IIS web server and I am testing using IE8 and Firefox 9

 

Hi, 

 

can anyone help me I'm new to coding and have been having trouble getting my code to insert into the database. I have checked the connection and that works fine but it says you're registered but it dosen't input it into the database.

 

This is the code i have in my script:

 

<?php

echo "<h1>Register</h1>";

 

$submit = $_POST ['submit'];

 

//form data

$fullname = strip_tags($_POST ['fullname']);

$username = strip_tags($_POST ['username']);

$password = strip_tags($_POST ['password']);

$repeatpassword = strip_tags($_POST ['repeatpassword']);

$date = date ("Y-m-d");

 

if ($submit)

{

 

//check for existance

if ($fullname&&$username&&$password&&$repeatpassword)

{

 

//check passwords match

if ($password==$repeatpassword)

{

 

//check char length of username and fullname

if (strlen($username)>25||strlen($fullname)>25)

{

echo "Maximum limit for username and fullname is 25 characters!";

}

else

{

 

//check password length

if (strlen($password)>25||strlen($password)<6)

{

echo "Your password must be between 6 and 25 characters!";

}

else

{

 

//register the user

 

//encrypt password

$password = md5 ($password);

$repeatpassword = md5 ($repeatpassword);

 

//open database

 

$connect = mysql_connect ("localhost","","") or die ("couldn't connect!");

 

mysql_select_db ("phplogin") or die ("couldn't find db!");

 

mysql_query ("INSERT INTO users VALUES ('','$fullname','$username','$password','$date')");

 

die ("You've been registered! <a href='index.php'>Click here</a> to return to login page");

}

 

}

 

}

else

echo "Your passwords do not match!";

 

 

}

else

echo "Please fill in <b>all</b> fields.";

 

}

 

 

?>

 

 

<html>

<form action='register.php' method='POST'>

<table>

<tr>

<td>Your Full name:</td>

<td><input type='text' name='fullname' value='<?php echo $fullname ?>'></td>

</tr>

<tr>

<td>Your Username:</td>

<td><input type='text' name='username' value='<?php echo $username ?>'></td>

</tr>

<tr>

<td>Your Password:</td>

<td><input type='password' name='password'></td>

</tr>

<tr>

<td>Repeat your password:</td>

<td><input type='password' name='repeatpassword'></td>

</tr>

</table>

<input type='submit' name='submit' value='Register'>

</form>

 

</html>

Link to comment
Share on other sites

I could be wrong but shouldn't this;

 

$connect = mysql_connect ("localhost","","") or die ("couldn't connect!");

mysql_select_db ("phplogin") or die ("couldn't find db!");

 

read more like this;

 

$connect = mysql_connect ("localhost","","") or die ("couldn't connect!");

mysql_select_db ("phplogin", $connect) or die ("couldn't find db!");

Link to comment
Share on other sites

My bet is on the query itself

mysql_query ("INSERT INTO users VALUES ('','$fullname','$username','$password','$date')");

try using the fieldnames

mysql_query ("INSERT INTO users (fullname,username,password,addedon) VALUES ('$fullname','$username','$password','$date')");

of course your fieldnames will be different

Link to comment
Share on other sites

 

Thank you but this didn't do anything different.

 

I could be wrong but shouldn't this;

 

$connect = mysql_connect ("localhost","","") or die ("couldn't connect!");

mysql_select_db ("phplogin") or die ("couldn't find db!");

 

read more like this;

 

$connect = mysql_connect ("localhost","","") or die ("couldn't connect!");

mysql_select_db ("phplogin", $connect) or die ("couldn't find db!");

Link to comment
Share on other sites

 

Hi I had the same problem that i would say registered but not show in the database.

 

My bet is on the query itself

mysql_query ("INSERT INTO users VALUES ('','$fullname','$username','$password','$date')");

try using the fieldnames

mysql_query ("INSERT INTO users (fullname,username,password,addedon) VALUES ('$fullname','$username','$password','$date')");

of course your fieldnames will be different

Link to comment
Share on other sites

I didn't read all your code... post it enclosed in  tags in the future (using the # symbol in the editor) for clarity for the rest of us.

 

what have you done to debug your code?

 

a basic step to debug is at least echo your queries and or variables and see if they contain what they are supposed to... per example your code:

mysql_query ("INSERT INTO users VALUES ('','$fullname','$username','$password','$date')");

 

will be much better for debugging purposes to write it in this way:

 

  $query = "INSERT INTO users VALUES ('','$fullname','$username','$password','$date')";

  // and then echo your raw query and look for errors
   echo "Query is : " . $query . "<br />";
  
   // and at least use a basic mechanism to trap possibles errors
   mysql_query( $query)  or die('Query Error : ' . mysql_error());

 

 

and also will help your debugging if, while in development you enable the error reporting and display; either globally (in php.ini) or locally in each script using this 2 lines at the beginning of your scripts.

error_reporting(E_ALL); 
ini_set("display_errors", 1);

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.