Jump to content

[SOLVED] Help with inserting SQL back into DB with PHP


ttocs

Recommended Posts

Hi guys,

 

I have a question about using PHP with SQL.  Let me first explain that I'm new to PHP.  We took a 3 day class at my job and are just starting to get into the benefits PHP can perform.  So, don't assume I know much when replying  ;)

 

Ok, so I'm making a tool that allows users to select from a drop down.  When they select their product, a query is run in the background that pulls in information based off of the product they selected.  I have this part down.  It is shown on the next page and allows them to make changes to it. 

 

What I'm trying to figure out is how to get this back into the database.  Multiple rows are being shown (one row for each month for 2 years, so 24 rows) and they are able to update as many as they like.  If this were one row I would just use a variable in a SQL insert statement, but for some reason, all of these rows are just throwing me through a loop.  I thought of an array, but we can't figure out the syntax if it would even work.  Does anyone have any ideas or maybe a snippet of code of this being done in the past.  Not sure if it matters, but we are using Oracle.

 

Thanks in advance for any help.

Here is the code that I have so far.  This is the php script that runs when you hit the button on the main page (after you pick the product).

 

for($i=0;$i<12;$i++){
$name = "Year_Mon$i";
$name2 = "Product$i";

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <title>Goal Tool</title>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <link type="text/css" rel="stylesheet" href="/css/search_redirect.css" />
  </head>
  
  <body>
    <div class="intro">	
    <p class="bi_motto"><br/>"Header"</p>
      <a href="/html/index.html">
        <img src="Image.jpg"/> 
      </a>
  </div>
    <p><br/></p>
    
<?php
$product = $_GET['product']; 

if (!$db = @ocinlogon("DB","user", "pw")) {
    $error = ocierror();
    printf("CONNECT error: %s", $error["message"]);
    die();
}

$query = "select * from db.table
  where product in ('$product')";

$stmt = ociparse($db,$query);

if (!@ociexecute($stmt)) {
    $error = ocierror($stmt);
  printf("OCIEXECUTE Failure. Error was: %s\n", $error["message"]);
   }

echo "<form action=\"goal_tool.php\" method=\"get\">";
  
  echo "<table>";
echo "<tr>";
echo "<td><b>Year_Month</b></td>";
echo "<td><b>Product</b></td>";
echo "<td><b>Revenue</b></td>";
echo "<td><b>IC</b></td>";
echo "<td><b>Impressions</b></td>";
echo "</tr>";

$i = 0;	
while($nrows = ocifetchinto($stmt, &$results, OCI_ASSOC)) {
  echo "<tr>";
  echo "<td><input type=text name=\"Year_Mon$i\" value=".$results['YEAR_MON']."></td>";
  echo "<td><input type=text name=\"Product$i\" value=".$results['PRODUCT']."></td>";
  echo "<td><input type=text name=\"Revenue$i\" value=".$results['REVENUE']."></td>";
  echo "<td><input type=text name=\"IC$i\" value=".$results['IC']."></td>";
  echo "<td><input type=text name=\"Imps$i\" value=".$results['IMPS']."></td>";
  echo "</tr>";
  $i++;
  	 }

echo "</table>";

echo "<input type=\"submit\" value=\"Submit\"/>";

 

This just returns the 24 rows in editable boxes.  Now I have to get this back in the database.

Instead of numbering every input field, you can use an array;

 

your code;

echo "<td><input type=text name=\"Year_Mon$i\" value=".$results['YEAR_MON']."></td>";

 

changed code;

echo '<td><input type=text name="Year_Mon[]" value='.$results['YEAR_MON'].'></td>';

 

If you do that for each field you'll have an array on the next html page ready made for you.

 

Without and security or fancy stuff, this would print an array of 20 fields;

 

foreach($_GET['Year_Mon'] as $key => $value) {
echo "$key: $value<br />";
}

 

That should get you in the right direction

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.