开发者

Text field not adding to an array when within a while loop

开发者 https://www.devze.com 2023-03-26 18:27 出处:网络
I have 2 columns on my add_membership page. The left column is \'Memberships added\' and the right column is \'Memberships too add\'.

I have 2 columns on my add_membership page. The left column is 'Memberships added' and the right column is 'Memberships too add'.

The code to present the 'memberships to add' is:

$credit=mysql_query("SELECT * FROM credit");
while($rowc=mysql_fetch_assoc($credit)){
$credit_id=$rowc['credit_id'];
if(in_array($credit_id, $_SESSION['pa']))
{}else{
$url_log=$rowc['logo_url'];
$name=$rowc['name'];
?>
<form action="#" method="post">
<div class="text_right"><input type="submit" value="Add" 
name="asubmit" class="search_input_submit" /></div>
<?php echo  "<img src='$url_log' width='50' height='50' alt='$name' title='$name'>"; ?>
<input type="checkbox" value="<?php echo $credit_id; ?>" name="credit_id" />
<strong><?php echo $name; ?></strong><br />
Membership Number:<input type="text" name"membership">
</form><br /><br />
<?php
}
}
?>

As you can see the Membership nu开发者_如何学运维mber input.

This is the code i've been trying to get to work to add the Membership Number:

<?php 
if($_POST['asubmit']){
$credit_id_add=$_POST['credit_id'];
$membership=$_POST['membership'];
mysql_query("INSERT INTO accreditation VALUES(NULL, $lid, $credit_id_add, 
'$membership','0' )");
echo $membership ;
$credit_id_add=null;
$referer = $_SERVER['HTTP_REFERER'];
echo "<meta http-equiv='refresh' content='1; url=$referer'> ";  
}
?>

I have 'echo'd' the variables I need to save and they all appear except for the 'Membership' which is why the error is occurring because it's trying to find the $membership value.

For the top section of the code, I have had to include the Credit ID into a checkbox as it doesn't seem to work by me just echo'ing for the $_POST to pick up, or putting it in a hidden text field.

Any light shed on this would be hugely appreciated.

Thanks in advance.


You are missing an = in your HTML:

<input type="text" name"membership">

should be:

<input type="text" name="membership">


If the first block of code is being used, you're also not closing the else condition:

else{
$url_log=$rowc['logo_url'];
$name=$rowc['name'];
?>

Should be:

else {
    $url_log = $rowc['logo_url'];
    $name = $rowc['name'];
}
?>

There should also be a } before the closing PHP tag to close the while loop. I'm not sure if that was just missed in a copy/paste from your page, but if it's in your code, fixing that will help.

Like this:

<?php
  $credit = mysql_query("SELECT * FROM credit");
  while ($rowc = mysql_fetch_assoc($credit)) {
      $credit_id = $rowc['credit_id'];
      if (!in_array($credit_id, $_SESSION['pa'])) {
          $url_log = $rowc['logo_url'];
          $name = $rowc['name'];
      }
  }
?>


General tip:

Instead of doing

if (some condition you only need when it's false) {
} else {
   ... do stuff here ...
}

just write

if (!some condition...) {
   ... do stuff here ...
}

Note the !, which is a logical negation, aka "not". It makes it far more explicit that you only want to deal with "false" conditions, and didn't just forget to fill in the "true" portion.

0

精彩评论

暂无评论...
验证码 换一张
取 消