Jump to content

Automating form-to-database (and back) data handling.


Recommended Posts

Hello Everyone,

 

I'm new to these forums, but am new to php as well, so I may be sticking around for a while. Hope my newbness doesn't cause too many problems.

 

On to the point.  I'm building a site with several lengthy forms, and while I know how to get the submitted data into their corresponding variables individually, I was looking for a way to automate the entire process and then automate the insertion of each piece of data into the database.  The tool I deferred to was an array and a foreach loop, but for some reason it's not working; I think it might be a problem in the MySQL query statement, but I'll let you be the judge of that.

 

foreach ($_POST ['listdock'] as $ld) {
if (trim ($ld == "")) {
$ld = null ;
}
$query = "INSERT INTO docks (1,2,3,4,5, etc) VALUES (\"$ld\")" ;
$result = mysql_query($query) ; 
echo "$ld";  

if ($result) { ?>

<h3>Your dock has been added to our database!</h3>

<p>Check the listings page to find your dock.</p> <?php ; 
} else { ?> 

<h3>We could not add your dock to our database at the moment.</h3> <?php ;
}
}

 

I see the problem...or at least one of them.  It's where the table names goes "(1,2,3,4,5,etc);" Any help appropriating this to work would be greatly appreciated.

The problem is $ld is a loop variable, and get overwritten everytime the foreach happens. What you probably want to do (assuming that the order of the array elements corresponds to the order of the columns in your insert statement) is to make a string

$insert = ""
foreach ($_POST ['listdock'] as $ld) {
$insert .= "'"$ld."',";
}
$insert = rtrim($insert, ",");//trim the last comma off the string

 

and then you could use the insert string like so

$query = "INSERT into blah blah blah VALUES($insert)";

I think what you need to do is this:

 

 

$num    = 1;
$tables = array();
$values = array();
foreach( $_POST['listdock'] as $ld )
{
   if ( !trim($ld) )
      $ld = null;


   $tables[] = $num++;
   $values[] = $ld;
}
$query  = "INSERT INTO docks ( " . implode(", ", $tables) . " ) VALUES ( '" . implode("', '", $values) . "' )"; // edit oops lol
$result = mysql_query($query);


   if ($result)

   {
      echo '<h3>Your dock has successfully been added to our database!</h3>' . "\n\n";
      echo '<p>Check the listings page to find your dock.</p>';
   }
   else
   {
      trigger_error("Error: " . mysql_error(), E_USER_ERROR);
   }

 

 

???

 

I think what you need to do is this:

 

 

$num    = 1;
$tables = array();
$values = array();
foreach( $_POST['listdock'] as $ld )
{
   if ( !trim($ld) )
      $ld = null;


   $tables[] = $num++;
   $values[] = $ld;
}
$query  = "INSERT INTO docks ( " . implode(", ", $tables) . " ) VALUES ( '" . implode("', '", $values) . "' )"; // edit oops lol
$result = mysql_query($query);


   if ($result)

   {
      echo '<h3>Your dock has successfully been added to our database!</h3>' . "\n\n";
      echo '<p>Check the listings page to find your dock.</p>';
   }
   else
   {
      trigger_error("Error: " . mysql_error(), E_USER_ERROR);
   }

 

 

???

 

I'm not sure what

   if ( !trim($ld) )
      $ld = null;

does and I 'm a little perplexed about what to do if I actually gave names to the tables in the database.  The number vals works for the $tables var so long as it's actually a number that I assign to the name of the field, but what if i gave them names?

The problem is $ld is a loop variable, and get overwritten everytime the foreach happens. What you probably want to do (assuming that the order of the array elements corresponds to the order of the columns in your insert statement) is to make a string

$insert = ""
foreach ($_POST ['listdock'] as $ld) {
$insert .= "'"$ld."',";
}
$insert = rtrim($insert, ",");//trim the last comma off the string

 

and then you could use the insert string like so

$query = "INSERT into blah blah blah VALUES($insert)";

 

My main problem wasn't in figuring out how to get the correct values inserted, so much as it was getting a number of values inserted to the corresponding table fields; so when you say "blah blah blah" i think that's skipping one of the more crucial parts of my dilemma.  do you know what I mean?

The only other way I can think of is if you add your sql field names to the corresponding array key or the list dock form input.

That way you can use:

 

 

<input type="text" name="listdock[sqlfieldName1]" value="" >

$tables = array();
$values = array();
foreach( $_POST['listdock'] as $field => $ld )
{
   if ( !trim($ld) )
      $ld = null; // this just sets $ld var to null if trim($ld) evaluates to false, I.e. Empty / 0


   $tables[] = $field;
   $values[] = $ld;
}
$query  = "INSERT INTO docks ( " . implode(", ", $tables) . " ) VALUES ( '" . implode("', '", $values) . "' )";
$result = mysql_query($query);


   if ($result)

   {
      echo '<h3>Your dock has successfully been added to our database!</h3>' . "\n\n";
      echo '<p>Check the listings page to find your dock.</p>';
   }
   else
   {
      trigger_error("Error: " . mysql_error(), E_USER_ERROR);
   }

The only other way I can think of is if you add your sql field names to the corresponding array key or the list dock form input.

That way you can use:

 

 

<input type="text" name="listdock[sqlfieldName1]" value="" >

$tables = array();
$values = array();
foreach( $_POST['listdock'] as $field => $ld )
{
   if ( !trim($ld) )
      $ld = null; // this just sets $ld var to null if trim($ld) evaluates to false, I.e. Empty / 0


   $tables[] = $field;
   $values[] = $ld;
}
$query  = "INSERT INTO docks ( " . implode(", ", $tables) . " ) VALUES ( '" . implode("', '", $values) . "' )";
$result = mysql_query($query);


   if ($result)

   {
      echo '<h3>Your dock has successfully been added to our database!</h3>' . "\n\n";
      echo '<p>Check the listings page to find your dock.</p>';
   }
   else
   {
      trigger_error("Error: " . mysql_error(), E_USER_ERROR);
   }

 

I believe this is what i needed.  Thank you!

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.