Go Back   PHP-Editors > Programming Help > PHP Programming Help

PHP Programming Help Post any question relating to PHP Programming here and hopefully someone can help.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 2008-12-03, 02:35 AM
Junior Member
 
Join Date: Dec 2008
Posts: 1
marius47 is on a distinguished road
Default Insert a custom number of rows at once

Hi.
I have a big problem that I do not know how to solve. I have a form used to insert some data into MYSQL .The database contains some products and the price for every product, a field for products status, another field where is inserted the date when the product is inserted into database, and a field that use auto_increment and will generate a unique value for each product.I want to attach to my form a text field where is inserted the number of how many rows to create in the database with the same product and price. My php code is :

Code:
 
<?php
error_reporting(E_ALL ^ E_NOTICE); 
function Basic()
{
require("database.php");
require("configa.php");
require_once("validator.php");
$TableID = $_GET['table_id'];
//print("<h3 align=center>Adding Records to $TableNames[$TableID]</h3>");
print("<table align=center class=Table>");
$Fields = explode("@", $Tables[$TableID]);
echo '<form action="add.php" method="POST">';
$date = date("Y-m-d");
$MappedTable = GetMappedTables($TableID);
 
print("<tr class=TableAltRow>");
print("<td>product</td><td>");echo '<input type="text" name="';print(product);echo '"></td>';
print("<tr class=TableAltRow>");
print("<td>price</td><td>");echo '<input type="text" name="';print(price);echo '"></td>';
print("");echo '<input type="hidden" name="';print(date);echo '" value="';print($adaugat);echo '"></td>';
print("<tr class=TableAltRow><td><input type=submit name=action value=Add></td><td>&nbsp;</td></tr>");
print("<input type=hidden name=table_id value=$TableID>");
print("</form>");
};
function Add()
{
require("database.php");
require("configa.php");
require("validator.php");
?>
<link rel="stylesheet" type="text/css" href="Colorful.css" />
<?php
$TableID = $_POST['table_id'];
$Fields = explode("@", $Tables[$TableID]);
$QUERY = "insert into `$TableNames[$TableID]`(";
$FieldCounter = 0;
foreach($Fields as $Field)
{
$FieldCounter++;
$QUERY .= "`".$Field."`";
if($FieldCounter < sizeof($Fields))
{
$QUERY .= ", ";
}
else
{
$QUERY .= ") values(";
};
};
$FieldCounter = 0;
foreach($Fields as $Field)
{
$FieldCounter++;
if(Validate($TableID, $Field, $_POST[$Field]) == "false")
{
if($Field != GetMapping($TableID,$Field))
{
$Temp = GetMapping($TableID, $Field);
$Mapped = explode(".", $Temp);
$Key = $Field;
if(!FieldExists("`$Mapped[0]`.`$Field`"))
{
$Key = GetCorrectKey($Mapped[0],$Field);
$qry = "select * from `$Mapped[0]` where `$Mapped[1]`='$_POST[$Field]'";
}
else
{
$qry = "select * from `$Mapped[0]` where `$Mapped[1]`='$_POST[$Field]'";
};
$Result = mysql_query($qry);
$Raw = mysql_fetch_array($Result);
$Key = trim($Key);
$QUERY .= "'".$Raw[$Key]."'";
}
else
{
$TMP = explode(" ", $Field);
if($TMP[1] != "")
{
$Field = str_replace(" ", "_", $Field);
};
$QUERY .= "'".$_POST[$Field]."'";
};
}
else
{
die("The value $_POST[$Field] at $Field in table $TableName[$TableID] is not accepted");
};
if($FieldCounter < sizeof($Fields))
{
$QUERY .= ", ";
}
else
{
$QUERY .= ")";
};
};
// print($QUERY);
mysql_unbuffered_query($QUERY);
if(!mysql_error())
{
print("<H3 align=center>Added Successfully</H3>");
}
else
{
print("<H3 aclign=center>Addition failed : ".mysql_error()."</H3>");
};
echo '<p align=center><a href="main.php?table_id=';print($TableID);echo'">Home</a></p>';
};
if($_POST['table_id'] == "" && $_GET['table_id'] != "")
{
Basic();
}
elseif($_POST['table_id'] != "")
{
Add();
};
?>
I really appreciate any help.
Reply With Quote
Must read Review for Serious PHP Developers


NuSphere PhpED 5.5 : The Staff of php-editors.com recently spent a few days working with NuSphere PhpED 5.5 (a popular PHP IDE) and NuCoder 2.0 (a PHP Encoding Utility), read up on all the details.

Sponsored Links
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:27 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.