need help w/insert and date compare

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ckdoublenecks
Forum Newbie
Posts: 21
Joined: Mon Jan 24, 2011 6:47 pm

need help w/insert and date compare

Post by ckdoublenecks »

this is solved- thanks -Can someone help me figure out why this isn't inserting into the database?
I also need to determine the duedate (a month in the future). Someone suggested a script that was 61 lines long but ?

Code: Select all

<?php?>
<html><head>
<?php
include("getinvno.php");
echo "Invoice No is ".$data['invno'];
?>
<script type="text/javascript">
var today = new Date();
var date = today.getMonth() + " / " + today.getDate() + " / " + today.getFullYear();
</script>
</head>
<body onload="document.form.ordernum.focus()">

<!--increment date to create due date (next month)-->

<br><script type="text/javascript">
document.write(date);
</script>
<form name="form">
<input type="text" size="5" name="ordernum" id="ordernum"MAXLENGTH=5 
onkeyup="getResults();if(this.value.length==this.size)document.form.invnum.focus();" />Order No:<br />
<input type="text" size="5" name="invnum" id="invnum" />Invoice No:<br />
AR/AP: <SELECT name=payrec>
<OPTION class=highlight value=ar selected>R
<OPTION class=highlight value=ap>P
</OPTION></SELECT><br>
<b>Bill</b> - Name: <INPUT TYPE="text" name=bname>
Street: <INPUT TYPE="text" name=bstreet>
State: <SELECT name=bstate>
<OPTION class=highlight value=fl selected>Florida
<OPTION class=highlight value=ga>Georgia
<OPTION class=highlight value=al>Alabama
<OPTION class=highlight value=la>Louisiana
<OPTION class=highlight value=sc>South Carolina
</OPTION></SELECT>
City: <INPUT TYPE="text" name="bcity"><br>
</OPTION></SELECT>
Zip: <INPUT TYPE="text" size=5name="bzip" MAXLENGTH=5
onKeyUp="if(this.value.length==this.size)document.form.contact.focus();"><br>
<b>Contact: </b><INPUT TYPE="text" name=contact><BR>
<b>Ship</b> - Name: 
<INPUT TYPE="text" name=sname>
Street <INPUT TYPE="text" name=sstreet>
State <SELECT name=sstate>
<OPTION class=highlight value=fl selected>Florida
<OPTION class=highlight value=ga>Georgia
<OPTION class=highlight value=al>Alabama
<OPTION class=highlight value=la>Louisiana
<OPTION class=highlight value=sc>South Carolina
</OPTION></SELECT>
City: <INPUT TYPE="text" name="scity"><br>
</OPTION></SELECT>
Zip: <INPUT TYPE="text" size=5name="szip" MAXLENGTH=5
onKeyUp="if(this.value.length==this.size)document.form.terms.focus();"><p>
Terms: <SELECT name=terms>
<OPTION class=highlight value=net 10 selected>Net 10
<OPTION class=highlight value=1_10_30ga>1 10 Net 30
<OPTION class=highlight value=2_10_30>2 10 Net 30
<OPTION class=highlight value=cash>Cash
</OPTION></SELECT>
FOB: <SELECT name=fob>
<OPTION class=highlight value=Destination selected>Destination
<OPTION class=highlight value=Origin>Origin
</OPTION></SELECT><p>
<b>Dates</b> - Ordered: 
<INPUT TYPE="text" size=10 name="dateord" MAXLENGTH=10
onKeyUp="if(this.value.length==this.size)document.form.datecomp.focus();">
Completed: <INPUT TYPE="text" size=10 name="datecomp" MAXLENGTH=10
onKeyUp="if(this.value.length==this.size)document.form.duedate.focus();">
Due: <INPUT TYPE="text" size=10 name="duedate" MAXLENGTH=10
onKeyUp="if(this.value.length==this.size)document.form.datepaid.focus();">
Paid: <INPUT TYPE="text" size=10 name="datepaid" MAXLENGTH=10
onKeyUp="if(this.value.length==this.size)document.form.qty.focus();"><p>
Qty: <INPUT TYPE="text" size=5 name=qty>
Description: <INPUT TYPE="text" size=25 name=descr>
Charges: <INPUT TYPE="text" size=7 name=charges>
Paid Amt: <INPUT TYPE="text" size=7 name=paidamt>
Tax: <INPUT TYPE="text" size=6 name tax><p>
<b>Shipping</b> - Date:
<INPUT TYPE="text" size=10 name "shipdate" MAXLENGTH=10
onKeyUp="if(this.value.length==this.size)document.form.shipamt.focus();">
Amt:<INPUT TYPE="text" size=7 name shipamt><p>
<b>Amount Due</b>: <INPUT TYPE="text" size=7 name=amtdue><p>
<input type="submit" value="submit data" />
</p>
</form>
</body></html> 
<?php
if (isset( $_POST['payrec']) ) 
{
$pd=$_POST['pd'];
$payrec=$_POST['payrec'];
$ordernum=$_POST['ordernum'];
$invnum=$_POST['invnum'];  
$bname=$_POST['bname'];  
$bstreet=$_POST['bstreet'];  
$bstate=$_POST['bstate'];
$bcity=$_POST['bcity'];
$bzip=$_POST['bzip'];
$sname=$_POST['sname'];  
$sstreet=$_POST['sstreet'];  
$sstate=$_POST['sstate'];
$scity=$_POST['scity'];
$szip=$_POST['szip'];
$contact=$_POST['contact'];
$terms=$_POST['terms'];
$fob=$_POST['fob'];
$shipdate=$_POST['shipdate'];
$shipamt=$_POST['shipamt'];  
$dateord=$_POST['dateord'];
$datecomp=$_POST['datecomp'];
$duedate=$_POST['duedate'];  
$datepaid=$_POST['datepaid'];
$qty=$_POST['qty'];  
$desc=$_POST['descr'];  
$charges=$_POST['charges'];  
$paidamt=$_POST['paidamt'];
$dayslate=$_POST['dayslate'];  
$tax=$_POST['tax'];
$amtdue=$_POST['amtdue'];
$paidsum=$_POST['paidsum'];
}
mysql_connect(localhost,root,"");
mysql_select_db(oodb) or die( "Unable to select database"); 
$query = "
INSERT INTO oocust (pd,payrec,ordernum,invnum,bname,bstreet,bstate,bcity,bzip,sname,sstreet,sstate,scity,szip,contact,terms,fob,shipdate,shipamt,dateord,datecomp,duedate,datepaid,qty,descr,charges,paidamt,
dayslate,tax,amtdue,paidsum)
VALUES('$pd','$payrec','$ordernum','$invnum','$bname','$bstreet','$bstate','$bcity','$bzip','$sname','$sstreet','$sstate','$scity','$szip','$contact','$terms','$fob','$shipdate','$shipamt','$dateord','$datecomp','$duedate','$datepaid','$qty','$descr',
'$charges','$paidamt','$dayslate','$tax','$amtdue','$paidsum')";
echo "data inserted</font><br /><br />"; 
$stat = mysql_query($query) or die('Query failed: ' . mysql_error()); 
mysql_close();
?>
Last edited by ckdoublenecks on Sat Apr 16, 2011 10:26 am, edited 1 time in total.
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: need help w/insert and date compare

Post by incubi »

What does the SQL error say when you try to insert the data?

As for the time calc. I did something like that. Here's the code you can adapt

Code: Select all

$sysdate = date("Y-m-d");
if(strtotime($sysdate) > (strtotime($row["base_date"]) +2600000)
Or if you want more info on it
http://www.nawrik.com/programming/php/u ... uture-day/

Or you can do it in the sql query see the Mysql Docs

Lee
ckdoublenecks
Forum Newbie
Posts: 21
Joined: Mon Jan 24, 2011 6:47 pm

Re: need help w/insert and date compare

Post by ckdoublenecks »

thanks for the response. there is no error message. It says that the records were inserted but when I check, it has inserted 2 records with only the id#s and all the numeric fields have a zero value.
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: need help w/insert and date compare

Post by incubi »

Oh, so the query inserts the data but the data is not what you want to see; that's different then
"figure out why this isn't inserting into the database?"

In any case I'm surprised it inserts 2 records considering you are not looping the insert query.

A couple of things

1: You need to read/read again the PHP docs and the mysql docs.
2: You should add some security to the code at some point.
3: To get more then one record you'll need to loop through an array of data.

From what I see in your code you have a form with fields and the value in the fields you catch with POST then you directly insert them into the DB. I don't see any other data. Unless, you are saying every time you submit your form the data being inserted is be overwritten in that case its a table ID issue make sure you have at least one field that's an unique value in your table.
ckdoublenecks
Forum Newbie
Posts: 21
Joined: Mon Jan 24, 2011 6:47 pm

Re: need help w/insert and date compare

Post by ckdoublenecks »

I know it doesn't sound right but the id s are unique and one submit does generate two records(2 new id s). I'm just learning and I'm wondering re. the concern re. security re. a home localhost system.
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: need help w/insert and date compare

Post by incubi »

Okay, I couple of things, I don't know whats in getinvno.php.

Also, you need to learn how to troubleshoot your code.
Start by breaking down your code into smaller pieces so you can find errors faster.
In this case start by breaking down your HTML. Make a form that's one or two fields
and if they work add two more and so on.....Make the html simple for testing by removing
the java script.

Also, test what the POST is giving you i.e.

Code: Select all

print $_POST['pd'];
and so on.....
To get you started I will tell you this line is not complete.
<form name="form">
You said I have a form, but what's it going to do how can it "post" data?

Let me know how that turns out and show me the work and how you did the troubleshooting and we will go from there.




Lee
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: need help w/insert and date compare

Post by incubi »

I forgot to give you one other clue.
When a script runs it runs from start to end. In other words,
when you load the script it runs when you press the submit button it runs. :D

Lee
ckdoublenecks
Forum Newbie
Posts: 21
Joined: Mon Jan 24, 2011 6:47 pm

Re: need help w/insert and date compare

Post by ckdoublenecks »

i had forgot to include method=post to the form. that created a record but I still have some work because it still creates another record with just the id & zero values for the numeric fields - carch u later. thanks
charlie
Post Reply