|
Answer» Hi, I'm baffled here; the two following statements from my JS <BR>file displays the correct values but the code to enter them into a table enters zeroes. I tried document.write(s) and the correct values were displayed.what am I missing? Thanks in advance.
Code: [Select]var expression = value1 + op +value2 +'='+ total; alert(expression); ------------------------------------ Code: [Select]<?php include('calcbegin.html'); $servername="localhost"; $username="root";table $password="cookie"; $dbname="homedb"; show //Createconnection $conn=newmysqli($servername,$username,$password,$dbname); //Checkconnection if($conn->connect_error) {die("Connectionfailed:".$conn->connect_error);}
$sql="INSERTINTOcalculator(purpose,value1,op,value2,total) VALUES('$purpose','$value1','$op','$value2','$total')"; if($conn->query($sql)===TRUE) {echo"Newrecordcreatedsuccessfully";} else {echo"Error:".$sql."<br>".$conn->error;} $conn->close(); ?> Looks like your getting 0's because no values are being passed to VALUES ('$purpose', '$value1', '$op', '$value2', '$total') and so because nothing is passed to them they are 0 and so $total = 0.
From looking at your shared code it establishes a connection to your database, and just passes whatever value the variables are without any data being passed into those variables ( which is usually 0, however because not initialized to 0 it could be different than 0 with bogus info ) to the database as SEEN here:
$sql = "INSERT INTO calculator (purpose, value1, op, value2, total) VALUES ('$purpose', '$value1', '$op', '$value2', '$total')";
Do you have more code to this to show other than what is shown that shows where the variables are being assigned values? I feel that is where your problem is with this in the variable value assignments.Below is my code in it"s entirety. thanks for any help. Code: [Select]<?php include('calcbegin.html'); ?> <html><HEAD> <script type="text/javascript"> function make_blank() { document.form.type.value =""; } </script> </head><body><center> <!-- use ajax - OnCalc function to send data to your database. ???--> <FORM name="Keypad" action="" method="post"> <input type="text" name="purpose" size="48" value="what's this for?" onFocus="this.value=''"> <TABLE border=2 width=50 height=60 cellpadding=1 cellspacing=5> <tr> <TD colspan=3 align=middle> <input name="ReadOut" type="Text" size=24 value="0" width=100%></TD> <TD></TD> <TD> <input name="btnClear" type="Button" value=" C " onclick="Clear()"></TD> <TD> <input name="btnClearEntry" type="Button" value=" CE " onclick="ClearEntry()"></TD> </TR><TR> <TD> <input name="btnSeven" type="Button" value=" 7 " onclick="NumPressed(7)"></TD> <TD> <input name="btnEight" type="Button" value=" 8 " onclick="NumPressed(8)"></TD> <TD> <input name="btnNine" type="Button" value=" 9 " onclick="NumPressed(9)"></TD> <TD></TD> <TD> <input name="btnNeg" type="Button" value=" +/- " onclick="Neg()"></TD> <TD> <input name="btnPercent" type="Button" value=" % " onclick="Percent()"></TD> </TR><TR> <TD> <input name="btnFour" type="Button" value=" 4 " onclick="NumPressed(4)"></TD> <TD> <input name="btnFive" type="Button" value=" 5 " onclick="NumPressed(5)"></TD> <TD> <input name="btnSix" type="Button" value=" 6 " onclick="NumPressed(6)"></TD> <TD></TD> <TD align=middle> <input name="btnPlus" type="Button" value=" + " onclick="Operation('+')"></TD> <TD align=middle> <input name="btnMinus" type="Button" value=" - " onclick="Operation('-')"></TD> </TR><TR> <TD> <input name="btnOne" type="Button" value=" 1 " onclick="NumPressed(1)"></TD> <TD> <input name="btnTwo" type="Button" value=" 2 " onclick="NumPressed(2)"></TD> <TD> <input name="btnThree" type="Button" value=" 3 " onclick="NumPressed(3)"></TD> <TD></TD> <TD align=middle> <input name="btnMultiply" type="Button" value=" x " onclick="Operation('*')"></TD> <TD align=middle> <input name="btnDivide" type="Button" value=" / " onclick="Operation('/')"></TD> </TR><TR> <TD> <input name="btnZero" type="Button" value=" 0 " onclick="NumPressed(0)"></TD> <TD> <input name="btnDecimal" type="Button" value=" . " onclick="Decimal()"></TD> <TD colspan=3> </TD><TD> <input name="btnEquals" type="Button" value=" = " onclick="Operation('=')"></TD> </TR></TABLE> <INPUT type="image" src="programmer.gif" alt="submit button"><p> </FORM> <font face="Verdana, Arial, Helvetica" size=2> <SCRIPT LANGUAGE="JavaScript"> var FKeyPad = document.Keypad; var Accumulate = 0; var FlagNewNum = false; var PendingOp = ""; function NumPressed (Num) { if (FlagNewNum) { FKeyPad.ReadOut.value = Num; FlagNewNum = false; } else { if (FKeyPad.ReadOut.value == "0") FKeyPad.ReadOut.value = Num;
else FKeyPad.ReadOut.value += Num; } } function Operation (Op) { var Readout = FKeyPad.ReadOut.value; if (FlagNewNum && PendingOp != "="); else { FlagNewNum = true; if ( '+' == PendingOp ) { var temp = Accumulate; Accumulate += parseFloat(Readout); OnCalc(temp,PendingOp,Readout,Accumulate); } else if ( '-' == PendingOp ) { var temp = Accumulate; Accumulate -= parseFloat(Readout); OnCalc(temp,PendingOp,Readout,Accumulate); } else if ( '/' == PendingOp ) { var temp = Accumulate; Accumulate /= parseFloat(Readout); OnCalc(temp,PendingOp,Readout,Accumulate); } else if ( '*' == PendingOp ) { var temp = Accumulate; Accumulate *= parseFloat(Readout); OnCalc(temp,PendingOp,Readout,Accumulate); } else { Accumulate = parseFloat(Readout); } FKeyPad.ReadOut.value = Accumulate;
PendingOp = Op; } } function Decimal () { var curReadOut = FKeyPad.ReadOut.value; if (FlagNewNum) { curReadOut = "0."; FlagNewNum = false; } else { if (curReadOut.indexOf(".") == -1) curReadOut += "."; } FKeyPad.ReadOut.value = curReadOut; } function ClearEntry () { FKeyPad.ReadOut.value = "0"; FlagNewNum = true; } function Clear () { Accumulate = 0; PendingOp = ""; ClearEntry(); } function Neg () { FKeyPad.ReadOut.value = parseFloat(FKeyPad.ReadOut.value) * -1; } function Percent () { FKeyPad.ReadOut.value = (parseFloat(FKeyPad.ReadOut.value) / 100) * parseFloat(Accumulate); } function OnCalc(value1,op,value2,total) { var expression = value1 + op +value2 +'='+ total; alert(expression); } </SCRIPT> Code: [Select]<?php include('gethomedb.php'); //nowconnectedtodatabase if(!empty($_POST["submit"])) { $id=$_POST['id']; $value1=$_POST['value1']; $op=$_POST['op']; $value2=$_POST['value2']; $total=$_POST['total']; $name=$_POST['total']; if(isset($_POST['total'])) { $fetch="SELECTpurpose,value1,op,value2,totalFROMcalculatorWHEREtotal='".$name."'"; $result=mysqli_query($con,$fetch); if(!$result) {echo"Error:".(mysqli_error($con));} //=========================================================== $query=" INSERTINTOcalculator(purpose,value1,op,value2,total) VALUES('$purpose','$value1','$op','$value2','$total')"; mysqli_query($con,$query); mysqli_close($con); } } ?>Code: [Select]<a href="http://localhost/home/calcprint.php">PRINT</a> </body></html>
|