Jump to content

Inserting New Mysql Table Data In Php


shinami576
 Share

Recommended Posts

I'm mainly just doing this little snippit for learning how MySQL and PHP interact. There are two problems I'm having. The first is that there is no new data being put into the "username" table. I think this is related to the permissions I've given to the "signup" username that I use to open a connection during registration. It currently has the permissions of SELECT and INSERT. The second is that it tells me "Username S has been created." no matter what I type. I'm guessing this is because of the way I'm attempting to filter the submitted username. I'm wanting the usernames to be made up of only characters consisting of a-z, A-Z, and 0-9.registration page

<head><style type="text/css">body{  background-color:#000000;  color:#ffffff;}</style></head><body><form action="perform_register.php" method="post">username: <input type="text" name="user" /><br />password: <input type="text" name="pass" /><br /><input type="submit" name="new_register" value="Submit" /></form></body>

perform_register.php

<?phpif ($_POST['new_register'] = 'Submit'){  //MAKE SURE IT'S A SANE REGISTRATION SUBMISSION!!!  //Otherwise...YOU ARE UP S. CREEK!  $user = trim($_POST['new_register']['user']);  if(!filter_var($user, FILTER_SANITIZE_SPECIAL_CHARS))  {	echo "Username contains characters other than ASCII characters.<br />Please press the back button on your browser.";  }  $con = mysql_connect("localhost","my_user","my_pass");  if (!$con)  {	die('Could not connect: ' . mysql_error());  }  $sql = "SELECT my_db * INSERT INTO username VALUES ($user)";  mysql_query($sql,$con);  mysql_close($con);  echo "Username ".$user." has been created.";}?>

Edited by Shinami
Link to comment
Share on other sites

$sql = "SELECT my_db * INSERT INTO username VALUES ($user)";Are you trying to select or insert? MySQL doesn't print error messages automatically, you need to check for them:mysql_query($sql,$con) or exit(mysql_error());You'll also want to use mysql_real_escape_string to sanitize the input from the form before you add it to the database. String values in a SQL query also need to be inside quotes, and it doesn't look like you're adding the password to the database.

Link to comment
Share on other sites

Yay just got home from work. Now, lets a reply cranked out and figure out what's going on. =)@jlhaslip:echo $_POST['new_register'];This showed the contents being 'Submit', which is the form I used to send the user name and password.echo $_POST['new_register']['user'];No matter what I submitted as the user name, the contents is always S. This also holds true when I send something through the "pass" box in the form. Yay?I tried using printr() but it was a fatal error so echo had to suffice.@justsomeguy:About the SELECT...if I understand correctly, don't I have to select a table before I can do pretty much anything? Otherwise, how does my query know what table in which database I'm trying to do something with or to? =omysql_query($sql,$con) or exit(mysql_error());I'm so glad you posted that. I'm now getting told that there's a syntax error in my query. Also, I apparently fail at just selecting a DB as well. Time to reread the SELECT section here.

Link to comment
Share on other sites

You can use print_r($_POST) to print out the $_POST array. If you do that, you should see that it's in $_POST['user'] instead of $_POST['new_register']['user'].

About the SELECT...if I understand correctly, don't I have to select a table before I can do pretty much anything? Otherwise, how does my query know what table in which database I'm trying to do something with or to?
You specify the table name as part of the insert query. A select query is only for retrieving information (selecting it) from the database, it's an output operation.To select a database to use for the MySQL connection you use mysql_select_db after mysql_connect.
Link to comment
Share on other sites

Wai~! print_r($_POST) resulted in:Array ( [user] => minako [pass] => meh [new_register] => Submit ) Wee~! I learned something new. =) I don't know why I recall seeing it somewhere else as the way I have it coded above. Well, time to fix that. I'm going to go out on a limb here while I'm on this. $_POST['new_register']['pass'] I'm guessing that was selecting the first character in the string array contents of "new_register', which was Submit, since I constantly got "S" as the output no matter what I put in the user box. Given that $_POST is an array and you can have multidimensional arrays, it would definitely make sense.print_r? I just realized why I got the fatal error earlier on that. I used printr. =DI was able to work out the rest of my SQL woes. The user name has successfully been added to my user table. I've also got a suggestion to make on the INSERT option for the website tutorials now too. =^-^=I'm not merely content with understanding my mistakes. If my mistakes had unpredictable content, I will also want to know where the contents of my mistakes came from. Thank you guys!

Edited by Shinami
Link to comment
Share on other sites

Just posting the "finished product" for others to reference and compare should they be struggling too or find themselves scratching their heads over something similar later on. >.> NOTE:THIS EXAMPLE DOES NOT CHECK FOR THE USER ALREADY EXISTING!The registration form.

<body><form action="perform_register.php" method="post">username: <input type="text" name="user" /><br /><input type="submit" name="new_register" value="Submit" /></form></body>

Performing the registration of a user name once they press Submit.

if ($_POST['new_register'] = 'Submit'){  //MAKE SURE IT'S A SANE REGISTRATION SUBMISSION!!!  //Otherwise...YOU ARE UP S. CREEK!  $user = trim($_POST['user']);  $con = mysql_connect("localhost","user","pass");  if (!$con)  {	die('Could not connect: ' . mysql_error());  }  mysql_select_db('my_db');  $user = mysql_real_escape_string($user);  $sql = "INSERT INTO username VALUES ('{$user}')";  mysql_query($sql,$con) or exit(mysql_error());  mysql_close($con);}

Edited by Shinami
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...