Solved

Mysql and insert query that has default values configure on a field in a table

Posted on 2014-08-01
9
628 Views
Last Modified: 2014-08-01
I have a small registration form that will allow someone to register to a class that our company offers.  My table name is registration and then I have 5 field defined in the table, the first field is the ID and it is auto increments, (works fine) the second field in the table is the regdate , I like to have the system use the time stamp to populate the data, but when I try to setup the field I get an error.  I have the field in MYSQL setup as type (Timestamp) Null (yes0 Default (Current_timestamp), attribute (on updateCurrent_timestamp), but when ever I post a record from the form, the data and time are not getting written to the database?  No sure why?

Second
I have another field in the same table that is CampaignID, this field has a default value of "Open House" it also is not being written to the database.  I need to have some help in the form or insert.php
$sql="INSERT INTO marketing.OpenHouseRegistrations (RegDate, FirstName, LastName, EmailAddress, PhoneNumber, CECredits)


VALUES

('$_POST[RegDate]','$_POST[FirstName]','$_POST[LastName]','$_POST[EmailAddress]','$_POST[PhoneNumber]','$_POST[CECredits]',$_POST'[CampaignID]')";
//THIS IS A TEST
  /*if (empty ($_Post["RegDate"]))*/
    /*{$$RegDate = $_POST['RegDate'];*/
  $RegDate = $_Post['RegDate'];
  //$IP = $_POST['IP'];
  $FirstName = $_POST['FirstName'];
  $LastName = $_POST['LastName'];
  $EmailAddress = $_POST['EmailAddress'];
  $PhoneNumber = $_POST['PhoneNumber'];
  $CECredits = $_POST['CECredit'];
  $CampaignID = $_POST['CampaignID'];

Please advise on what I doing wrong when using default values?

Thank you
0
Comment
Question by:Scott Johnston
  • 4
  • 4
9 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Your insert statement is inserting CampaignID value but it's not in your column list
(RegDate, FirstName, LastName, EmailAddress, PhoneNumber, CECredits)


What's the value of $_POST[RegDate]
Also all $_POST vars should be in apostrophes or speech marks i.e.
 $_POST["RegDate"]

$sql="INSERT INTO marketing.OpenHouseRegistrations (RegDate, FirstName, LastName, EmailAddress, PhoneNumber, CECredits,CampaignID)
VALUES
 ('".$_POST['RegDate']."','".$_POST['FirstName']."','".$_POST['LastName']."','".$_POST['EmailAddress']."','".$_POST['PhoneNumber']."','".$_POST['CECredits']."','".$_POST['CampaignID']."')";

Open in new window

Add this after the above line and post here
echo $sql;

But this is not the correct way to build your sql, you should be escaping your input data to prevent sql injection.
0
 

Author Comment

by:Scott Johnston
Comment Utility
I am sorry but I copies the wrong code,
Here is my updated code and it still is not working....
$sql="INSERT INTO marketing.OpenHouseRegistrations (RegDate, FirstName, LastName, EmailAddress, PhoneNumber, CECredits, CampaignID)


VALUES

('$_POST[RegDate]','$_POST[FirstName]','$_POST[LastName]','$_POST[EmailAddress]','$_POST[PhoneNumber]','$_POST[CECredits]','$_POST[CampaignID]')";
//THIS IS A TEST
  $RegDate = $_Post['RegDate'];
  $FirstName = $_POST['FirstName'];
  $LastName = $_POST['LastName'];
  $EmailAddress = $_POST['EmailAddress'];
  $PhoneNumber = $_POST['PhoneNumber'];
  $CECredits = $_POST['CECredit'];
  $CampaignID = $_POST['CampaignID'];
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Still use this and post back the result of the $sql line

$sql="INSERT INTO marketing.OpenHouseRegistrations (RegDate, FirstName, LastName, EmailAddress, PhoneNumber, CECredits,CampaignID)
VALUES
 ('".$_POST['RegDate']."','".$_POST['FirstName']."','".$_POST['LastName']."','".$_POST['EmailAddress']."','".$_POST['PhoneNumber']."','".$_POST['CECredits']."','".$_POST['CampaignID']."')";
echo $sql;

Open in new window

0
 

Author Comment

by:Scott Johnston
Comment Utility
I changed the coding to include the Double quote, added the Echo, the form works great, all the fields I enter data get populated into the database, the field that I want to have adefault value that is defined in the MYSQL table are not.  Can I just setup the insert so that it post a default value?  maybe that type of workaround will work better than using the MYSQL to write default value..???
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
Comment Utility
If you are adding a value for the column then it will not be the default value but whatever you pass in the SQL
If there is not going to be a value at the time you insert the record then remove the column and value from the SQL and the default value will be inserted.
0
 

Author Comment

by:Scott Johnston
Comment Utility
That was way to easy....So any time I am using a default value I do not need the field in the form or on the insert statement..
Thank you
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Correct
0
 

Author Closing Comment

by:Scott Johnston
Comment Utility
I appreciate your help, I am getting better at this but have very little expertise.  Thanks for helping!!!
0
 
LVL 14

Expert Comment

by:Renante Entera
Comment Utility
By the way, I just want to add something that might be useful...

I have seen part of your code:
$RegDate = $_Post['RegDate'];

Open in new window

Take note that it will not give you the expected result 'coz it should be:
$RegDate = $_POST['RegDate'];

Open in new window


In addition to that, if you are including complex variables within double quotes.  You can use braces around it then your code will look like this:
$sql="INSERT INTO marketing.OpenHouseRegistrations (RegDate, FirstName, LastName, EmailAddress, PhoneNumber, CECredits,CampaignID)
VALUES
 ('{$_POST['RegDate']}','{$_POST['FirstName']}','{$_POST['LastName']}','{$_POST['EmailAddress']}','{$_POST['PhoneNumber']}','{$_POST['CECredits']}','{$_POST['CampaignID']}')";
echo $sql;

Open in new window


Hope this helps a lot.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now