PHP and Mysql form submission help, trying to learn.<

liunx

Guest
I have never worked with sql before, well I've installed scripts but never written anything myself. I'm also a beginner with PHP and only know the extreme basics.

I'm trying to write a small script where I can enter a username, title and note. I have no need for this, I just thought it would be something easy to begin with. So far it works, it uploads the content to the database when I press submit. However if I click refresh it will upload the info again and again. How to I stop the script after it submits?

For the life of me I've not been able to get the date to work, so for now I've removed it... How would I implement the use of;

Date
DateTime
TimeStamp
Time

If you could change the code to show me how to use each of those that would help me a lot.

I would also like someone to clean up my PHP code for inserting the sql data and show me the best and most safe way to do it.

I know this is asking a lot, so don't feel like you need to do it lol. I've looked at some tutorials but none of the ones I've found really explain the reasoning for doing something one way over another way, and none of them have shown how to use the date and/or time functions, they all use var instead.

The code I wrote is below.


Form Code

<form action="<?php echo $PHP_SELF; ?>" method="post" name="acp_notes" id="acp_notes">
<p><strong>Username</strong>
<br>
<input name="user" type="text" id="user">
<br>
<br>
<strong>Title:</strong><br>
<input name="title" type="text" id="title">
<br>
<br>
<strong>Note:</strong>
<br>
<textarea name="note" cols="60" rows="5" id="note"></textarea>
<br>
<input type="Submit" name="Submit" value="Submit">
</p>
</form>


PHP Code

<?php
$username="user";
$password="pass";
$database="db";
$table="acp_notes";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO $table VALUES ('','$user','$title','$note')";

mysql_query($query);
mysql_close();
?>Hi,
would you please ask all PHP questions in the PHP subforum, I'll move the thread there, thank you :)when you write your query for your database, you will have to set date as 'date'

$datesubmitted = time();

if you want to read the date again, you will have to format it using the date() function
<!-- m --><a class="postlink" href="http://uk2.php.net/manual/en/print/function.date.php">http://uk2.php.net/manual/en/print/function.date.php</a><!-- m -->

should help you, i don't know about the second part to your problem though sorryto add date you need to have a column in the table that is varchar(40) then you do date like so

$query = "INSERT INTO $table VALUES ('','$user','$title','$note','".time()."')";

if date was last.

to make it stop submitting you need ot forward to another page. hit submit it should give you a confirmation page then forward you to the form again for another insertion. meta tags are good for this.Whenever I read about using dates with php/sql it says not to use the varchar and to use date/datetime/timestamp, is there any reason for this, easier in the long run, etc...


Thanks.it is suppose to be better to use TIMESTAMP in mysql or date in mysql cause it is just a little faster. but it is all personal preference I think. I don't like the way mysql stores the date and time so it is up to you on how you wnat to do it.
 
Back
Top