Jump to content

Recommended Posts

Hello,

 

In my code I'm trying to get multiple items from a form submitted to a database. The database seems to receive the number of items sent and the id value just fine but the data itself only shows "Array" where it should show an actual value (ie, Microsoft, IBM, Universal, etc).

 

The code that submits to the database is,

 

	while(list(, $ic) = each ($_POST["company$val"])) {
   mysql_query("INSERT INTO data VALUES (null, 'company', '".$ic."', '".$id."')");
}

 

company is simply the name of the field while $ic is simply a random variable I assigned that holds the user value chosen in the form, $id is the id number of the form.

 

An example of the database input,

 

id    name    data  fid

200 company Array 12

 

The issue is however that if I instead tell the form to print the array then I get a list of all the item names (as it should be) so I assume the form is fine. I've been focused on this problem for quite a while now but haven't been able to find a solution, the examples I've seen don't seem to address this type of an issue either, any help would be greatly appreciated.

 

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/244662-form-only-submits-array-to-database/
Share on other sites

can you post what is posted when you submit the form and the relevant code so we can get a better idea of what you are trying to do.

 

Hello,

 

The code and what is stored in the database was already posted in my original post, unless you mean something else?

Without providing a script and all of it's elements there is absolutely no way anyone could help you.  The reason is, we would need to know how the form that submits the data is coded, and all of the data sanitation and validation that is going with it.  We also need to see what you are trying to do, so that we could go along with the logic.

 

You can say what it is doing, but a lot of times your code will be loaded on a test server for testing (when someone is being nice).  However, in this instance, the form is being asked for, so the poster can see why you are getting a multi-dimensional array from it.

Hello,

 

Sure thing, here's the larger scope code (I ended up changing a few things since posting last night),

 

   while (list(, $pid) = each ($_POST['id'])) {

   $r = mysql_query("UPDATE ".$pre."content SET name = '".addslashes($_POST["name_$pid"])."' WHERE id = '".$pid."'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$r."<br><b>Error:</b> (".mysql_errno().")".mysql_error());

   $result = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = '".$_POST["cat_$pid"]."' OR cat = 'global' OR cat = '' ORDER BY `id` DESC");
   while ($row = mysql_fetch_array($result)) {
   $name = "$row[name]";
   $type = "$row[type]";
   

if ($row[name] == "company") {
while(list(, $ic) = each ($_POST["company$pid"])) {	  
mysql_query("INSERT INTO data VALUES (null, 'company', '".$ic."', '".$pid."')");	
}
}
   }

   }

 

And here's the form,

 

echo "<form action='index.php.php?view=manage&edit=2' name='form1' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"left\">";

    while (list(, $pid) = each ($_POST['id'])) {
$query="SELECT * FROM ".$pre."content WHERE id = '$pid'";
$result=mysql_query($query);
while($row2 = mysql_fetch_array($result)) {
	$nameav = "$row2[name]";
	$cat = "$row2[cat]";
	$namea = htmlspecialchars(stripslashes($nameav));

echo "<input type=\"hidden\" name=\"id[]\" value=\"$pid\"><input type=\"hidden\" name=\"cat_$pid\" value=\"$cat\"><tr><td><b><center>Item #".$pid."</b></center></td></tr><tr><td><b>Name</b></td><td><input type='text' name=\"name_$pid\" value=\"$namea\"></td></tr>";

$query = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = '".$cat."' OR cat = '' OR cat = 'global' ORDER BY `id` DESC") or die(mysql_error());
while($row = mysql_fetch_array($query)) {
	$name = "$row[name]";
	$type = "$row[type]";

		if ($type == "company") {

		echo "<link rel=\"stylesheet\" href=\"../js/css/common.css\" type=\"text/css\" />
		<link type=\"text/css\" rel=\"stylesheet\" href=\"http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/ui.all.css\" />
		<link type=\"text/css\" href=\"../js/css/ui.multiselect.css\" rel=\"stylesheet\" />
		<script type=\"text/javascript\" src=\"../js/jquery-1.4.2.min.js\"></script>
		<script type=\"text/javascript\" src=\"../js/jquery-ui-1.8.custom.min.js\"></script>
		<script type=\"text/javascript\" src=\"../js/plugins/localisation/jquery.localisation-min.js\"></script>
		<script type=\"text/javascript\" src=\"../js/plugins/scrollTo/jquery.scrollTo-min.js\"></script>
		<script type=\"text/javascript\" src=\"../js/ui.multiselect.js\"></script>
		<script type=\"text/javascript\">
		$(function(){
		$.localise('ui-multiselect', {/*language: 'en',*/ path: '../js/locale/'});
		$(\".multiselect\").multiselect();
		$('#switcher').themeswitcher();
		});
		</script>";

		$data = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."data WHERE id2 = '".$row2[id]."' AND name = 'company'"));
		$woo = mysql_fetch_row(mysql_query("SELECT name FROM ".$pre."pr WHERE id = '".$data[0]."'"));

		echo "<tr><td>".$name."</td><td><select name='".$name.$pid."[]' multiple='multiple' class = 'multiselect'>";

		if ($woo[0]) {
		echo "<option value='".$data."' selected>-- ".$woo[0]." --</option>";
		}

		$sql2 = mysql_query("SELECT * FROM ".$pre."pr ORDER BY `name` ASC") or die(mysql_error());
		while($row25 = mysql_fetch_array($sql2)) {
		echo "<option value='".$row25[id]."'>".$row25[name]."</option>";
		}
			echo "</select></td></tr>";
}

}
}
echo "<tr><td><input type=\"submit\" name=\"Add\" value=\"Submit Changes\"></td></tr></form></table>";

 

The js stuff at the beginning is simply for a script I'm using that makes selection easier to manage, I tried without it but the same issue still arises.

 

Thanks,

I see why you are having trouble.  There is no possible way for you to know what the name of the input elements are.  You should re-think you logic of how you are going to retrieve these fields.  I would suggest something along the lines of:

 

<?php
if(isset($_POST['Add'])) {
foreach($_POST['product'] as $key => $value) {     
     if(!empty($value)) {
       echo 'Index: ' . $key . ' has a value:' . $value . "<br />\n";
     }
    }
  }
?>
<form action="#" method="post">
<input name="product[<?php echo $pid; ?>]" placeholder="Name" />
<input type="submit" name="Add" value="Submit Changes">
</form>

 

This way you will know that you have a product line, and the pid is the index of that input element.  If you add it to a loop, just increment the pid.

I see why you are having trouble.  There is no possible way for you to know what the name of the input elements are.  You should re-think you logic of how you are going to retrieve these fields.  I would suggest something along the lines of:

 

<?php
if(isset($_POST['Add'])) {
foreach($_POST['product'] as $key => $value) {     
     if(!empty($value)) {
       echo 'Index: ' . $key . ' has a value:' . $value . "<br />\n";
     }
    }
  }
?>
<form action="#" method="post">
<input name="product[<?php echo $pid; ?>]" placeholder="Name" />
<input type="submit" name="Add" value="Submit Changes">
</form>

 

This way you will know that you have a product line, and the pid is the index of that input element.  If you add it to a loop, just increment the pid.

 

Hello,

 

Thanks for your response, rewriting the old code to be more cleaner/efficient is something I'm definitely looking into/partially started with the submission part of the form.

 

If the form isn't sending out the correct input elements, is there any reason why doing something like print_r() would output the selected form options correctly but not send them to the database?

 

Ex,

 

while(list(, $ic) = each ($_POST["company$val"]))
{
print_r($ic);
mysql_query("INSERT INTO data VALUES (null, 'company', '".$ic."', '".$id."')");	
}

 

On submit this would output something like 567 while in the database the only thing submitted is Array for the three entries.

It simply outputs,

 

5

4

6

 

 

 

There's also a script related message ("Form successfully submitted, click here to return to the main menu") but nothing else.

Which corresponds to the id value of a company in the database.

Impossible!  You should get an array on that print_r, as I changed the variable it pointed to.  Something else is wrong if that is it's actual output.

 

Whoops, I think what happened here was when I originally posted my browser started to hang so I resubmitted and you must have posted since then!

 

But to answer your question, after modifying what you posted to match the $_POST variable I get the following,

 

Array
(
    [0] => 8
    [1] => 9
    [2] => 6
)

 

Repeating for as many items there are within the array (in this case three, so the above message posted three times). I'm guessing the issue here is that the script is simply sending all the data at once for as many times as selected versus individually for as many times as selected?

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.