will this work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gotornot
Forum Commoner
Posts: 54
Joined: Fri Jul 31, 2009 2:30 am

will this work

Post by gotornot »

I am trying to insert all extra questions asked on data cpture into a db will this work

if (isset($_REQUEST))
{
foreach($_REQUEST as $key=>$value)
{
// only get the data for extra questions
$schq = "SELECT * FROM extraq WHERE programid='$programid'";
$schr = mysql_query($schq);
while($schrow=mysql_fetch_array($schr))
{
$thisqid = $schrow["id"];
if ($thisqid=='$key')
{
$addq3 = "INSERT INTO $extraqopt (extraqid, value, subid) VALUES ('$thisqid', '$value', '$subid')";
$addr3 = mysql_query($addq3);
}
}
}
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: will this work

Post by AbraCadaver »

Yes and no. Your single quotes around $key will keep it from ever matching, however if you change it it should work. Plus I don't see where $programid and $subid are being defined. That being said, it is not optimal. Why not just send the field names as keys like this (example only):

Code: Select all

<input type="hidden" name="data[extraqid]" value="<?php echo $extraqid; ?>">
<input type="hidden" name="data[subid]" value="<?php echo $subid; ?>">
<input type="text" name="data[value]">
To process:

Code: Select all

$fields = implode(',', array_keys($_POST['data']));
$values = "'" . implode("','", array_map('mysql_real_escape_string', $_POST['data'])) . "'";
 
mysql_query("INSERT INTO table_name ($fields) VALUES ($values)";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
gotornot
Forum Commoner
Posts: 54
Joined: Fri Jul 31, 2009 2:30 am

Re: will this work

Post by gotornot »

this does not seem to put the data in the table and is driving me up the wall

//grab the sub id for extra q submission
$chkq4 = "SELECT * FROM submission WHERE email='$email' AND programid='$programid'";
$chkr4 = mysql_query($chkq4);
$chkrow4 = mysql_fetch_array($chkr4);
$subid = $chkrow4["id"];
// insert extraq data
// grab the extra data in from the string i may have to blow it appart and check it has a value
if (isset($_REQUEST))
{
foreach($_REQUEST as $key=>$value)
{
// only get the data for extra questions
$schq = "SELECT * FROM extraq WHERE programid='$programid'";
$schr = mysql_query($schq);
while($schrow=mysql_fetch_array($schr))
{
$thisqid = $schrow["id"];
if ($thisqid==$key)
{
$addq3 = "INSERT INTO $extraqopt (extraqid, value, subid) VALUES ('$thisqid', '$value', '$subid')";
$addr3 = mysql_query($addq3);
}
}
}
}

if basically their is an extra q why can it not bung it in.
Post Reply