Jump to content

Array Error


Slowie

Recommended Posts

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

 

Link to comment
Share on other sites

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']' ");

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.