Jump to content

Recommended Posts

I am new to using arrays..........so I don't really know what is wrong here, could someone please help, it is not posting properly.........I think it has to do with arrays:

<?php



$sql = mysql_query("SELECT * FROM Registration WHERE eventid='".$_GET['eventid']."'");
while($row = mysql_fetch_array($sql)){
$eventid = $row["eventid"];
$event = $row["event"];
$startdate = $row["startdate"];
$enddate = $row["enddate"];
$description = $row["description"];
$location = $row["location"];
$title1 = $row['title1'];
$title2 = $row['title2'];
$title3 = $row['title3'];
$title4 = $row['title4'];
$title5 = $row['title5'];
$title6 = $row['title6'];
$title7 = $row['title7'];
$title8 = $row['title8'];
$price1 = $row['price1'];
$price2 = $row['price2'];
$price3 = $row['price3'];
$price4 = $row['price4'];
$price5 = $row['price5'];
$price6 = $row['price6'];
$price7 = $row['price7'];
$price8 = $row['price8'];
$date1 = $row['date1'];
$date2 = $row['date2'];
$date3 = $row['date3'];
$date4 = $row['date4'];
$date5 = $row['date5'];
$date6 = $row['date6'];
$date7 = $row['date7'];
$date8 = $row['date8'];

    

//this will echo the contents of each db row as they are iterated in the loop

#############################
echo "<form action ='quickregister.php' method='post'>";
echo "<center><table border='1' width='600'>";

// Example of testing title1 for the userid (already registered) ...

echo "<tr><td width='300'>"; if (!empty($title1)) { echo  "<select name=\"title1\" id=\"title1\">
       <option value=\"$title1\">$title1</option>";}
    if (!empty($title2)) { echo  "<select name=\"title2\" id=\"title1\">
       <option value=\"$title2\">$title2</option>";}
    if (!empty($title3)) { echo  "<select name=\"title3\" id=\"title1\">
       <option value=\"$title3\">$title3</option>";}
    if (!empty($title4)) { echo  "<select name=\"title4\" id=\"title1\">
       <option value=\"$title4\">$title4</option>";}
    if (!empty($title5)) { echo  "<select name=\"title5\" id=\"title1\">
       <option value=\"$title5\">$title5</option>";}
    if (!empty($title6)) { echo  "<select name=\"title6\" id=\"title6\">
       <option value=\"$title6\">$title6</option>";}
    if (!empty($title7)) { echo  "<select name=\"title7\" id=\"title1\">
       <option value=\"$title7\">$title7</option>";}
    if (!empty($title8)) { echo  "<select name=\"title8\" id=\"title1\">
       <option value=\"$title8\">$title8</option>";}
   
   
     echo "</select>"; 
 echo "<input type='hidden' name='eventid' value='$eventid'>";
  echo "<input type='hidden' name='event' value='$event'>";}

?>
<p><form id="form1" name="form1" method="post" action="quickregister.php">
   <center>
     <label for="title1"></label>
     <table width="323" border="1" cellspacing="2" cellpadding="2">
       <tr>
         <th width="58" scope="col"> </th>
         <th width="245" scope="col"> </th>
        </tr>
       <tr>
         <td> </td>
         <td>
          </td>
        </tr>
       <tr>
         <td>
          <br /></td>
         <td><?php    
    
    $select = mysql_query("SELECT * FROM members") or die(mysql_error());
    while ($member = mysql_fetch_array($select)) {
        echo '<input type="checkbox" name="user[]" value="'.$member[name].'">'.$member[name].'<br><input type="hidden" name="userid[]" value="'.$member[userid].'"><input type="hidden" name="email[]" value="'.$member[email].'">';
    }
    
?></td>
        </tr>
       <tr>
         <td> </td>
         <td><input type="hidden" name="eventname" id="<?php echo $eventname; ?>" /></td>
        </tr>
       <tr>
         <td> </td>
         <td><input type="submit" name="submit" id="submit" value="Post" /></td>
        </tr>
    </table></center>
   
</form>

<?php
if ($_POST['submit']){
    foreach ($_POST[user] as $key => $value)
 foreach ($_POST[userid] as $key => $value1)
  foreach ($_POST[email] as $key => $value2){
    
    // [..] code that adds the message to the database

        $title1 = $_POST['title1'];
        $title2 = $_POST['title2'];
        $title3 = $_POST['title3'];
        $title4 = $_POST['title4'];
        $title5 = $_POST['title5'];
        $title6 = $_POST['title6'];
        $title7 = $_POST['title7'];
        $title8 = $_POST['title8'];
       
        $eventid = $_POST['eventid'];
	        $event = $_POST['event'];
    
    $query ="insert into Events(title1, title2, title3, title4, title5, title6, title7, title8, userid, name,  eventid, event, email, price1, price2, price3, price4, price5, price6, price7, price8, barcode, verified) VALUES ('$title1', '$title2', '$title3', '$title4', '$title5', '$title6', '$title7', '$title8', '$value1', '$value','$eventid', '$event', '$value2', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick')";



mysql_query($query) or die('Error, query failed');



}
exit();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/234548-problems-with-arrays/
Share on other sites

First you could clean up your code using the following!  It will turn each field into a $variable of the same name!

If you wanted to, makes things look really nice.

<?php
$row= $database->fetchassoc($sql);

foreach($row as $field => $value)
{
$this->$field = $value;
}
// Not perfect, but you might get the idea!

 

As for the $_GET I would highly recommend running it through some sort of clean up.

 

Someone could inject their own SQL into your code.

 

<?php
function escapeMysql($str)
{

if(get_magic_quotes_gpc()) : $str= stripslashes($str); endif;

$str= mysql_real_escape_string($str); // Will help prevent mysql injection!
}

$myvar = escapeMysql($_GET['eventid'];

$sql = mysql_query("SELECT * FROM Registration WHERE eventid='".$myvar."'");
?>

 

Also would want to run any $_POST any other values you get from User Input through that function.  First rule of coding: Never trust user input!

 

Now let me look over your code and see what's going on with the array error.

 

Edit: Can you post an example of what it's turning out to be?

I guess I misinterpreted what you meant by it isn't posting properly.

 

I can't see right off the bat anything wrong with it other than what I pointed out.

 

I do recommend using mysql_fetch_assoc() though, I believe it's quicker.

 

Does any of it work?  I.E. Populate Select Field?  If it isn't there are a few solutions to that.

If it does populate: Where does it stop working?

 

Edit: And on the for I posted!  I have a Database class so you'ld change the $database->fetchassoc to the mysql_fetch_assoc or mysql_fetch_array!  Sorry I didn't notice that when I pulled it out of my code.

yeah it works just fine.........maybe this will give you idea why it no work...........I am trying to post multiple values at a time.........it could be a problem with a loop too maybe?  but this is like a page where I select checkboxes and register the selected for an event.

Edit: SCRATCH THAT, just found out it doesn't matter!

 

Also on that foreach, I use it in a class which is why $this-> is there.  Change it to something else and you should be able to pull your information like $this->title1.  I'd change it to $events->$field  or something you have not already used.

 

Alright this time I actually think I have something, took me a while to locate it.

 

You've got a nested <form> and you only close out one of them.

 

echo "<form action ='quickregister.php' method='post'>";
echo "<center><table border='1' width='600'>";

// Example of testing title1 for the userid (already registered) ...

echo "<tr><td width='300'>"; if (!empty($title1)) { echo  "<select name=\"title1\" id=\"title1\">
       <option value=\"$title1\">$title1</option>";}
    if (!empty($title2)) { echo  "<select name=\"title2\" id=\"title1\">
       <option value=\"$title2\">$title2</option>";}
    if (!empty($title3)) { echo  "<select name=\"title3\" id=\"title1\">
       <option value=\"$title3\">$title3</option>";}
    if (!empty($title4)) { echo  "<select name=\"title4\" id=\"title1\">
       <option value=\"$title4\">$title4</option>";}
    if (!empty($title5)) { echo  "<select name=\"title5\" id=\"title1\">
       <option value=\"$title5\">$title5</option>";}
    if (!empty($title6)) { echo  "<select name=\"title6\" id=\"title6\">
       <option value=\"$title6\">$title6</option>";}
    if (!empty($title7)) { echo  "<select name=\"title7\" id=\"title1\">
       <option value=\"$title7\">$title7</option>";}
    if (!empty($title8)) { echo  "<select name=\"title8\" id=\"title1\">
       <option value=\"$title8\">$title8</option>";}
   
   
     echo "</select>"; 
 echo "<input type='hidden' name='eventid' value='$eventid'>";
  echo "<input type='hidden' name='event' value='$event'>";}

?>
<p><form id="form1" name="form1" method="post" action="quickregister.php">

 

I do believe that would throw off the submit button!  ^.^

Let me know if that works.

no, still not working  :-[

<?php
if ($_POST[submit]){
    foreach ($_POST[user] as $key => $value){

    
    // [..] code that adds the message to the database

        $title1 = $_POST['title1'];
        $title2 = $_POST['title2'];
        $title3 = $_POST['title3'];
        $title4 = $_POST['title4'];
        $title5 = $_POST['title5'];
        $title6 = $_POST['title6'];
        $title7 = $_POST['title7'];
        $title8 = $_POST['title8'];
        $value1 = $_POST['userid'];
        $value2 = $_POST['email'];
       
        $eventid = $_POST['eventid'];
	        $event = $_POST['event'];
    
    $query ="insert into Events(title1, title2, title3, title4, title5, title6, title7, title8, userid, name,  eventid, event, email, price1, price2, price3, price4, price5, price6, price7, price8, barcode, verified) VALUES ('$title1', '$title2', '$title3', '$title4', '$title5', '$title6', '$title7', '$title8', '$value1', '$value','$eventid', '$event', '$value2', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick')";


  
mysql_query($query) or die('Error, query failed');


}
}
?>

 

echo "<form action ='quickregister.php' method='post'>";
echo "<center><table border='1' width='600'>";

// Example of testing title1 for the userid (already registered) ...

echo "<tr><td width='300'>"; if (!empty($title1)) { echo  "<select name=\"title1\" id=\"title1\">
       <option value=\"$title1\">$title1</option>";}
    if (!empty($title2)) { echo  "<select name=\"title2\" id=\"title1\">
       <option value=\"$title2\">$title2</option>";}
    if (!empty($title3)) { echo  "<select name=\"title3\" id=\"title1\">
       <option value=\"$title3\">$title3</option>";}
    if (!empty($title4)) { echo  "<select name=\"title4\" id=\"title1\">
       <option value=\"$title4\">$title4</option>";}
    if (!empty($title5)) { echo  "<select name=\"title5\" id=\"title1\">
       <option value=\"$title5\">$title5</option>";}
    if (!empty($title6)) { echo  "<select name=\"title6\" id=\"title6\">
       <option value=\"$title6\">$title6</option>";}
    if (!empty($title7)) { echo  "<select name=\"title7\" id=\"title1\">
       <option value=\"$title7\">$title7</option>";}
    if (!empty($title8)) { echo  "<select name=\"title8\" id=\"title1\">
       <option value=\"$title8\">$title8</option>";}
   
   
     echo "</select>"; 
 echo "<input type='hidden' name='eventid' value='$eventid'>";
  echo "<input type='hidden' name='event' value='$event'>";}

?> <p>
  <center>
     <table width="323" border="1" cellspacing="2" cellpadding="2">
       <tr>
         <th width="58" scope="col"> </th>
         <th width="245" scope="col"> </th>
       </tr>
       <tr>
         <td> </td>
         <td>
         </td>
       </tr>
       <tr>
         <td>
          <br /></td>
         <td><?php    
    
    $select = mysql_query("SELECT * FROM members") or die(mysql_error());
    while ($member = mysql_fetch_array($select)) {
        echo '<input type="checkbox" name="user[]" value="'.$member[name].'">'.$member[name].'<br><input type="hidden" name="userid" value="'.$member[userid].'"><input type="hidden" name="email" value="'.$member[email].'">';
    }
    
?></td>
       </tr>
       <tr>
         <td> </td>
         <td><input type="hidden" name="event" id="<?php echo $event; ?>" /></td>
       </tr>
       <tr>
         <td> </td>
         <td><input type="submit" name="submit" id="submit" value="Post" /></td>
       </tr>
    </table></center>
   
</form>

Well there looks to be quite a few mishaps in your code, either that or I just don't understand the purpose.

 

For one:

<?php
foreach ($_POST[user] as $key => $value){
?>

 

I see userid in the rest of the code, but don't actually see user anywhere.

 

To tidy up that code I would go with something like this

 

<?php
$post = new stdClass();

foreach($_POST as $field => $value)
{
$post->$field = $value;
}

echo $post->title1; // anything after $post-> would be the field title it was given.
?>

 

That will clear up that long list of variables you have.

 

Also run this to remove the mysql injection!

 

<?php
function escapeMysql($str)
{
     if(get_magic_quotes_gpc()) : $str = stripslashes($str); endif;

     $str = mysql_real_escape_string($str); // Will help prevent mysql injection!

     return $str;
}
     

$post = new stdClass();

foreach($_POST as $field => $value)
{
$post->$field = escapeMysql($value);
}
?>

 

Trust me, you really want to escape the quotation marks that users can enter to inject or really mess with your database.  If someone who realizes you don't have your input escaped is actually evil enough, they will ruin your database.

 

I'm not sure what you have going on with the foreach, but if you don't have a $_POST[user], it won't really do you any good.

 

<?php

function escapeMysql($str)
{
     if(get_magic_quotes_gpc()) : $str = stripslashes($str); endif;

     $str = mysql_real_escape_string($str); // Will help prevent mysql injection!

     return $str;
}
     

$post = new stdClass();


if ($_POST[submit])
{

foreach($_POST as $field => $value)
{
	$post->$field = escapeMysql($value);
}
    
    $query ="insert into Events(title1, title2, title3, title4, title5, title6, title7, title8, userid, name,  eventid, event, email, price1, price2, price3, price4, price5, price6, price7, price8, barcode, verified) VALUES ('$post->title1', '$post->title2', '$post->title3', '$post->title4', '$post->title5', '$post->title6', '$post->title7', '$post->title8', '$post->value1', '$post->value','$post->eventid', '$post->event', '$post->value2', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick')";
  
mysql_query($query) or die('Error, query failed');	
}
?>

 

The $post->variable will hold any and all data posted to it, and will automatically escape any quotes that may exist.

 

I'd go into more detail, but mres is definitely a life saver.

 

Try that and see what you get, because from what I see... the foreach is cancelling the post effect.

Looking at the code in your first post, It could be down to you using 3 nested foreach loops when running your query.

 foreach ($_POST[user] as $key => $value)
 foreach ($_POST[userid] as $key => $value1)
  foreach ($_POST[email] as $key => $value2){
...

Why the need for 3?

 

From I see you only need to use one. Having nested foreach loops you're just asking for trouble.

 foreach ($_POST[user] as $key => $value) {
       $userid = $_POST['userid'][$key];
       $email = $_POST['email'][$key];
...

ok, well now it is posting userid's of 12+........nothing below......why?

 

<?php
function escapeMysql($str)
{
     if(get_magic_quotes_gpc()) : $str = stripslashes($str); endif;

     $str = mysql_real_escape_string($str); // Will help prevent mysql injection!

     return $str;
}

$myvar = escapeMysql($_GET['eventid']);

$sql = mysql_query("SELECT * FROM Registration WHERE eventid='".$myvar."'");
while($row = mysql_fetch_assoc($sql)){
$eventid = $row["eventid"];
$event = $row["event"];
$startdate = $row["startdate"];
$enddate = $row["enddate"];
$description = $row["description"];
$location = $row["location"];
$title1 = $row['title1'];
$title2 = $row['title2'];
$title3 = $row['title3'];
$title4 = $row['title4'];
$title5 = $row['title5'];
$title6 = $row['title6'];
$title7 = $row['title7'];
$title8 = $row['title8'];
$price1 = $row['price1'];
$price2 = $row['price2'];
$price3 = $row['price3'];
$price4 = $row['price4'];
$price5 = $row['price5'];
$price6 = $row['price6'];
$price7 = $row['price7'];
$price8 = $row['price8'];
$date1 = $row['date1'];
$date2 = $row['date2'];
$date3 = $row['date3'];
$date4 = $row['date4'];
$date5 = $row['date5'];
$date6 = $row['date6'];
$date7 = $row['date7'];
$date8 = $row['date8'];

    

//this will echo the contents of each db row as they are iterated in the loop

#############################
echo ' <form id="form1" name="form1" method="post" action="quickregister.php">';
echo "<center><table border='1' width='600'>";

// Example of testing title1 for the userid (already registered) ...

echo "<tr><td width='300'>"; if (!empty($title1)) { echo  "<select name=\"title1\" id=\"title1\">
       <option value=\"$title1\">$title1</option>";}
    if (!empty($title2)) { echo  "<select name=\"title2\" id=\"title1\">
       <option value=\"$title2\">$title2</option>";}
    if (!empty($title3)) { echo  "<select name=\"title3\" id=\"title1\">
       <option value=\"$title3\">$title3</option>";}
    if (!empty($title4)) { echo  "<select name=\"title4\" id=\"title1\">
       <option value=\"$title4\">$title4</option>";}
    if (!empty($title5)) { echo  "<select name=\"title5\" id=\"title1\">
       <option value=\"$title5\">$title5</option>";}
    if (!empty($title6)) { echo  "<select name=\"title6\" id=\"title6\">
       <option value=\"$title6\">$title6</option>";}
    if (!empty($title7)) { echo  "<select name=\"title7\" id=\"title1\">
       <option value=\"$title7\">$title7</option>";}
    if (!empty($title8)) { echo  "<select name=\"title8\" id=\"title1\">
       <option value=\"$title8\">$title8</option>";}
   
   
     echo "</select>"; 
 echo "<input type='hidden' name='eventid' value='$eventid'>";
  echo "<input type='hidden' name='event' value='$event'>";}

?> <p>
  <center> <table width="323" border="1" cellspacing="2" cellpadding="2">
      <tr>
        <th width="58" scope="col"> </th>
        <th width="245" scope="col"> </th>
      </tr>
      <tr>
        <td> </td>
        <td>
       </td>
      </tr>
      <tr>
        <td>
          <br /></td>
        <td><?php    
    
    $select = mysql_query("SELECT * FROM members") or die(mysql_error());
    while ($member = mysql_fetch_array($select)) {
        echo '<input type="checkbox" name="user[]" value="'.$member[name].'">'.$member[name].'<br><input type="hidden" name="email1[]" value="'.$member[email].'"><input type="text" name="userid1[]" value="'.$member[userid].'"><input type="text" name="id1[]" value="'.$member[userid].','.$eventid.'">';
    }
    
?></td>
      </tr>
      <tr>
        <td> </td>
        <td>
          <!-- Textarea gets replaced with TinyMCE, remember HTML in a textarea should be encoded --></td>
      </tr>
      <tr>
        <td> </td>
        <td><input type="submit" name="submit" id="submit" value="Post" /></td>
      </tr>
    </table></center>
    <p> </p>
    <p> </p>
    <p> </p>
    <p> </p>
  </form>

<?php
if ($_POST[submit]){
  foreach ($_POST[user] as $key => $value) {
       $userid1 = $_POST[userid1][$userid];
       $email1 = $_POST[email1][$key];
       $id1 = $_POST[id1][$key];
	   

    
    // [..] code that adds the message to the database

        $title1 = $_POST['title1'];
        $title2 = $_POST['title2'];
        $title3 = $_POST['title3'];
        $title4 = $_POST['title4'];
        $title5 = $_POST['title5'];
        $title6 = $_POST['title6'];
        $title7 = $_POST['title7'];
        $title8 = $_POST['title8'];
  
       
       
        $eventid = $_POST['eventid'];
	        $event = $_POST['event'];
    
    $query ="insert into Events(title1, title2, title3, title4, title5, title6, title7, title8, userid, name,  eventid, event, email, price1, price2, price3, price4, price5, price6, price7, price8, barcode, verified, id) VALUES ('$title1', '$title2', '$title3', '$title4', '$title5', '$title6', '$title7', '$title8', '$userid1', '$value','$eventid', '$event', '$email1', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', '$id1')";




mysql_query($query) or die(mysql_error());
echo mysql_error();
echo $query;
echo "Registrations verified";


}}
?>

ok, well basically, everytime I begin trying to put a new piece in, like lets say, I register 2 people the first time, then 3 the second, well the first time, it gives id's 12 and 13, then second time ids of 12,13, and 14.  I need to know why it isn't assigning the actual person's id. 

part of the issue could be that the person with 12 is the first result?

First result of what? In your registration table. If the userid field in that table is set to auto_increment then yes that may be why. You can reset the counter so it starts at 1 but that I do not recommend. Especially if you already have data in your registration and events table, as then there will be no correlation between a user and their events. I assume that's how you're linking an event to a user.

there is not auto increment, primary key though is id, which is the userid,eventid (1,25 for ex.) 12 is the first userid that is returned from the array piece.  I need to know how I can make it post the correct userid that is returned from the one piece, I have set fields to text and they show the correct userid for each person, but it is only posting the first persons userid that is in the members table, does that make sense?

Just read your code from this post.

ok, well now it is posting userid's of 12+........nothing below......why?

 

<?php
function escapeMysql($str)
{
     if(get_magic_quotes_gpc()) : $str = stripslashes($str); endif;

     $str = mysql_real_escape_string($str); // Will help prevent mysql injection!

     return $str;
}

$myvar = escapeMysql($_GET['eventid']);

$sql = mysql_query("SELECT * FROM Registration WHERE eventid='".$myvar."'");
while($row = mysql_fetch_assoc($sql)){
$eventid = $row["eventid"];
$event = $row["event"];
$startdate = $row["startdate"];
$enddate = $row["enddate"];
$description = $row["description"];
$location = $row["location"];
$title1 = $row['title1'];
$title2 = $row['title2'];
$title3 = $row['title3'];
$title4 = $row['title4'];
$title5 = $row['title5'];
$title6 = $row['title6'];
$title7 = $row['title7'];
$title8 = $row['title8'];
$price1 = $row['price1'];
$price2 = $row['price2'];
$price3 = $row['price3'];
$price4 = $row['price4'];
$price5 = $row['price5'];
$price6 = $row['price6'];
$price7 = $row['price7'];
$price8 = $row['price8'];
$date1 = $row['date1'];
$date2 = $row['date2'];
$date3 = $row['date3'];
$date4 = $row['date4'];
$date5 = $row['date5'];
$date6 = $row['date6'];
$date7 = $row['date7'];
$date8 = $row['date8'];

    

//this will echo the contents of each db row as they are iterated in the loop

#############################
echo ' <form id="form1" name="form1" method="post" action="quickregister.php">';
echo "<center><table border='1' width='600'>";

// Example of testing title1 for the userid (already registered) ...

echo "<tr><td width='300'>"; if (!empty($title1)) { echo  "<select name=\"title1\" id=\"title1\">
       <option value=\"$title1\">$title1</option>";}
    if (!empty($title2)) { echo  "<select name=\"title2\" id=\"title1\">
       <option value=\"$title2\">$title2</option>";}
    if (!empty($title3)) { echo  "<select name=\"title3\" id=\"title1\">
       <option value=\"$title3\">$title3</option>";}
    if (!empty($title4)) { echo  "<select name=\"title4\" id=\"title1\">
       <option value=\"$title4\">$title4</option>";}
    if (!empty($title5)) { echo  "<select name=\"title5\" id=\"title1\">
       <option value=\"$title5\">$title5</option>";}
    if (!empty($title6)) { echo  "<select name=\"title6\" id=\"title6\">
       <option value=\"$title6\">$title6</option>";}
    if (!empty($title7)) { echo  "<select name=\"title7\" id=\"title1\">
       <option value=\"$title7\">$title7</option>";}
    if (!empty($title8)) { echo  "<select name=\"title8\" id=\"title1\">
       <option value=\"$title8\">$title8</option>";}
   
   
     echo "</select>"; 
 echo "<input type='hidden' name='eventid' value='$eventid'>";
  echo "<input type='hidden' name='event' value='$event'>";}

?> <p>
  <center> <table width="323" border="1" cellspacing="2" cellpadding="2">
      <tr>
        <th width="58" scope="col"> </th>
        <th width="245" scope="col"> </th>
      </tr>
      <tr>
        <td> </td>
        <td>
       </td>
      </tr>
      <tr>
        <td>
          <br /></td>
        <td><?php    
    
    $select = mysql_query("SELECT * FROM members") or die(mysql_error());
    while ($member = mysql_fetch_array($select)) {
        echo '<input type="checkbox" name="user[]" value="'.$member[name].'">'.$member[name].'<br><input type="hidden" name="email1[]" value="'.$member[email].'"><input type="text" name="userid1[]" value="'.$member[userid].'"><input type="text" name="id1[]" value="'.$member[userid].','.$eventid.'">';
    }
    
?></td>
      </tr>
      <tr>
        <td> </td>
        <td>
          <!-- Textarea gets replaced with TinyMCE, remember HTML in a textarea should be encoded --></td>
      </tr>
      <tr>
        <td> </td>
        <td><input type="submit" name="submit" id="submit" value="Post" /></td>
      </tr>
    </table></center>
    <p> </p>
    <p> </p>
    <p> </p>
    <p> </p>
  </form>

<?php
if ($_POST[submit]){
  foreach ($_POST[user] as $key => $value) {
       $userid1 = $_POST[userid1][$userid];
       $email1 = $_POST[email1][$key];
       $id1 = $_POST[id1][$key];
	   

    
    // [..] code that adds the message to the database

        $title1 = $_POST['title1'];
        $title2 = $_POST['title2'];
        $title3 = $_POST['title3'];
        $title4 = $_POST['title4'];
        $title5 = $_POST['title5'];
        $title6 = $_POST['title6'];
        $title7 = $_POST['title7'];
        $title8 = $_POST['title8'];
  
       
       
        $eventid = $_POST['eventid'];
	        $event = $_POST['event'];
    
    $query ="insert into Events(title1, title2, title3, title4, title5, title6, title7, title8, userid, name,  eventid, event, email, price1, price2, price3, price4, price5, price6, price7, price8, barcode, verified, id) VALUES ('$title1', '$title2', '$title3', '$title4', '$title5', '$title6', '$title7', '$title8', '$userid1', '$value','$eventid', '$event', '$email1', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', '$id1')";




mysql_query($query) or die(mysql_error());
echo mysql_error();
echo $query;
echo "Registrations verified";


}}
?>

Which I guess is the most recent version of your code. I didn't actually look at your code properly last time round. Now that I have reviewed it. I'm struggling to work out what you're trying to do. My first thought was you're editing multiple events for a specific user.

 

But then I just spotted this

        <td><?php

    $select = mysql_query("SELECT * FROM members") or die(mysql_error());
    while ($member = mysql_fetch_array($select)) {
        echo '<input type="checkbox" name="user[]" value="'.$member[name].'">'.$member[name].'<br><input type="hidden" name="email1[]" value="'.$member[email].'"><input type="text" name="userid1[]" value="'.$member[userid].'"><input type="text" name="id1[]" value="'.$member[userid].','.$eventid.'">';
    }

?></td>

which now makes me think your are trying to link multiple users to a specific event.

 

Can you confirm what the script is supposed to do.

 

 

so basically I have 6 main rows, userid, eventid, event name, subevent names, and user's name and an id column(eventid, userid(unique)).  I need to make it so that when I register a user for an event, the correct userid will post, not userid's starting after 12.  The first name on the page is associated with userid 12, so that is where it is getting it from and it is not grabbing any other userids besides for that one................so If I were to select all users, I am sure it would work fine, but if I only select 2 users lets say userids of 1 and 2, then it posts it as userids of 12 and 13, not 1 and 2.  I have attached some pics to show what it is doing.  so in the picture, whether I select the last one or the middle one, the userid's do not correspond to person when they post.  first box is userid, second is id.........every two boxes.............does this make sense?

 

[attachment deleted by admin]

Okay I now understand what your code is supposed to do. You want it to add the members you have chosen to an event.

 

Looking at the screenshoot above, your form is displaying like due to this line of code.

echo '<input type="checkbox" name="user[]" value="'.$member[name].'">'.$member[name].'<br><input type="hidden" name="email1[]" value="'.$member[email].'"><input type="text" name="userid1[]" value="'.$member[userid].'"><input type="text" name="id1[]" value="'.$member[userid].','.$eventid.'">';

You need to wrap that line of code in a block level element, such as a div.

 

On to why it may be displaying your users in an disorderly fashion. This maybe due to you not telling mysql to fetch the results in a particular order.

    $select = mysql_query("SELECT * FROM members") or die(mysql_error());

You'll want to use an ORDER BY clause to sort the data, in your case the userid field. So we'll change the query to

    $select = mysql_query("SELECT * FROM members ORDER BY userid ASC") or die(mysql_error());

That should now display the members in ascending order based on the userid. In order for this to work correctly make sure the data type of the userid column is set to INT in your table schema.

 

Try this code and see if the script works

<?php

if(isset($_POST['submit']))
{
    if(isset($_POST['event_members']) && is_array($_POST['event_members']))
    {
        $members = $_POST['members'];

        foreach($_POST['event_members'] as $userid)
        {
            $name = $members[$userid]['name'];
            $email = $members[$userid]['email'];
            $id   = $members[$userid]['id'];

            $eventid = $_POST['eventid'];
            $event = $_POST['event'];

            for($i = 1; $i <= 8; $i++)
                ${"title$i"} = isset($_POST["title$i"]) ? $_POST["title$i"] : '';

            $query_values[] = "('$title1', '$title2', '$title3', '$title4', '$title5', '$title6', '$title7', '$title8', '$userid', '$name','$eventid', '$event', '$email', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', 'quick', '$id')";
        }

        $query = "insert into Events (\n\ttitle1, title2, title3, title4, title5, title6, title7, title8, userid, name,  eventid, event, email, \n\t" .
                                     "price1, price2, price3, price4, price5, price6, price7, price8, barcode, verified, id\n)\nVALUES\n\t";
        $query .= implode(",\n\t", $query_values);

        echo "Generated query: <pre>$query</pre>";

        $result = mysql_query($query);

        if(!$result)
            trigger_error("Mysql Error!" . mysql_error());
        else
            echo "Added " . mysql_affected_rows() . " new users to the event!";
    }
}

$myvar = escapeMysql($_GET['eventid']);

$sql = mysql_query("SELECT * FROM Registration WHERE eventid='".$myvar."'");

if(mysql_num_rows($sql) == 1)
{
    $row = mysql_fetch_assoc($sql);

$eventid = $row["eventid"];
    $event = $row["event"];
    $startdate = $row["startdate"];
    $enddate = $row["enddate"];
    $description = $row["description"];
    $location = $row["location"];
    $title1 = $row['title1'];
    $title2 = $row['title2'];
    $title3 = $row['title3'];
    $title4 = $row['title4'];
    $title5 = $row['title5'];
    $title6 = $row['title6'];
    $title7 = $row['title7'];
    $title8 = $row['title8'];
    $price1 = $row['price1'];
    $price2 = $row['price2'];
    $price3 = $row['price3'];
    $price4 = $row['price4'];
    $price5 = $row['price5'];
    $price6 = $row['price6'];
    $price7 = $row['price7'];
    $price8 = $row['price8'];
    $date1 = $row['date1'];
    $date2 = $row['date2'];
    $date3 = $row['date3'];
    $date4 = $row['date4'];
    $date5 = $row['date5'];
    $date6 = $row['date6'];
    $date7 = $row['date7'];
    $date8 = $row['date8'];
?>
<form id="form1" name="form1" method="post" action="">
<table border='1' width='600'>
    <tr>
        <td width='300'>
        <?php
            for($i = 1; $i <= 8; $i++):

                $titleNum = "title$i";
                $titleText = $$titleNum;

                if(!empty($titleText)): ?>

            <input type="text" name="<?php echo $titleNum; ?>" value="<?php echo $titleText; ?>" />

        <?php
                endif;
            endfor; ?>

            <input type="hidden" name="eventid" value="<?php echo $eventid; ?>">
            <input type="hidden" name="event" value="<?php echo $event; ?>">

            <table width="323" border="1" cellspacing="2" cellpadding="2">
                <tr>
                    <th width="58" scope="col"> </th>
                    <th width="245" scope="col"> </th>
                </tr>
                <tr>
                    <td> </td>
                    <td></td>
                </tr>
                <tr>
                <td><br /></td>
                <td>
                <?php

                    $select = mysql_query("SELECT * FROM members ORDER BY userid ASC") or die(mysql_error());

                    while ($member = mysql_fetch_array($select)):
                        $userid = $member['userid'];
                ?>
                    <p>
                        <input type="checkbox" name="event_members[]" value="<?php echo $userid; ?>"><?php echo $member['name']; ?><br>
                        <input type="text" name="members[<?php echo $userid; ?>][name]" value="<?php echo $member['name']; ?>">
                        <input type="text" name="members[<?php echo $userid; ?>][email]" value="<?php echo $member['email']; ?>">
                        <input type="hidden" name="members[<?php echo $userid; ?>][id]" value="<?php echo $member['userid']; ?>,<?php echo $eventid; ?>">
                    </p>
                <?php endwhile; ?>
            </td>
      </tr>
      <tr>
        <td> </td>
        <td><!-- Textarea gets replaced with TinyMCE, remember HTML in a textarea should be encoded --></td>
    </tr>
    <tr>
        <td> </td>
        <td><input type="submit" name="submit" id="submit" value="Post" /></td>
    </tr>
</table>
</form>
<?php
}
else
{
    echo "Invalid event id";
}
?>

 

 

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.