Insert issue

Hi,
I’m trying to make this form insert names and existing ids into an artist table.
The name works fine but I’m not able to get the id (exhibitid) into the table.
Each exhibit can have multiple artist names attached to it. If the user wants to insert more artist names I also need it to insert the corresponding id.
The id comes with the url like this:
www.example.no/nayb/admin/newartist.php?id=38
Here is the code:


$aid = $_GET['id'];

if (isset($_POST['aname'])): 
  // Nye kunstnere er blitt lagt til
  // via denne formen.
  // New artists has been added


if(count($_POST))
{
	$len = count($_POST['aname']);
	for ($i=0; $i < $len; $i++)
	{

foreach ($_POST['aname'] as $i=>$aname) {

$sql = "INSERT INTO artist(`aname`, `exhibitid`) VALUES( '$aname', '$aid')";
	if (@mysql_query($sql)) {
    echo '<p>En kunstner er lagt til / New artist has been added</p>';
  } else {
    exit('<p>Feil ved innlegging av kunstner: ' . mysql_error() . '</p>');
  }
	}
}
}
?>
<p><a href="exhibitionlist.php">Tilbake til kunstnerlisten / Back</a></p>

<?php
else: // Allow the user to add artists
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div id="newlink">
<div>
<b>Kunstner:</b> 
<input type="text" name="aname[]" size="60"><br/>
<input type="hidden" name="exhibitid[]" value="<?php echo "$aid"; ?>">
</div>
</div>
<p id="addnew">
<a href="javascript:new_link()">Legg til kunstner / Add artist</a>
</p>

<input type="submit" value="LEGG TIL KUNSTNER(E) / ADD ARTIST(S)" />
</form>
<!-- Template -->
<div id="newlinktpl" style="display:none">
<div>
<b>Kunstner:</b> <input type="text" name="aname[]" size="60"><br/>
<input type="hidden" name="exhibitid[]" value="<?php echo "$aid"; ?>">
</div>
</div>
<?php endif; ?>

Thank you for any help!

Change this line:

$aid = $_GET['id']; 

to

$aid = $_POST['id']; 

Your’re trying to get the id value from the $_GET array but the form is sending the value via the $_POST array

Thanks for helping. I tried to change this but it doesn’t make any difference. Names go fine but the exhibitid field stays empty.

Youre not sending any GET or POST data from the form with the name ‘ID’, but at the top of the script you making $aid equal this non existant variable

Also the second part of your form isnt inside <form> tags

I tought I was sending the id variable with the url? It usually works fine when I’m updating with the WHERE clause (WHERE id=‘$id’).
How can I pass a variable with the url and insert it in a table?

(The form uses a javascript so that part isn’t a problem. It works fine inserting multiple names.)

In the form, the exhibit id is stored in a hidden field with name=“exhibitid”.

Since all artists in a form seem to have the same exhibit id, you might as well have only 1 hidden field with the exhibit id.

The value you put in that hidden field comes from $_GET[‘id’]. Where do you pass “id” in the query string? Is there a link in another page that links to this form, and that has the exhibit id in its query string?

When the user submits the form, that query string value is lost. Unless you add it in the action tag of the form. And then you wouldn’t need to put it in a hidden field in the form.

Well from the code yourve posted (I cant see the javascript function so I dont know what your doing insode that)

Your query is inserting the value $aid into the database field ‘exhibitid’

$aid is being set at the top of the script from $_GET[‘id’]

BUT

your form doesnt have any input fields with the name ‘id’ and you are sending in POST method not GET.

If you think that the ‘magic’ to link these events is being done in the javascript function newlink() then you’ll need to post that code to convince me. :wink:

The id comes from another page, artistlist.
Since the value is a “none-existing variable” I guess I will have to define it as I do in the edit page.
I have changed the code and I now get ‘array’ in the exhibitid field in the artist table.


&lt;?php include 'accesscontrol.php'; ?&gt;     
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;title&gt;Legg til kunstnere&lt;/title&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /&gt;
&lt;link href="default.css" rel="stylesheet" type="text/css" /&gt;
&lt;script type="text/javascript" src="addtwofields.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="general"&gt;
&lt;?php
$exid = $_POST['exhibitid'];

if (isset($_POST['aname'])): 
  // Nye kunstnere er blitt lagt til
  // via denne formen.

if(count($_POST))
{
	$len = count($_POST['aname']);
	for ($i=0; $i &lt; $len; $i++)
	{

foreach ($_POST['aname'] as $i=&gt;$aname) {

$sql = "INSERT INTO artist(`aname`, `exhibitid`) VALUES( '$aname', '$exid')";
	if (@mysql_query($sql)) {
    echo '&lt;p&gt;En kunstner er lagt til / New artist has been added&lt;/p&gt;';
  } else {
    exit('&lt;p&gt;Feil ved innlegging av kunstner: ' . mysql_error() . '&lt;/p&gt;');
  }
	}
}
}
?&gt;
&lt;p&gt;&lt;?php echo "&lt;a href='exhibitionlist.php?id=$gid'&gt;Tilbake til utstillingsoversikten / Back&lt;/a&gt;"; ?&gt;&lt;/p&gt;

&lt;?php
else: // Allow the user to add artists
?&gt;
&lt;?php
  $aid = $_GET['id'];

  $exhibit = @mysql_query(
    "SELECT id FROM exhibit WHERE id='$aid'");
  if (!$exhibit) {
    exit('&lt;p&gt;Error fetching exhibit details: ' .
        mysql_error() . '&lt;/p&gt;');
  }

  $exhibit = mysql_fetch_array($exhibit);
  $exid = $exhibit['id'];
?&gt;

&lt;form action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" method="post"&gt;
&lt;div id="newlink"&gt;
&lt;div&gt;
&lt;b&gt;Kunstner:&lt;/b&gt; 
&lt;input type="text" name="aname[]" size="60"&gt;&lt;br/&gt;
&lt;input type="hidden" name="exhibitid[]" value="&lt;?php echo "$exid"; ?&gt;"&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p id="addnew"&gt;
&lt;a href="javascript:new_link()"&gt;Legg til kunstner / Add artist&lt;/a&gt;
&lt;/p&gt;

&lt;input type="submit" value="LEGG TIL KUNSTNER(E) / ADD ARTIST(S)" /&gt;
&lt;/form&gt;
&lt;!-- Template --&gt;
&lt;div id="newlinktpl" style="display:none"&gt;
&lt;div&gt;
&lt;b&gt;Kunstner:&lt;/b&gt; &lt;input type="text" name="aname[]" size="60"&gt;&lt;br/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;?php endif; ?&gt;
&lt;/div&gt;&lt;!--general--&gt;
&lt;/body&gt;
&lt;/html&gt;

why are you specifing the fields as type array when you are only giving the option to add one artist at a time on your form?

I’m not sure what you mean. When I click “Add Artist” (not the button) the javascript adds another form field where the user writes a new artist name. When you use the Add Artist button it inserts all the names. Unfortunately without the ‘exhibitid’ value…

<input type="hidden" name="exhibitid" value="<?php echo "$exid"; ?>">

Perfect:) Thanks for your help!