Page 1 of 1
Blank Data on SQL Insert
Posted: Mon Sep 12, 2005 11:18 am
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.
Re: Blank Data on SQL Insert
Posted: Mon Sep 12, 2005 11:28 am
by neophyte
arnyt wrote:
mysql_connect(localhost,"user","password");
Localhost should be a string 'localhost'.
Don't forget teh PHP tags when posting code.
Posted: Mon Sep 12, 2005 11:29 am
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>
Posted: Mon Sep 12, 2005 11:45 am
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?
Posted: Tue Sep 13, 2005 3:18 am
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).
Posted: Tue Sep 13, 2005 4:12 am
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!!
Posted: Tue Sep 13, 2005 6:31 am
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?