/    Sign up×
Community /Pin to ProfileBookmark

PHP Form Isn’t Inserting into Database

I have written this PHP script but it is not working. I also [b][u]do not know how to record the date and time of the form’s submission[/u].[/b] Any ideas? I have to insert the date & time into the ‘signups’ table with the attribute name ‘reg_submit_date’.

[code]
<?php

$host=”localhost”; // Host name
$username=”studentac”; // Mysql username
$password=”pimmsnumber1″; // Mysql password
$db_name=”database_test”; // Database name
$tbl_name=”signups”; // Table name

// Connect to server and select database.
mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);

// Get values from form
$firstname=$_POST[‘firstname’];
$lastname=$_POST[‘lastname’];
$total_payable=$_POST[‘total_payable’];

// Insert data into mysql
$sql=”INSERT INTO $tbl_name(firstname, lastname, total_payable)VALUES(‘$firstname’, ‘$lastname’, ‘$total_payable’)”;
$result=mysql_query($sql);

// if successfully insert data into database, displays message “Successful”.
if($result){
echo “Successful!”;
echo “<BR>”;
}

else {
echo “Error occured”;
}

// close connection
mysql_close();
?>
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@keelerzauthorApr 28.2011 — I'm also not sure whether it should be the following???
<i>
</i>// Insert data into mysql
$sql="INSERT INTO [B][COLOR="Red"]Signups[/COLOR][/B] (firstname, lastname, total_payable)VALUES('$firstname', '$lastname', '$total_payable')";
$result=mysql_query($sql);
Copy linkTweet thisAlerts:
@NogDogApr 29.2011 — If the reg_submit_date field is a date or datetime field, then you can use the MySQL NOW() function, e.g.:
[code=php]
$sql = "
INSERT INTO $tbl_name(
firstname,
lastname,
total_payable,
reg_submit_date
) VALUES (
'" . mysql_real_escape_string($firstname) . "',
'" . mysql_real_escape_string($lastname) . "',
'" . mysql_real_escape_string($total_payable) . "',
NOW()
)";
[/code]

PS: I decided to add some escaping to prevent SQL injection errors, too. ?
Copy linkTweet thisAlerts:
@DariaMay 02.2011 — Tip above is perfect for the actual time/date (if your server time/date configured to your time zone! I once forgot and my stuff was off by 5 hours ? ); also the more crooked way you may not necessarily use but should be aware of - would be to actually create the input field, as such:
[code=html]<input name="reg_submit_date" value="<?php echo date('Y-m-d h:i:s');?>">[/code] and then add this field/value into your INSERT statement.

Now, why bother? you may ask? - Just in case if you want to manipulate the input value - if for any reason you need to offset it by minutes or hours or days, etc. then you can just change the value on the hidden input to re-calculate accordingly to what you want prior to inserting - and you can even offset it in variance depending on other factors (different members/different themes, etc) if it is not universal across all entries.
×

Success!

Help @keelerz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...