Jump to content

Problem with form submissions in php and mySQL


music_lp90

Recommended Posts

Hi, I'm new to php and mySQL, but I've just learned how to create a form in html that links to a php page that sends the form data into a table in mySQL. The problem I'm having is that if someone tries to enter a message more than once, it won't let them because it won't allow duplicate names. How can I fix this?The table I created has the fields firstname VARCHAR(30)lastname VARCHAR(30)request LONGTEXTThis is the html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Untitled Document</title></head><body bgcolor="#ffffff"><form action="insert.php" method="post">Firstname: <input type="text" name="firstname" />Lastname: <input type="text" name="lastname" /><br><br>Request: <textarea name="request" cols="100" rows="10"></textarea><br><input type="submit" /></form></body></html>and this is the php that inserts the request into the mySQL<?php$con = mysql_connect("myHost","myUsername","myPasword");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("myDb", $con);$sql="INSERT INTO request (FirstName, LastName, request)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[request]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "request added";mysql_close($con)?>Thanks, any help is greatly appreciated.

Link to comment
Share on other sites

First of All Do this.Table MUST have PRIMARY KEY, add 'iRequestId' as INT(50) set it AUTO_INCREMENT.I can't understand ur question. Please Explain What u want in output.EXPLAIN : 'The problem I'm having is that if someone tries to enter a message more than once, it won't let them because it won't allow duplicate names'

Link to comment
Share on other sites

It's probably because one of your columns (firstname or lastname or both) has a constraint on it (e.g. a primary key). You could start by removing that constraint.

Link to comment
Share on other sites

Thanks, it works now. I didn't really understand what the primary key was when I created the table in the database. I created a new column, called it id, and set it to auto_increment and it works now. Thanks for the help, both of you, I really appreciate it!

Link to comment
Share on other sites

If you plan on developing this further, I'd like to offer a suggestion. Create two tables.Something like this:

Users--------- UserID FirstName LastNameMessages--------- MessageID UserID MessageText
This way, each message is associated to a particular user - through that UserID - so if you ever wanted to be able to determine how many messages, for example, each user had submitted, you could count the number of records in your Messages table that had a particular UserID.Good luck!
Link to comment
Share on other sites

Hey, thanks for the suggestion of using two tables. For what I'm using it for right now, its not something that would really be necessary, but I can definitely see using it in other cases and I probably will. I've come up with another question though. I'm trying to insert the date and time the message was submitted into the database. I created a new field in the mySQL table and made it's type set to timestamp and set it's attributes to ON UPDATE CURRENT_TIMESTAMP and set the default to 0000-00-00 00:00:00.here's the php that I have.<?php$con = mysql_connect("myHost","myUsername","myPassword");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("databaseName", $con);$sql="INSERT INTO request (FirstName, LastName, request)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[request]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }$to = "myEmail";$subject = $_POST[firstname].$_POST[lastname]."Request";$msg = $_POST[request];mail("$to", "$subject", "$msg");echo "Thankyou, your request has been added";mysql_close($con)?>I've been searching the web for how to enter the time here in the php, but so far I haven't had any luck. Thanks for any help!

Link to comment
Share on other sites

Here is the reference for PHP date/time functions:http://www.php.net/manual/en/ref.datetime.phpYou have a couple options. You can either use the MySQL functions to update the date, or you can use PHP. I always use PHP to deal with dates, but you can do whatever you want. If you want to use PHP, there are a couple options there. Either you can use the MySQL timestamp or date data type for the field, and use the PHP date function to format a date the way MySQL expects it, or you can just use timestamps and store it as a number, which is what I prefer to do. In that case, the database field would be an integer, and you would use the PHP time or mktime functions to generate the timestamp. The time function will generate the timestamp for the current date and time, or you can use mktime to generate the timestamp for a specific date and time. Either way, the output is an integer number that you can use with the database. So you would use the time function to generate the current timestamp when you insert a new item into the database, and then when you read that timestamp back out and want to display the date and time on the page, you would use the getdate function to convert the timestamp into an array with the month, day, year, hour, minutes, etc.

Link to comment
Share on other sites

Thankyou for the help with the date/time issue. I've got it working now. I couldn't get the timestamp to work for some reason and I don't think I figured out exactly how to do the date how you were saying you did it, but what I did was use $var = date("y/m/d") and inserted it into a VARCHAR column in the table and it works well enough for what I need now.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...