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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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';
   }
}
?>

Link to comment
Share on other sites

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>'; 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.