0

Hello, I am having a little trouble getting the variable $total to insert into mysql table. I used javascript to get the total that is in the input box. Any help is very much appreciative.

Here is site:

http://illcomputers.comyr.com/custom.php


Thank You

Snippets of Code

JavaScript Code

var mainitems = datacost + dvdcost + keymouse + flpycost + pwrcost + mntrcost + speakerCost + Case;
var lowitems = cpule + memle + videole + soundle + boardle + osle;
var total = mainitems + lowitems;
document.getElementById("amt1").value = total.toFixed(2);

HTML Code

<td><input name="amt1" id="amt1" type="text" readonly /></td>
              <td><input name="amt2" id="amt2" type="text" readonly /></td>
              <td><input name="amt3" id="amt3" type="text" readonly /></td>

PHP Code

$total = $_POST['amt1'];

echo "Total Cost: ".$total."<br />";

$query = "INSERT INTO illumcomputers (price) VALUES ('$total')";
			 
$result = mysqli_query($dbc, $query)

or die ('Error querying database.');

Edited by pgmarco: n/a

2
Contributors
7
Replies
11
Views
6 Years
Discussion Span
Last Post by pgmarco
0

line 5 of the javascript block is NOT the same as what you actually have on your live page. This is what you actually have: document.getElementById("amt1").value = "$" + total.toFixed(2); This means that on your php page, your $total variable should have a STRING that begins with a dollar symbol. If your DB field (price) is expecting a decimal, then get rid of the leading dollar sign symbol: $total = preg_replace('#[^0-9\.]#','',$_POST['amt1']);

0

Thanks but I already did that and it still doesn't work I keep getting error querying database, do I need to replace total with
this? preg_replace('#[^0-9\.]#','',$_POST);

Edited by pgmarco: n/a

0

do I need to replace total with
this?

On your original post, you posted one Javascript code and two php code block. On your SECOND php code block replace line 2 with what I posted earlier. That line of code is basically removing anything that is neither a number nor a period.

Also, feedback such as "it still doesn't work" is not helpful. Have you verified that it is even attempting to execute the query?

Does it execute the die() statement? If so, try changing the die() statement to: ... or die('Error querying database: ' . mysqli_error($dbc) ); so you can get a description of the error.

0

I am still learning PHP and MySQL, so I am just telling you what I know, I believe it did execute the die() statement because it returned the message Error querying database. I replaced the die statment to or die('Error querying database: ' . mysqli_error($dbc) ); and replaced the $total and got Parse error: syntax error, unexpected T_LOGICAL_OR in F:\web\www\template\computer_process.php on line 131

Line 131 is or die('Error querying database: ' . mysqli_error($dbc) );

$total = preg_replace('#[^0-9\.]#','',$_POST['amt1']);

echo "Total Cost: ".$total."<br />";

$query = "INSERT INTO illumcomputers (price) VALUES ('$total')";

$result = mysqli_query($dbc, $query);

or die('Error querying database: ' . mysqli_error($dbc) );

mysqli_close($dbc);

Edited by pgmarco: n/a

0

since you are using " or die(...)", you cannot end line 8 with a semicolon. On line 8 of your ORIGINAL post you correctly left out the semicolon.

It may help you understand that statement if you put in a single line: $result = mysqli_query($dbc, $query) or die('Error querying database: ' . mysqli_error($dbc) );

0

a more useful statement would be: $result = mysqli_query($dbc, $query) or die('<hr>Unable to execute query:<br> '. $query . '<br />' . mysqli_error($dbc) ); it would let you see what you actually tried to execute as well as a description of the error. If the problem persists, post the error reported.

0

Thank you so much for the help hielo, I got the error message back and it just ended up being that i spelled the table name wrong. The error that came up was could not find table. Again, Thank you for the help, I figured it was something lame like that.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.