Jump to content

$field = next(array) misses the first (0) value as null


makoshark

Recommended Posts

I am trying to teach myself PHP but some of the functions escape me! I am trying to pull out the value of an array, array(question[0], question[1]. question[2] ect...  I am using the next() function but it does not process the first array as the first value returns a null

<input name="question[]" type="checkbox" value="OrderTotalHigh" />
<input name="question[]" type="checkbox" value="ShippingHigh" />
.
.
.
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
    
@mysql_select_db("$DBName") or die("Unable to select database $DBName");
//         echo 'Please select only 3 or less'
           foreach ($_POST['question'] as $value)
      { 
            $Tfield = next($_POST['question']);
          $sql="INSERT INTO exit_reason ($Tfield) VALUES('1')";
         echo $sql . '<br>'; 
      }
   }
   else
   {
      echo 'Your Vote is Very Important to us.Please select 1 reason';
   }

What can I use to pass the string?

you don't need to use next.

just do this

mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
   
@mysql_select_db("$DBName") or die("Unable to select database $DBName");
//         echo 'Please select only 3 or less'
           foreach ($_POST['question'] as $value)
      {
            
          $sql="INSERT INTO exit_reason ($value) VALUES('1')";
         echo $sql . '<br>';
      }
   }
   else
   {
      echo 'Your Vote is Very Important to us.Please select 1 reason';
   }

the for each is looping through the $_POST['question'] array already.

That works great but now I need to know why I am not getting data into my table. here is the rest of the PHP part. Thanks Jim

    <input type="submit" value="Submit" name="submit">
    <input type="reset"  value="Reset" onkeydown="reset"> 

    </form>
</body>
</html>

<?php  


if (isset($_POST['submit']))
{
   if (isset($_POST['question']))
   {   
  $DBhost = "localhost";
  $DBuser = "root";
  $DBpass = "";
  $DBName = "exit_poll";
  $table = "exit_reason";
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
    
@mysql_select_db("$DBName") or die("Unable to select database $DBName");
//         echo 'Please select only 3 or less'
           foreach ($_POST['question'] as $value)
      {    
          //$query = INSERT INTO exit_reason [(question.value)](1);      include ("connect.php");  
          
          $sql="INSERT INTO exit_reason ($value) VALUES('1')";
//          $query=mysql_query ("INSERT INTO ($table) SET date = NOW(), (question) VALUE('1')");   SET date = NOW(),
         echo $sql . '<br>'; 
      }
   }
   else
   {
      echo 'Your Vote is Very Important to us.Please select at least 1 reason';
   }
}
?>

change this

mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

to this

$conn=mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

and you're not actually running the sql.

$sql="INSERT INTO exit_reason ($value) VALUES('1')";
$result=mysql_query($conn,$sql); 
//          $query=mysql_query ("INSERT INTO ($table) SET date = NOW(), (question) VALUE('1')");   SET date = NOW(),
echo $sql . '<br>'; 

should be

$sql="INSERT INTO exit_reason ($value) VALUES('1')";
mysql_query($conn, $sql); 
//          $query=mysql_query ("INSERT INTO ($table) SET date = NOW(), (question) VALUE('1')");   SET date = NOW(),
         echo $sql . '<br>'; 

Well almost I am getting an error "mysql_query() supplied argument is not a valid MySQL -link resource" with this line of code

mysql_query($conn, $sql);

$conn is set at a value of 2 as reported by the debugger. I have to let you know I am very excited to get this far! and I greatly appreciate your help. Eventually I want to put it on my zen cart solution as a survey. I will be sure to post it for others.

your query should be closer to this

INSERT INTO $table(date,question) values(now(),1)

 

you're combining an update query with an insert query and that won't work.  If you were updating it would this

update $table set date=now(), question=1 where YOURIDFIELD=ANID

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.