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
viper6277
Forum Newbie
Posts: 6 Joined: Fri Aug 17, 2007 3:51 pm
Post
by viper6277 » Thu Sep 06, 2007 5:43 pm
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi All, I have an php script with, what I think is a small array problem....this is a piece of the code.
Basically the loop gets data from a form then does an insert of the array into my database, which all woks except for the last field ....part_status...Code: Select all
foreach($_POST["part"] as $key=>$val)
{
$qty = $_POST["qty"][$key];
$part_status = $_POST["part_status"][$key];
$data_insert.='("'.$val.'",
"'.$qty.'",
"'.$part_status.'"),';
echo "Part Number: $val QTY: $qty Part Status: $part_status<br />\n";
}
//remove last comma
$data_insert=substr($data_insert,0,-1);
$sql_parts="INSERT INTO tbl_service_calls_parts (part_number,qty, part_status) VALUES ".$data_insert."";
this is the result: ....What the hell is that "0" about.... it should say "Part Status" just like the line before it.
Part Number: FF3-001-000 QTY: 2 Part Status: Part Used
Part Number: AA2-001-000 QTY: 5 Part Status: 0
I'm at a loss, if I drop the "$part_status = $_POST["part_status"][$key];" the script works fine.
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
yacahuma
Forum Regular
Posts: 870 Joined: Sun Jul 01, 2007 7:11 am
Post
by yacahuma » Thu Sep 06, 2007 9:40 pm
Can you post the form?
viper6277
Forum Newbie
Posts: 6 Joined: Fri Aug 17, 2007 3:51 pm
Post
by viper6277 » Fri Sep 07, 2007 9:14 am
yacahuma wrote: Can you post the form?
Code: Select all
<?php include("include/db_connect.php"); ?>
<?php
if (isset($_POST['submit']))
{
//Count parts in array
$total_parts=(count($_POST["part"]));
// loop to define insert data (remember that the array keys start from 0)
$data_insert='';
foreach($_POST["part"] as $key=>$val)
{
$qty = $_POST["qty"][$key];
$part_status = $_POST["part_status"][$key];
$data_insert.='("'.$val.'","'.$qty.'","'.$part_status.'"),';
echo "Part Number: $val QTY: $qty Part Status: $part_status<br />\n";
}
//remove last comma
$data_insert=substr($data_insert,0,-1);
$sql_parts="INSERT INTO tbl_service_calls_parts (part_number,qty, part_status) VALUES ".$data_insert."";
if (!mysql_query($sql_parts))
{
die('Error: ' . mysql_error());
}
mysql_close($con);
?>
<?php
} else { // if form hasn't been submitted
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Dynamic fields</title>
<script language="javascript">
var counter=1;
function addRow() {
counter=counter+1;
//Alert(counter);
var tbody = document.getElementById("table1").getElementsByTagName("tbody")[0];
var row = document.createElement("TR");
//Number
var cell1 = document.createElement("TD");
var cell1 = document.createElement("TD");
//cell1.setAttribute("class","list_side");
//cell1.setAttribute("className","list_side");
cell1.innerHTML = " "+counter+"";
//Part
var cell2 = document.createElement("TD");
var inp2 = document.createElement("INPUT");
inp2.setAttribute("type","text");
inp2.setAttribute("name","part["+counter+"]");
inp2.setAttribute("size","21");
cell2.appendChild(inp2);
//Quantity
var cell3 = document.createElement("TD");
var inp3 = document.createElement("INPUT");
inp3.setAttribute("type","text");
inp3.setAttribute("name","qty["+counter+"]");
inp3.setAttribute("value","");
inp3.setAttribute("size","8");
cell3.appendChild(inp3);
//Part Status
var cell4 = document.createElement("TD");
var inp4 = document.createElement("SELECT");
var oOption = document.createElement("option");
var t = document.createTextNode("Part Used");
oOption.setAttribute("value", 0);
inp4.setAttribute("name","part_status["+counter+"]");
oOption.appendChild(t);
inp4.appendChild(oOption);
cell4.appendChild(inp4);
var oOption1 = document.createElement("option");
var t = document.createTextNode("Part to Order");
oOption1.setAttribute("value", 1);
oOption1.appendChild(t);
inp4.appendChild(oOption1);
row.appendChild(cell1);
row.appendChild(cell2);
row.appendChild(cell3);
row.appendChild(cell4);
tbody.appendChild(row);
//alert(row.innerHTML);
}
</script>
</head>
<body>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table width="700" border="0" align="center">
<tr>
<td width="132"><div align="right">Tech:</div></td>
<td width="134"><label>
<select name="tech" id="tech">
<option></option>
<option>Tech 1</option>
<option>Tech 2</option>
<option>Tech 3</option>
<option>Tech 4</option>
</select>
</label></td>
<td width="113"> </td>
<td width="203"> </td>
</tr>
<tr>
<td><div align="right">Company Name:</div></td>
<td><label>
<input name="company" type="text" id="company" size="29">
</label></td>
<td><div align="right">Call ID:</div></td>
<td><input name="callid" type="text" id="callid" size="29"></td>
</tr>
<tr>
<td><div align="right">Time In: </div></td>
<td><input name="timein" type="text" id="timein" size="29"></td>
<td><div align="right">Time Out: </div></td>
<td><input name="timeout" type="text" id="timeout" size="29"></td>
</tr>
<tr>
<td><div align="right">Total Meter:</div></td>
<td><input name="tmeter" type="text" id="tmeter" size="29"></td>
<td><div align="right">BW Meter: </div></td>
<td><input name="bwmeter" type="text" id="bwmeter" size="29"></td>
</tr>
<tr>
<td><div align="right">Color Meter: </div></td>
<td><input name="cmeter" type="text" id="cmeter" size="29"></td>
<td><div align="right">Scanner Meter:</div></td>
<td><input name="smeter" type="text" id="smeter" size="29" ></td>
</tr>
<tr>
<td><div align="right">Mileage:</div></td>
<td><input name="mileage" type="text" id="mileage" size="29"></td>
<td><div align="right">Charageable:</div></td>
<td><select name="charageable" id="charageable">
<option>Chargeable</option>
<option selected>Under Service Contract</option>
</select></td>
</tr>
<tr>
<td><div align="right">Contact Name: </div></td>
<td><input name="contact" type="text" id="contact" size="29"></td>
<td><div align="right">Contact Phone: </div></td>
<td><input name="contact_phone" type="text" id="contact_phone" size="29"></td>
</tr>
<tr>
<td><div align="right">IT Name: </div></td>
<td><input name="it" type="text" id="it" size="29"></td>
<td><div align="right">IT Phone:</div></td>
<td><input name="it_phone" type="text" id="it_phone" size="29"></td>
</tr>
<tr>
<td><div align="right">Call Status:</div></td>
<td><select name="call_status" id="call_status">
<option>Complete</option>
<option>Incomplete - Machine Down</option>
<option>Incomplete - Machine Up</option>
</select></td>
<td><div align="right">Action:</div></td>
<td><select name="action" id="action">
<option></option>
<option>Need Parts</option>
<option>Need TA</option>
<option>Need Demo</option>
</select></td>
</tr>
</table>
<p> </p>
<table width='340' align='center' id='table1' border='0'>
<tr>
<td> </td>
<td class='list_data' align='center' colspan='4'><input name="button" type='button' onClick='addRow();' value='New Part'></td>
</tr>
<tr>
<td> </td>
<td class='list_title' align='center'>Part Number</td>
<td class='list_title' align='center'>Qty</td>
<td class='list_title' align='center'>Part Status</td>
</tr>
<tr>
<td class='list_side'>1</td>
<td align='center'><input type='text' name='part[]' size='21'></td>
<td align='center'><input type='text' name='qty[]' size='8'></td>
<td align='center'>
<select name="part_status[]">
<option>Part Used</option>
<option>Part to Order</option>
</select></td>
</tr>
</table>
<p>
<label>
<div align="center">
<input type="submit" name="submit" value="Submit">
</div>
</label>
</p>
</form>
<?php
}
?>
</body>
</html>
viper6277
Forum Newbie
Posts: 6 Joined: Fri Aug 17, 2007 3:51 pm
Post
by viper6277 » Tue Sep 25, 2007 9:06 pm
Anyone have any ideas on this one ?... I've been real busy doing other things but this is still an issue for me.
EricS
Forum Contributor
Posts: 183 Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga
Post
by EricS » Tue Sep 25, 2007 10:52 pm
Have you done a var_dump on your $_POST variable?
This will show you all the data (including data types) that is being passed to your script. It looks as though you have a mistake in your Javascript which is creating the option your getting bad data for.
Anyway. The var_dump will tell you what data your getting and you can modify your form to provided the data your expecting.
lnt
Forum Newbie
Posts: 12 Joined: Mon Sep 24, 2007 8:47 am
Post
by lnt » Wed Sep 26, 2007 8:09 am
Remove counter in the name of html element
Code: Select all
inp2.setAttribute("name","part[]");
inp3.setAttribute("name","qty[]");
inp4.setAttribute("name","part_status[]");