Talha Farooq Talha Farooq - 3 months ago 12
HTML Question

MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near

I am facing this error in my code I have my PHP code is like..

<?php
$num = $_POST['h'];



if(isset($_POST['submit']))
{

for($i=0;$i<=$num;$i++)
{
if(isset($_REQUEST["quantity$i"])){$quantity = $_REQUEST["quantity$i"];}
if(isset($_REQUEST["purchase$i"])){$purchase = $_REQUEST["purchase$i"];}else{$purchase= 'purchase';} //for error controling
if(isset($_REQUEST["sale$i"])){$sale = $_REQUEST["sale$i"];}else{$sale = 00;} //for error controling
$sql="INSERT into myaccounts

('quantity','purchase','sale') values ('$quantity','$purchase','$sale')"; //for error controling
if(mysqli_query($dbh, $sql))
{
echo "Sucess";

} else {
echo 'MySQL Error: ' . mysqli_error($dbh);
}
}}
?>


The element
h
is in
html
code is

<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr >
<th class="text-center">
#
</th>
<th class="text-center">
Quantity
</th>
<th class="text-center">
Purchase Price
</th>
<th class="text-center">
Sale Price
</th>
<th class="text-center">
Profit Or Loss
</th>

</tr>
</thead>
<tbody>

<tr id='addr0'></tr>

<button type="submit" value="Register" name="submit" class="btn btn-default pull-">Submit</button>
</tbody>

</table>
<input type="hidden" name="h" id="h" />
</div>

</form>
</div>
<a id="add_row" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete Row</a>


</div>
<script>
$(document).ready(function(){
var i=0;
$("#add_row").click(function(){
$('#addr'+i).html("<td>"+ (i+1) +"</td><td><input name='quantity"+i+"' type='text' placeholder='Name' class='form-control input-md' /></td><td><input name='purchase"+i+"' type='text' placeholder='Purchase' class='form-control input-md'></td><td><input name='sale"+i+"' type='text' placeholder='Sale ' class='form-control input-md'></td>");

$('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
i++;
});
$("#delete_row").click(function(){
if(i>1){
$("#addr"+(i-1)).html('');
i--;
}
});

});
</script>


I have build up signup and login form using same
Insert
query now I am unable to achieve result here.

Answer

Remove single quotes from field names, like bellow:

$sql="INSERT INTO myaccounts (quantity,purchase,sale) values ('$quantity','$purchase','$sale')"; 
Comments