Jump to content

[SOLVED] mysql posts "Array" instead of post form value


lilwing

Recommended Posts

I've got something like:

 

<?php

$query = mysql_query('SELECT username FROM users WHERE ID > 4 ORDER BY username, permissions ASC');
while($row = mysql_fetch_array($query)) {
  $list = $row['username'];
  echo("<li>");
  echo('<input type="radio" name="username[]" value="'.$list.'" />' . $list);
  echo("</li>");
}

?>

 

That works perfectly fine. The value in the html source is correct. However, this is part of a form, and that form tries to post to a table. Everything else posts fine, but this particular field posts "Array" instead of the values I want it to.

 

The query looks like this:

 

<?php

$contenttype = $_POST['contenttype'];
$contentid = $_POST['contentid'];
$contentname = $_POST['contentname'];
$contentowner = $_POST['username'];

$insert = 'INSERT INTO content (ContentOwner, ContentID, ContentType, ContentName) VALUES ("'.$contentowner.'", "'.$contentid.'", "'.$contenttype.'", "'.$contentname.'")';

	mysql_query($insert) or die(mysql_error());

?>

 

What have I done wrong?  ???

I'm guessing $contentowner is the one that's not working properly. Run the code below with your form and post the output (I commented out the mySQL bit as we don't need that at the minute). I added a print_r statement that prints an array out in a readable  way. You can use it to determine which part of the username array you want to use.

 

<?php
$contenttype = $_POST['contenttype'];
$contentid = $_POST['contentid'];
$contentname = $_POST['contentname'];
$contentowner = $_POST['username'];

// I added this.
print_r($contentowner);

/*
$insert = 'INSERT INTO content (ContentOwner, ContentID, ContentType, ContentName) VALUES ("'.$contentowner.'", "'.$contentid.'", "'.$contenttype.'", "'.$contentname.'")';
mysql_query($insert) or die(mysql_error());
*/
?>

Try it using like this..

 

$_REQUEST['username'][0]

Or $_REQUEST['username'][1]. The reason for posting my message above was to figure out which one, but I suppose guessing would be quicker. Just increment the number until you get the value you're looking for.

since username is a radio button, you're only going to have one checked in your form, so there's no reason to make name = 'username[]' an array.  just make it name = 'username' and it will post to $_POST['username'] like all your other vars.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.