Reputation: 103
I have a code here that inserts all the data to the database...my problem its not working what is the problem with my code? can anyone look for my code and see what wrong? Please
html code:
<form method="post" name="frm" id="frm" >
<input type="text" name="name1" id="query1" onBlur="getvalues1()" />
<input type="radio" name="optA" value="1" onClick="optTotal()" />1
<input type="radio" name="optA" value="2" onClick="optTotal()" />2
<input type="radio" name="optA" value="3" onClick="optTotal()" />3
|
<input type="radio" name="optB" value="1" onClick="optTotal()" />1
<input type="radio" name="optB" value="2" onClick="optTotal()" />2
<input type="radio" name="optB" value="3" onClick="optTotal()" />3
|
<input type="radio" name="optC" value="1" onClick="optTotal()" />1
<input type="radio" name="optC" value="2" onClick="optTotal()" />2
<input type="radio" name="optC" value="3" onClick="optTotal()" />3
|
<input type="radio" name="optD" value="1" onClick="optTotal()" />1
<input type="radio" name="optD" value="2" onClick="optTotal()" />2
<input type="radio" name="optD" value="3" onClick="optTotal()" />3
<input type="text" name="total" id="total" onKeyUp="optTotal()" /><br />
<input type="text" name="aic1" id="aic1"/>
<input type="text" name="batchcode1" id="batchcode1" /><br>
<input type="submit" value="Test" name="calculate" id="press" />
<br />
</form>
php code:
<?php
if(isset($_POST['calculate']))
{
$name = array_key_exists('name1', $_POST) ? $_POST['name1'] : null;
//$name = $_POST['name'];
$score1 = array_key_exists('optA', $_POST) ? $_POST['optA'] : null;
//$score1 = $_POST['optA'];
$score2 = array_key_exists('optB', $_POST) ? $_POST['optB'] : null;
//$score2 = $_POST['optB'];
$score3 = array_key_exists('optC', $_POST) ? $_POST['optC'] : null;
//$score3 = $_POST['optC'];
$score4 = array_key_exists('optD', $_POST) ? $_POST['optD'] : null;
//$score4 = $_POST['optD'];
$aic = $_POST['aic1'];
//$score3 = $_POST['optC'];
$batchcode = $_POST['batchcode1'];
//$score4 = $_POST['optD'];
$total = ($score1 + $score2 + $score3 + $score4);
mysql_query("INSERT INTO score (name,score1,score2,score3,score4,total,aic,bathcode) VALUES ('$name','$score1','$score2','$score3','$score4','$total','$aic','$batchcode')");
header("Location:home.php");
}
?>
score script:
<script>
function optTotal()
{
var a1 = document.querySelector('input[name="optA"]:checked');
var b1 = document.querySelector('input[name="optB"]:checked');
var c1 = document.querySelector('input[name="optC"]:checked');
var d1 = document.querySelector('input[name="optD"]:checked');
if (a1 != null)
a1 = parseFloat(a1.value);
else
a1 = 0;
if (b1 != null)
b1 = parseFloat(b1.value);
else
b1 = 0;
if (c1 != null)
c1 = parseFloat(c1.value);
else
c1 = 0;
if (d1 != null)
d1 = parseFloat(d1.value);
else
d1 = 0;
document.frm.total.value=parseFloat(a1)+parseFloat(b1)+parseFloat(c1)+parseFloat(d1);
}
</script>
i cant find whats wrong with my code...my insert query is not working its not saving if i click the button.
Upvotes: 0
Views: 68
Reputation: 774
I tested the code it gave me this Query:
INSERT INTO score
(name,score1,score2,score3,score4,total,aic,bathcode)
VALUES
('x1','2','3','2','2','9','x2','x3')
Paste it in phpmyadmin and check the error message if is there any.
But I don't really see any MYSQL connection and database selecting at
if(isset($_POST['calculate'])) So make sure you connected to sql and selected a database.
$con = mysql_connect("localhost","user","pass");
if(!$con){die("Can't connect.");}
mysql_select_db("database_name",$con);
Because it seems it's generating a valid query.
Edit: Also what Ed Cottrell said is important too. PDO example:
$db = new PDO('mysql:host=localhost;dbname=DB_NAME;charset=utf8', 'DB_USER', 'DB_PASS');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
And the insert part:
$stmt = $db->prepare("INSERT INTO score (name,score1,score2,score3,score4,total,aic,bathcode)
VALUES (?,?,?,?,?,?,?,?)");
$stmt->execute(array($name,$score1,$score2,$score3,$score4,$total,$aic,$batchcode);
Upvotes: 1