jeger003 Posted January 14, 2009 Share Posted January 14, 2009 Im trying to insert my own data entered into a specific table in my database This is the form I just need to insert the users name and a private email. I want to insert it into my usersdata table in my mysql DB.........I cant figure out what the problem is. <html> <head> <title>Forms</title> </head> <body> <form action="processor.php" method="post"> Username: <input type="text" name=[username] value="" /> <br /> Private Email: <input type="text" name=[private_email] value="" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> include 'config.php'; if(isset($_POST['private_email'])) if(isset($_POST['username'])) { { $email = $_POST['private_email']; $username = $_POST['username']; mysql_query("INSERT INTO userdata (private_email) WHERE username = '".$username."' VALUES ('$email'"); } } Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted January 14, 2009 Share Posted January 14, 2009 Your query is malformed. "INSERT INTO userdata (private_email) VALUES('".$email."') WHERE username = '"$username."'"; That will pretty much be the query youneed. The field/values have to be formatted as above and the where clause has to be added to the "end" of the query in this situation. Also keep in mind about security. You want to secure those variables before putting them into the database. You also want to perform some type of validation before inserting them into the database. http://www.w3schools.com will also list out examples of each type of SQL/Mysql operation that can be performed as well a proper structuring of each query. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 also, your html should be: <html> <head> <title>Forms</title> </head> <body> <form action="processor.php" method="post"> Username: <input type="text" name="username" value="" /> <br /> Private Email: <input type="text" name="private_email" value="" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> and the PHP: <?php include 'config.php'; if(isset($_POST['private_email']) && isset($_POST['username'])) { { $email = mysql_real_escape_string($_POST['private_email']); $username = mysql_real_escape_string($_POST['username']); mysql_query("INSERT INTO userdata (username,private_email) VALUES ('$username','$email'"); } } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 14, 2009 Share Posted January 14, 2009 That will pretty much be the query youneed. The field/values have to be formatted as above and the where clause has to be added to the "end" of the query in this situation. INSERTS don't have where clauses. Except maybe when using sub-queries Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted January 14, 2009 Share Posted January 14, 2009 He is right, I was in a hurry. If your trying to add something into a query at a specific location use "Update" command. If you are adding a new record use the "Insert" command. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 He is right, I was in a hurry. If your trying to add something into a query at a specific location use "Update" command. If you are adding a new record use the "Insert" command. or, REPLACE if you want INSERT or UPDATE if it exists Quote Link to comment Share on other sites More sharing options...
jeger003 Posted January 14, 2009 Author Share Posted January 14, 2009 So i can't use the "WHERE"? Cause I need it to be able to find the user by using the username field. Im not actually storing the user name. I only need to store the email. This form is for me in the back end to help me create users a private email. It wont be public to the users. How would i user the username field in the form to match with the username in the database and store the email. how can use replace in the query? Thank You guys for the helP! Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 then use UPDATE: mysql_query("UPDATE userdata SET private_email = '$username' WHERE private_email = '$email'"); Quote Link to comment Share on other sites More sharing options...
jeger003 Posted January 14, 2009 Author Share Posted January 14, 2009 then use UPDATE: mysql_query("UPDATE userdata SET private_email = '$username' WHERE private_email = '$email'"); Its not working not sure whats going on.............would it be possible to use "if"? like if $username == $_POST['username']; i know its not written properly but would this be possible? if so how do i write it out? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 oops...should be: mysql_query("UPDATE userdata SET username = '$username' WHERE private_email = '$email'"); Quote Link to comment Share on other sites More sharing options...
jeger003 Posted January 14, 2009 Author Share Posted January 14, 2009 oops...should be: mysql_query("UPDATE userdata SET username = '$username' WHERE private_email = '$email'"); should i be using UPDATE with INSERT INTO? Is UPDATE storing the email? Cause I only need the email stored not the username. I made the username field in the form to use as a search. To find the username that matches with the username i entered and store the email. could something like this be used inside the if brackets? $username = $_POST['username']; $data = mysql_query("SELECT * FROM userdata WHERE username = '".$username."'") or die(mysql_error()); then maybe i can have an INSERT INTO thingy here? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 what are the columns of the userdata table? Quote Link to comment Share on other sites More sharing options...
jeger003 Posted January 14, 2009 Author Share Posted January 14, 2009 what are the columns of the userdata table? id, username, email, private_email, date, location, firstname, and lastname Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 wow...i clearly need some coffee $username = mysql_real_escape_string($_POST['username']); mysql_query("UPDATE userdata SET private_email = '$email' WHERE username = '$username'") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
jeger003 Posted January 14, 2009 Author Share Posted January 14, 2009 wow...i clearly need some coffee $username = mysql_real_escape_string($_POST['username']); mysql_query("UPDATE userdata SET private_email = '$email' WHERE username = '$username'") or die(mysql_error()); hahah AWESOME rhodesa!!!! works great! now one last thing i'd like it to do......i did a few test......and is there a way to have it tell me if it couldnt find a username? say i misspelled a username how can i make it tell me that the username does not exist or something? Quote Link to comment Share on other sites More sharing options...
premiso Posted January 14, 2009 Share Posted January 14, 2009 $data = mysql_query("SELECT * FROM userdata WHERE username = '".$username."'") or die(mysql_error()); if (mysql_num_rows($data) < 1) echo $username . ' could not be found.'; Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 14, 2009 Share Posted January 14, 2009 $data = mysql_query("SELECT * FROM userdata WHERE username = '".$username."'") or die(mysql_error()); if (mysql_num_rows($data) < 1) echo $username . ' could not be found.'; I think a better option than running a new query would be to use mysql_affected_rows() which tells you the number of affected rows from the last executed query. Quote Link to comment Share on other sites More sharing options...
jeger003 Posted January 15, 2009 Author Share Posted January 15, 2009 $data = mysql_query("SELECT * FROM userdata WHERE username = '".$username."'") or die(mysql_error()); if (mysql_num_rows($data) < 1) echo $username . ' could not be found.'; I think a better option than running a new query would be to use mysql_affected_rows() which tells you the number of affected rows from the last executed query. Hey thank you guys! they both do exactly what i'd like it to do and im using both! Thanks Again Guys!!!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.