Slowie Posted May 31, 2011 Share Posted May 31, 2011 Hi guys this one page is giving me a headache. i posted earlier today with a question on how to insert multiple records and got given a quick demo of how to do it so ive gone away to look more and ive edited my code. however it still doesnt work. im wondering if ive coded a tiny bit wrong. id be really appreciative if you guys could help me out <?php include 'dbc.php'; page_protect(); company(); $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION[branch]' "); $id = $_POST['id']; if (isset($_POST['submit'])) { //Assign each array to a variable $id = $_POST['id']; $user_name = $_POST['user_name']; $Serviceamount = $Serviceamount['item']; $limit = count($id); echo $limit; $values = array(); // initialize an empty array to hold the values for($k=0;$k<$limit;$k++){ $user_name[$k] = check_input($user_name[$k]); $Serviceamount[$k] = check_input($Serviceamount[$k]); $values[$k] = "( '{$id[$k]}', '{$Serviceamount[$k]}')"; // build the array of values for the query string } $query = "INSERT INTO `Services` (Staffname, ServiceAmount) VALUES " . implode( ', ', $values ); // Form the query string and add the implod()ed values if (!mysql_query($query,$link)){ die('Error: ' . mysql_error()); } else { echo "$row record added"; } echo $values; } if (checkAdmin()) { ?> <html> <head> <title>Book Off Holiday</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script src="php_calendar/scripts.js" type="text/javascript"></script> <link href="styles.css" rel="stylesheet" type="text/css"> </head> <body> <form name="form" action="Newkpi.php" method="post"> <table width="100%" border="0" cellspacing="0" cellpadding="5" class="main"> <tr> <td colspan="3"> </td> </tr> <td width="160" valign="top"> <?php if (isset($_SESSION['user_id'])) { } ?> <a href="admin.php">Admin CP </a> </td> <td width="732" valign="top"> <p> <h3 class="titlehdr">New KPI</h3> <table width="300px" border="0" align="Centre" cellpadding="2" cellspacing="0"> <tr bgcolor="#000050"> <td width="20px"><h3 class="Text2">Staff ID</h3></td> <td width="20px"><h3 class="Text2">Staff Member</h3></td> <td width="20px"><h3 class="Text2">Service Amount</h3></td> <td width="20px"><h3 class="Text2">Service Date</h3></td> <td width="20px"><h3 class="Text2">Forecast For Next Month</h3></td> <td width="20px"><h3 class="Text2">Product Sales</h3></td> <td width="20px"><h3 class="Text2">Clients This Month</h3></td> <td width="20px"><h3 class="Text2">Personel Retension</h3></td> <td width="20px"><h3 class="Text2">Total Retension</h3></td> <td width="20px"><h3 class="Text2">Colours</h3></td> <td width="20px"><h3 class="Text2">Cuts</h3></td> <td width="20px"><h3 class="Text2">Pre-Booking</h3></td> <td width="20px"><h3 class="Text2">Time Used</h3></td> </tr> <?php while ($rrows = mysql_fetch_array($stafflist)) {?> <tr> <td><h3 class="Text3"><input type="" name="id[]" id="id[]" size="4" value="<?php echo $rrows['id'];?>" /></h3></td> <td><h3 class="Text3"><input type="" name="user_name[]" id="user_name[]" value="<?php echo $rrows['full_name'];?>" /></h3></td> <td><h3 class="Text3"><input name="Serviceamount" type="text" size="4" id="Serviceamount"></h3></td> <td><h3 class="Text3"><input name="servicedate" type="text" size="4" id="servicedate"></h3></td> <td><h3 class="Text3"><input name="forecast" type="text" size="4" id="forecast"></h3></td> <td><h3 class="Text3"><input name="productsales" type="text" size="4" id="productsales"></h3></td> <td><h3 class="Text3"><input name="Clientsthismonth" type="text" size="4" id="Clientsthismonth"></h3></td> <td><h3 class="Text3"><input name="Personelret" type="text" size="4" id="Personelret"></h3></td> <td><h3 class="Text3"><input name="Totalret" type="text" size="4" id="Totalret"></h3></td> <td><h3 class="Text3"><input name="colours" type="text" size="4" id="colours"></h3></td> <td><h3 class="Text3"><input name="cuts" type="text" size="4" id="cuts"></h3></td> <td><h3 class="Text3"><input name="prebooking" type="text" size="4" id="prebooking"></h3></td> <td><h3 class="Text3"><input name="timeused" type="text" size="4" id="timeused"></h3></td> </tr> <?php } ?> </table> <input name="submit" type="submit" id="submit" value="Create"> </table> </form> </body> </html> <?php } ?> thank you in advance Quote Link to comment https://forums.phpfreaks.com/topic/238022-array-error/ Share on other sites More sharing options...
Pikachu2000 Posted May 31, 2011 Share Posted May 31, 2011 "It still doesn't work" isn't much to go on. Could you be (a lot) more descriptive? Quote Link to comment https://forums.phpfreaks.com/topic/238022-array-error/#findComment-1223107 Share on other sites More sharing options...
fugix Posted May 31, 2011 Share Posted May 31, 2011 what error(s) exactly are you receiving? also the first error that i see is in this line $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION[branch]' "); need to add quotes in your session e.g $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION['branch']' "); Quote Link to comment https://forums.phpfreaks.com/topic/238022-array-error/#findComment-1223111 Share on other sites More sharing options...
Slowie Posted May 31, 2011 Author Share Posted May 31, 2011 sorry guys the error im getting is that when the button is pressed it should take the values from the text boxes and insert them into a table called Services but it isnt doing anything when the button is pressed it doesnt tell me if there is an sql error or tell me that it was successful. it also doesnt insert any information where it should Quote Link to comment https://forums.phpfreaks.com/topic/238022-array-error/#findComment-1223115 Share on other sites More sharing options...
fugix Posted May 31, 2011 Share Posted May 31, 2011 to check if it is a mysql error you can kill the script upon receiving an error and print the error e.g $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION[branch]' ") or die(mysql_error()); try to debug your queries and let us know your results Quote Link to comment https://forums.phpfreaks.com/topic/238022-array-error/#findComment-1223126 Share on other sites More sharing options...
Pikachu2000 Posted May 31, 2011 Share Posted May 31, 2011 what error(s) exactly are you receiving? also the first error that i see is in this line $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION[branch]' "); need to add quotes in your session e.g $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION['branch']' "); That line was fine the way it was. The code you offered will produce: Parsing Error: /htdocs/test.php line 105 - syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING @ OP Regarding the following line, I don't see where $Serviceamount['item'] would ever get a value in the code you posted. $Serviceamount = $Serviceamount['item']; Do you have error_reporting = -1 -and- display_errors = On in your php.ini file while you're doing development? Quote Link to comment https://forums.phpfreaks.com/topic/238022-array-error/#findComment-1223127 Share on other sites More sharing options...
Slowie Posted May 31, 2011 Author Share Posted May 31, 2011 sorry again the part im having trouble with is $id = $_POST['id']; if (isset($_POST['submit'])) { //Assign each array to a variable $id = $_POST['id']; $user_name = $_POST['user_name']; $Serviceamount = $Serviceamount['item']; $limit = count($id); echo $limit; $values = array(); // initialize an empty array to hold the values for($k=0;$k<$limit;$k++){ $user_name[$k] = check_input($user_name[$k]); $Serviceamount[$k] = check_input($Serviceamount[$k]); $values[$k] = "( '{$id[$k]}', '{$Serviceamount[$k]}')"; // build the array of values for the query string } $query = "INSERT INTO `Services` (Staffname, ServiceAmount) VALUES " . implode( ', ', $values ); // Form the query string and add the implod()ed values if (!mysql_query($query,$link)){ die('Error: ' . mysql_error()); } else { echo "$row record added"; } echo $values; } ive attached some printscreens for reference on what happens when the button is clicked [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/238022-array-error/#findComment-1223133 Share on other sites More sharing options...
Slowie Posted May 31, 2011 Author Share Posted May 31, 2011 what error(s) exactly are you receiving? also the first error that i see is in this line $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION[branch]' "); need to add quotes in your session e.g $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION['branch']' "); That line was fine the way it was. The code you offered will produce: Parsing Error: /htdocs/test.php line 105 - syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING @ OP Regarding the following line, I don't see where $Serviceamount['item'] would ever get a value in the code you posted. $Serviceamount = $Serviceamount['item']; Do you have error_reporting = -1 -and- display_errors = On in your php.ini file while you're doing development? i have ting error_reporting = E_ALL & ~E_DEPRECATED and display_errors = Off on a ubuntu setup i also changed $Serviceamount = $_POST['Item']; to $Serviceamount = $_POST['Serviceamount']; and <td><h3 class="Text3"><input name="Serviceamount" type="text" size="4" id="Serviceamount"></h3></td> to <td><h3 class="Text3"><input name="Serviceamount[]" type="text" size="4" id="Serviceamount[]"></h3></td> Quote Link to comment https://forums.phpfreaks.com/topic/238022-array-error/#findComment-1223138 Share on other sites More sharing options...
Pikachu2000 Posted May 31, 2011 Share Posted May 31, 2011 Edit it to the values in my previous post, then restart Apache and see what errors/warnings are generated. That information can be very helpful while debugging. Quote Link to comment https://forums.phpfreaks.com/topic/238022-array-error/#findComment-1223142 Share on other sites More sharing options...
fugix Posted May 31, 2011 Share Posted May 31, 2011 what error(s) exactly are you receiving? also the first error that i see is in this line $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION[branch]' "); need to add quotes in your session e.g $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION['branch']' "); That line was fine the way it was. The code you offered will produce: Parsing Error: /htdocs/test.php line 105 - syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING @ OP Regarding the following line, I don't see where $Serviceamount['item'] would ever get a value in the code you posted. $Serviceamount = $Serviceamount['item']; Do you have error_reporting = -1 -and- display_errors = On in your php.ini file while you're doing development? his original code would have caused that error due to the session in the query..i simply appended or die() on the end Quote Link to comment https://forums.phpfreaks.com/topic/238022-array-error/#findComment-1223145 Share on other sites More sharing options...
Slowie Posted May 31, 2011 Author Share Posted May 31, 2011 ahhh thank you very much right the errors i get are before the button is clicked Notice: Undefined index: id in /var/www/Newkpi.php on line 12 and once the button is clicked i get Fatal error: Call to undefined function check_input() in /var/www/Newkpi.php on line 28 which would explain why its not inserting now solved i got rid of that function and it worked thank you to everyone especially fugix and Pikachu2000 who helped alot with my debugging problem Quote Link to comment https://forums.phpfreaks.com/topic/238022-array-error/#findComment-1223146 Share on other sites More sharing options...
fugix Posted May 31, 2011 Share Posted May 31, 2011 ahhh thank you very much right the errors i get are before the button is clicked Notice: Undefined index: id in /var/www/Newkpi.php on line 12 and once the button is clicked i get Fatal error: Call to undefined function check_input() in /var/www/Newkpi.php on line 28 which would explain why its not inserting now solved i got rid of that function and it worked thank you to everyone especially fugix and Pikachu2000 who helped alot with my debugging problem glad we could help you Quote Link to comment https://forums.phpfreaks.com/topic/238022-array-error/#findComment-1223154 Share on other sites More sharing options...
Pikachu2000 Posted May 31, 2011 Share Posted May 31, 2011 what error(s) exactly are you receiving? also the first error that i see is in this line $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION[branch]' "); need to add quotes in your session e.g $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION['branch']' "); That line was fine the way it was. The code you offered will produce: Parsing Error: /htdocs/test.php line 105 - syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING @ OP Regarding the following line, I don't see where $Serviceamount['item'] would ever get a value in the code you posted. $Serviceamount = $Serviceamount['item']; Do you have error_reporting = -1 -and- display_errors = On in your php.ini file while you're doing development? his original code would have caused that error due to the session in the query..i simply appended or die() on the end No the OP's code wouldn't have caused that error. You added quotes in the array index, and the error I posted was copied and pasted after running the actual code with the edit you posted. Quote Link to comment https://forums.phpfreaks.com/topic/238022-array-error/#findComment-1223169 Share on other sites More sharing options...
fugix Posted May 31, 2011 Share Posted May 31, 2011 what error(s) exactly are you receiving? also the first error that i see is in this line $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION[branch]' "); need to add quotes in your session e.g $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION['branch']' "); That line was fine the way it was. The code you offered will produce: Parsing Error: /htdocs/test.php line 105 - syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING @ OP Regarding the following line, I don't see where $Serviceamount['item'] would ever get a value in the code you posted. $Serviceamount = $Serviceamount['item']; Do you have error_reporting = -1 -and- display_errors = On in your php.ini file while you're doing development? his original code would have caused that error due to the session in the query..i simply appended or die() on the end No the OP's code wouldn't have caused that error. You added quotes in the array index, and the error I posted was copied and pasted after running the actual code with the edit you posted. okay, problem was solved....moving on Quote Link to comment https://forums.phpfreaks.com/topic/238022-array-error/#findComment-1223174 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.