Problem with PHP inserting DATA from dynamic row

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
azegurb
Forum Newbie
Posts: 4
Joined: Mon Feb 15, 2010 6:58 am

Problem with PHP inserting DATA from dynamic row

Post by azegurb »

I have code written in JavaScript
this a form that dynamically adds row (taken from internet and modernized by JavaScript masters)

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head><title>dinamik sheet</title>
<script>
var i=iteration;
function addrow(){
    var tbl = document.getElementById('sheet');
    var iteration = tbl.rows.length;
    var row=tbl.insertRow(iteration);
 
    var cellLeft = row.insertCell(0);
    var textNode = document.createTextNode(iteration);
    cellLeft.appendChild(textNode);
 
    var cellRight = row.insertCell(1);
    var el = document.createElement('input');
    el.type = 'text';
    el.name = 'txtRow'+iteration;
    el.id = 'txtRow'+iteration;
    el.size = 40;
    el.value = '0';
    el.onblur = sum;
    cellRight.appendChild(el);
 
    var cellRight1 = row.insertCell(2);
    cellRight1.id = 'txtRowe'+iteration;
 
    var cellRightsel = row.insertCell(3);
    var sel = document.createElement('select');
    sel.name = 'selRow' + iteration;
    sel.id = 'selRow' + iteration;
    sel.onchange = sum;
    sel.options[0] = new Option('MotherBoard ASUS', '150');
    sel.options[1] = new Option('MotherBoard Gigabyte', '165');
    sel.options[2] = new Option('MotherBoard MSI', '100');
    sel.options[3] = new Option('Graphiqcard ASUS', '85');
    sel.options[4] = new Option('GraphigCard ATI', '90');
    sel.options[5] = new Option('GraphigCard GefORCE', '88');
    cellRightsel.appendChild(sel);
    sum();
}
function removeRowFromTable()
{
  var tbl = document.getElementById('sheet');
  var lastRow = tbl.rows.length;
  if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
 
function sum(){
    var s1 = 0;
    var s2 = 0;
    var tbl=document.getElementById('sheet');
    var iteration=tbl.rows.length-1;
    for(var i=1; i<=iteration; i++){//loop through table rows
        var el1 = document.getElementById('txtRow'+i);//Row's Income field
        var el2 = document.getElementById('selRow'+i);//Row's percentage menu
        var el3 = document.getElementById('txtRowe'+i);//Row's Tax cell
        if(!el1 || !el2 || !el3) continue;
        var txt = el1.value;
        if(txt != ( '' + Number(txt) )) continue;//reject non-numeric entries
        var tax = Number(txt) * Number(el2[el2.selectedIndex].value);
        el3.innerHTML = tax.toFixed(2);
        s1 += Number(txt);
        s2 += tax;
    }
    var t1 = document.getElementById('total');
    var t2 = document.getElementById('taxtotal');
    if(t1){ t1.value = s1.toFixed(2); }
    if(t2){ t2.value = s2.toFixed(2); }
}
 
onload = function(){
    addrow();
}
</script>
</head>
<body>
<form name="eval_edit" method="POST" action="part.php?id=iteration-1>
<table align="center" width="75%">
<tr>
<td align="center">Balance sheet</td></tr>
<tr>
<td align="center">
    <table id="sheet" border="1">
    <tr><td>object</td><td>amount of products</td><td>amount of price per saled product </td><td>part name</td></tr>
 
    </table>
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="Add" onclick="addrow()" />
<input type="button" value="Remove" onclick="removeRowFromTable()" />
<input type="button" value="SUM" onClick="sum()"/>
<input type="submit" value="Submit" /><br />
</td>
</tr>
<tr>
<td align="left">
<input id="total" name="total" type="text"/>total amount of products<br />
<input id="taxtotal" name="taxtotal" type="text"/>total price of products
</td>
</tr>
</table>
</form>
 
</body>
</html>
and i created
table in PHPmyadmin
teze.sql

Code: Select all

CREATE TABLE `teze` (
  id int(11) NOT NULL auto_increment,
  amount varchar(200) NOT NULL default '',
  price varchar(100) NOT NULL default '',
  date date NOT NULL default '0000-00-00',
  name text NOT NULL,
  PRIMARY KEY  (id)
) TYPE=MyISAM;
and part.php

Code: Select all

<?php
@$db=mysql_connect('localhost','user DB','password DB');
mysql_select_db('database name');
if (isset($_POST['submit']))
{
$amount=$_POST['amount'];
$price=$_POST['amount'];
$date=$_POST['amount'];
$name=$_POST['amount'];
$i=($_GET['id'])
for($l=1;$l<$i;$l++){
$sql="INSERT INTO 'teze' VALUES ('','".$amount."','".$price."','".$date."','".$name."')";
$result=mysql_query($sql);
}
}
?>
but it gives error
I would like to add rows with help JavaScript
and submit data into mysql database with help of PHP
but it returns error
I need this script immediatly
if possible please help me
thanks beforehands
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with PHP inserting DATA from dynamic row

Post by requinix »

azegurb wrote:but it returns error
Have you considered that maybe, just maybe, it would help to tell us what that error is?

In SQL, 's are for strings. teze is a table, not a string. If you want quotes, use `s.
Post Reply