Jump to content

need help with an array.... and mysql ...


imarockstar

Recommended Posts

I have this script wich grabs all the post data from a form and puts it into an array then inserts it into a database .. and it works great .. but I added a column ... and I cant figure out how to add it to the array to be place into the database ... below is the code that works .. and then below that is the code that I added .. that does not work ...

 

 

working code

 

<?php
session_start();
include("connect.php");

$MembersInfo = $_SESSION['membersInfo'];
$id = $MembersInfo['id'];




$sql="SELECT * FROM gq_answers where userid = '$id' ";
$result=mysql_query($sql);
$num_rows = mysql_num_rows($result);

if ($num_rows >= 1) { header("Location: ../dashboard.php?e=1"); }

else { 

$keys = array_values(array_keys($_POST));
$values = array_values($_POST);
for($a=0; $a<count($keys); $a++){
mysql_query("INSERT INTO gq_answers(userid,qid,gqa_answer)Values('$id','$keys[$a]','$values[$a]')") or die(mysql_error());
}


header("Location: ../dashboard.php");

}

?>

 

 

code that does not work ..

i tried adding another value ... to be inserted ..

 

<?php
session_start();
include("connect.php");

$MembersInfo = $_SESSION['membersInfo'];
$id = $MembersInfo['id'];




$sql="SELECT * FROM gq_answers where userid = '$id' ";
$result=mysql_query($sql);
$num_rows = mysql_num_rows($result);

if ($num_rows >= 1) { header("Location: ../dashboard.php?e=1"); }

else { 

$keys = array_values(array_keys($_POST));
$values = array_values($_POST);
for($a=0; $a<count($keys); $a++){
mysql_query("INSERT INTO gq_answers(userid,qid,gqa_answer,weight)Values('$id','$keys[$a]','$values[$a]','$values[$b]')") or die(mysql_error());
}


header("Location: ../dashboard.php");

}

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/165927-need-help-with-an-array-and-mysql/
Share on other sites

What is the $b index of the $value array?  I don't see where the $b index would be coming from?  The $a index is defined in the for loop but $b is not defined.

 


<?php
session_start();
include("connect.php");

$MembersInfo = $_SESSION['membersInfo'];
$id = $MembersInfo['id'];




$sql="SELECT * FROM gq_answers where userid = '$id' ";
$result=mysql_query($sql);
$num_rows = mysql_num_rows($result);

if ($num_rows >= 1) { header("Location: ../dashboard.php?e=1"); }

else { 

foreach ($_POST as $key => $value) {

      $tables[] = trim($key);

      $values[] = mysql_real_escape_string($value);

}

mysql_query("INSERT INTO gq_answers (" . implode(", ", $tables) . ") VALUES ('" . implode("', '", $values) . "')");

}


   header("Location: ../dashboard.php");

}

?>

 

Just make sure all the input fields on your form are use the column name in the database as the field's name.  Make sure you shift or pop any array elements that don't have table columns too .... like the submit button ... etc.

I pulled the 'b' out of my ass since the key and value said 'a'

 

 

the first set of code works ... as you can see I added 'weight' to the db insert .. but not sure how to make it actually make it work ...

 

i prob am to confusing to understand .. lol ..

This should work:

 



<?php
session_start();
include("connect.php");

$MembersInfo = $_SESSION['membersInfo'];
$id = $MembersInfo['id'];




$sql="SELECT * FROM gq_answers where userid = '$id' ";
$result=mysql_query($sql);
$num_rows = mysql_num_rows($result);

if ($num_rows >= 1) { header("Location: ../dashboard.php?e=1"); }

else { 

foreach ($_POST as $key => $value) {

      $tables[] = trim($key);

      $values[] = mysql_real_escape_string($value);

}

mysql_query("INSERT INTO gq_answers (" . implode(", ", $tables) . ") VALUES ('" . implode("', '", $values) . "')");

}


   header("Location: ../dashboard.php");

}

?>

 

Just make sure all the input fields on your form are use the column name in the database as the field's name.  Make sure you shift or pop any array elements that don't have table columns too .... like the submit button ... etc.

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.