Wondered if anyone had a clue why I am getting blank data on an insert to a MYSQL database.
Heres the PHP Code:
<?
$user=$_POST['user'];
$email=$_POST['email'];
mysql_connect(localhost,"user","password");
@mysql_select_db("steve_test") or die( "Unable to select database");
$sql = "INSERT INTO emails (user,email) VALUES ('$user','$email')";
mysql_query($sql);
echo mysql_error();
mysql_close();
?>
Ignore the user password combo since the connection is fine. It's just that everytime this program is called it inserts a blank from the form.
HTML CODE:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Add New User</title>
</head>
<body>
<h1>
Add New User
</h1>
<form action="add.php" method="POST">
User Name: <input type="text" name="user" />
Email Address: <input type="text" name="email" />
<input type="submit" value="Add" />
</form>
</body>
</html>
Since I am pretty new at this I haven't a clue what's happening.
Any help would be appreciated.
Cheers.
Blank Data on SQL Insert
Moderator: General Moderators
Re: Blank Data on SQL Insert
Localhost should be a string 'localhost'.arnyt wrote:
mysql_connect(localhost,"user","password");
Don't forget teh PHP tags when posting code.
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
first off, perdy please use tags when posting php in the forum, it makes life about a billion times easier.
second of all you are not doing any validating, it always is going to insert into the db when you call it unless you check if the $_POST['email'] and the name are set. if they are set then insert otherwise its just going to give you the blank record becuase the $email and the $name are blank
second of all you are not doing any validating, it always is going to insert into the db when you call it unless you check if the $_POST['email'] and the name are set. if they are set then insert otherwise its just going to give you the blank record becuase the $email and the $name are blank
Code: Select all
if (false !== isset($_POST['user']) && false !== isset($_POST['email']))
{
$user=$_POST['user'];
$email=$_POST['email'];
mysql_connect(localhost,"user","password");
@mysql_select_db("steve_test") or die( "Unable to select database");
$sql = "INSERT INTO emails (user,email) VALUES ('$user','$email')";
mysql_query($sql);
echo mysql_error();
mysql_close();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Add New User</title>
</head>
<body>
<h1>
Add New User
</h1>
<form action="add.php" method="POST">
User Name: <input type="text" name="user" />
Email Address: <input type="text" name="email" />
<input type="submit" value="Add" />
</form>
</body>
</html>What elese are you supposed to get from an insert statement? Normally all a database returns when doing inserts, updates, deletes, etc..., is the number of affected rows or the last insert id.
http://us2.php.net/mysql_affected_rows
Besides, you aren't trying to fetch any data in the code anyway? Did you write this?
http://us2.php.net/mysql_affected_rows
Besides, you aren't trying to fetch any data in the code anyway? Did you write this?
Firstly sorry for not using the tags I'll know better next time.
Secondly to BDKR : it's probably the way I worded the question that could have caused some confusion. I am not expecting anything to be returned from the program. What I would like is the data in the user and email fields to be added to the table in the database. It isn't being added. All that is being added to the table in the database is a blank record.
And just to answer your other question about whether I wrote it.....As I said I'm pretty new at this and most of the code has been chopped from a tutorial (apart from the HTML).
Secondly to BDKR : it's probably the way I worded the question that could have caused some confusion. I am not expecting anything to be returned from the program. What I would like is the data in the user and email fields to be added to the table in the database. It isn't being added. All that is being added to the table in the database is a blank record.
And just to answer your other question about whether I wrote it.....As I said I'm pretty new at this and most of the code has been chopped from a tutorial (apart from the HTML).
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
thats not weird at all. see when you have the code without the validation every time you load that page it will run the insert query, even if there is no data to be inserted (ie. the name and email have no value becuase there is no post data because nobody clicked submit on the form) but if you check to see if the $_POST data exists (ie. what i did for you) then it won't run the insert query unless someone hit submit on the form. do you understand?