GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Jan-2005, 06:36
C05MIN C05MIN is offline
New Member
 
Join Date: Jan 2005
Posts: 1
C05MIN is on a distinguished road

I have to use an other method ?


Hi.
Why the code works only for the print instruction ?– I mean it shows on the screen the results—but in the data bases it only records, the first row of values.
What method do I have to use ?
The data for this script are sent to it by a post action
PLEASE HELP ME !! .

PHP Code:

<? 
include ("conectare.php"); 
$i=1;$z=5; 
while($i<=$z) 
{ 
$id_produs = "id_produs"."$i"; 
$id_unitate = "id_unitate"."$i"; 
$cantitatea = "cantitatea"."$i"; 
$pret_unitar = "pret_unitar"."$i"; 

$factura = 55;//$_POST['nr_generare']; 
$var = $_POST[$id_produs]; 
$var1 = $_POST[$id_unitate]; 
$var2 = $_POST[$cantitatea]; 
$var3 = $_POST[$pret_unitar]; 

print "$var"; 
print "$var1"; 
print "$var2"; 
print "$var3"."<br>"; 

$sql = "insert into produse values ($factura,$var,$var1,$var2,$var3)"; 
mysql_query($sql); 

$i++; 
}


Thanks.
  #2  
Old 26-Jan-2005, 10:16
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Hello C05MIN,

You should really add mysql_error() to the script, like in this example below:

PHP Code:

<?php
// orginal script here....
mysql_query( "insert into produse values ($factura, '$var', '$var1', '$var2', '$var3')" ) or die( mysql_error() );

// the rest...
?>


I am guessing, $factura a PRIMARY KEY value? If yes, then this is your problem isn't it? You can't have multiple rows with $factura being 55 in each. Also you need to add those single quotes in your values if they are not numeric...

Also if I were you, I would use the LOOP to compile the values and then do the INSERT just once outside of it.

Like this maybe...

PHP Code:

<?
include ("conectare.php");
$i=1; $z=5;
while($i<=$z)
{
  $id_produs = "id_produs"."$i";
  $id_unitate = "id_unitate"."$i";
  $cantitatea = "cantitatea"."$i";
  $pret_unitar = "pret_unitar"."$i";

  $factura = 55;//$_POST['nr_generare'];
  $var = $_POST[$id_produs];
  $var1 = $_POST[$id_unitate];
  $var2 = $_POST[$cantitatea];
  $var3 = $_POST[$pret_unitar];

  print "$var";
  print "$var1";
  print "$var2";
  print "$var3"."<br>";

  $sql .= "( $factura, '$var', '$var1', '$var2', '$var3' ), ";

  $i++;
}

// NOW, you do the database INSERT!
  // first, we remove the last comma inside the $sql string
  $sql = rtrim( $sql, ", " );
  // next we prepend the insert statement to the string...
  $sql = "INSERT INTO `produse` ( `col_name_factura`, `col_name_var`, `col_name_var1`, `col_name_var2`, `col_name_var3` )
  VALUES " . $sql;
  // do the insert now...
  mysql_query( $sql ) or die( mysql_error() );


Finally, don't forget to pass ANY value off the form through mysql_escape_string() if your magic_quotes_gpc setting is OFF.
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
calling abstract base class method calls draw instead achoo FLTK Forum 1 19-Dec-2004 09:38
The requested method POST is not allowed andret Apache Web Server Forum 1 14-Jun-2004 16:29
regarding main method jerry C++ Forum 18 09-Mar-2004 18:48
Method POST is not allowed on the URL jwalsh Computer Software Forum - Windows 2 27-Feb-2004 15:36
Best method to get listed on Google no1special_1999 Search Engine Optimization Forum 4 23-Sep-2003 23:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:35.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.