makoshark Posted June 10, 2009 Share Posted June 10, 2009 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? Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted June 10, 2009 Share Posted June 10, 2009 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. Quote Link to comment Share on other sites More sharing options...
makoshark Posted June 10, 2009 Author Share Posted June 10, 2009 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'; } } ?> Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted June 10, 2009 Share Posted June 10, 2009 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>'; Quote Link to comment Share on other sites More sharing options...
makoshark Posted June 10, 2009 Author Share Posted June 10, 2009 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. Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted June 10, 2009 Share Posted June 10, 2009 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 Quote Link to comment Share on other sites More sharing options...
makoshark Posted June 11, 2009 Author Share Posted June 11, 2009 I tried the changes you made but I am still getting the same error my $conn value is link=2 so when it gets to mysql_query($conn, $sql); it sayssupplied argument is not a valid MySQL -link resource Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.