Jump to content

Add POST results to array unless empty


cs.punk

Recommended Posts

$a = array( "{$_POST['a0']}",
                "{$_POST['a1']}",
                "{$_POST['a2']}",
     "{$_POST['a3']}",
     "{$_POST['a4']}",
     "{$_POST['a5']}",
     "{$_POST['a6']}",
     "{$_POST['a7']}",
     "{$_POST['a8']}",
     "{$_POST['a9']}"); // !?!?!?!? How do I make is so that if the user
		        // !?!?!?!?	only gave 4 answers (a1,a2,a3,a4), only
		        // !?!?!?!?	add those results to the array?

 

LoL a long isset command should work but thats some hefty coding...

Link to comment
https://forums.phpfreaks.com/topic/149788-add-post-results-to-array-unless-empty/
Share on other sites

I tried it.. Although it's correct it does not work in the way I want it... Heres the full code..

 

<?php
if ( isset($_POST['title']) && ($_POST['question']) && ($_POST['a1']) && ($_POST['a2']) )
{$title = "{$_POST['title']}";
  $question = "{$_POST['question']}";
  $author = "{$_SESSION['user']}";
  $date = "4:00 pm, 11 03 2009"; // !?!?!?!?*******			What should I type here to get the date??

  $count = 0;
  $a = array();

  while (isset($_POST['a' . $count]))
   {$a[] = $_POST['a' . $count];
$count++;
    }

  $con = mysqli_connect ("$dbhost","$dbuser","$dbpass","$dbname")
         or die ("Could not connect to server");

  $query = "INSERT INTO polls (title, question, author, date, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
            VALUES ('$title', '$question', '$author', '$date', '$a[0]', '$a[1]', '$a[2]', '$a[3]',                    '$a[4]','$a[5]', '$a[6]', '$a[7]', '$a[8]', '$a[9]')";		   
  $result = mysqli_query($con, $query)
            or die ( mysqli_error($result) );	

  $insert_id = mysqli_insert_id($con);
  $query2 = "INSERT INTO 'poll_ratings' (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, id)
             VALUES ('$a[0]', '$a[1]', '$a[2]', '$a[3]', '$a[4]', '$a[5]', '$a[6]', '$a[7]', '$a[8]',                     '$a[9]', '$insert_id')";
  $result2 = mysqli_query($con,$query2)
             or die ("Couldn’t execute query2." . mysql_error());

  echo "You poll has been added";
}
else
{echo "<form id=\"form1\" name=\"form1\" method=\"post\" action=\"add_poll.php\">
        <p class=\"smallheading\">
	 Title:	<input type=\"text\" name=\"title\" />
	<br />
	 Question:	<input type=\"text\" name=\"question\" />
	<br />
	 Answer 1:	<input type=\"text\" name=\"a0\" />
	<br />
	 Answer 2:		<input type=\"text\" name=\"a1\" />
	 <br />
	 Answer 3:		<input type=\"text\" name=\"a2\" />
	 <br />
	 Answer 4:		<input type=\"text\" name=\"a3\" />
	 <br />
	 Answer 5:		<input type=\"text\" name=\"a4\" />
	 <br />
	 Answer 6:		<input type=\"text\" name=\"a5\" />
	 <br />
	 Answer 7:		<input type=\"text\" name=\"a6\" />
	 <br />
	 Answer 8:		<input type=\"text\" name=\"a7\" />
	 <br />
	 Answer 9:		<input type=\"text\" name=\"a8\" />
	 <br />
	 Answer 10:		<input type=\"text\" name=\"a9\" />
	<input name=\"button\" type=\"submit\" value=\"Submit\"/>
	</form>
	</p>";
}

 

It still sends 'blank' entries into the table..? While I need them completely empty... As:

<?php

$con = mysqli_connect ("$dbhost","$dbuser","$dbpass","$dbname")
   or die ("Could not connect to server");
   
$ssql = "SELECT * FROM polls WHERE ID = '$pollid'";
$result = mysqli_query($con,$ssql)
          or die ("Couldn’t execute query." . mysql_error());
$row = mysqli_fetch_assoc($result);
extract($row);

echo "<p align=\"center\" class=\"mediumheading\">$title</p>";
echo "<p align=\"center\" class=\"paragraph\">$question</p>";

echo "<form action=\"poll_result.php\" name=\"vote_answer\">
   	   <input type=\"radio\" name=\"answer\" value=\"a0\"> $a0
   <br/>
       <input type=\"radio\" name=\"answer\" value=\"a1\"> $a1
   <br/>
       <input type=\"radio\" name=\"answer\" value=\"a2\"> $a2
   <br/>
       <input type=\"radio\" name=\"answer\" value=\"a3\"> $a3
   <br/>
       <input type=\"radio\" name=\"answer\" value=\"a4\"> $a4
   <br/>
       <input type=\"radio\" name=\"answer\" value=\"a5\"> $a5
   <br/>
       <input type=\"radio\" name=\"answer\" value=\"a6\"> $a6
   <br/>
       <input type=\"radio\" name=\"answer\" value=\"a7\"> $a7
   <br/>
       <input type=\"radio\" name=\"answer\" value=\"a8\"> $a8
   <br/>
       <input type=\"radio\" name=\"answer\" value=\"a9\"> $a9
      </form>";
?>

 

It would give a radio button for a empty cell...

 

Thanks for your help btw!

 

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.