Jump to content

[SOLVED] can't get a simple form to input data into mysql database


jeger003

Recommended Posts

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'");   
   }
}


 

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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'");   
   }
}
?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites


$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.

Link to comment
Share on other sites


$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!!!!!

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.