Blank Data on SQL Insert

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arnyt
Forum Newbie
Posts: 3
Joined: Mon Sep 12, 2005 11:12 am

Blank Data on SQL Insert

Post by arnyt »

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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Re: Blank Data on SQL Insert

Post by neophyte »

arnyt wrote:
mysql_connect(localhost,"user","password");
Localhost should be a string 'localhost'.

Don't forget teh PHP tags when posting code.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

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

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>
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

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?
arnyt
Forum Newbie
Posts: 3
Joined: Mon Sep 12, 2005 11:12 am

Post by arnyt »

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).
arnyt
Forum Newbie
Posts: 3
Joined: Mon Sep 12, 2005 11:12 am

Post by arnyt »

It seems to be working now.
I used the validation code from Shiznatix (thanks for that) and it seems to now be writing the record to the table.
Weird thing is, the values from the HTML form were set in the original!!
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

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?
Post Reply