Current location: Hot Scripts Forums » Programming Languages » PHP » mysql generated variable insertion problem


mysql generated variable insertion problem

Reply
  #1 (permalink)  
Old 01-09-05, 06:53 PM
wwanthony wwanthony is offline
Newbie Coder
 
Join Date: Jan 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Question mysql generated variable insertion problem

Hi,
I have a script that works perfectly to generate the output then echo the result to a browser, but now I have adapted it to insert into a mysql database theres all kinds of problems.

Heres a snippet of the functioning code:

PHP Code:

    for ($i=1$i<8$i++) {

        
$codigo calcula_codigo($i$intdooptoCharArray($txtdoop), toCharArray($txtdoop1), toCharArray($txtdoop2) );

                 
$codes $codigo.$i."<br>";
    
    }

        
#Create the code in the database

$result = @mysql_query("INSERT INTO code SET ... etc 
As you can see the $codes are actually a list of 7 codes which are generated by the script. From the script above nothing is input into the database, but if I remove the ."<br>" then the only code that is insert into the database is the 7th code. If I put the $result = part above the } then it inputs 7 different rows of entries into the database when I want the 7 codes input into the same row.

I hope theres someone who will know what i've done wrong here.

Best Regards,
Anthony
Reply With Quote
  #2 (permalink)  
Old 01-09-05, 08:59 PM
the_mole001's Avatar
the_mole001 the_mole001 is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Australia
Posts: 96
Thanks: 0
Thanked 0 Times in 0 Posts
Hello there,

Perhaps if you tried:

$codes = null;
for ($i=1; $i<8; $i++) {
$codigo = calcula_codigo($i, $intdoop, toCharArray($txtdoop), toCharArray($txtdoop1), toCharArray($txtdoop2) );

$codes .= $codigo.$i."/n";

}

#Create the code in the database

$result = @mysql_query("INSERT INTO code SET ... etc

it would work?
__________________
Current Project: GGAC Website
Project Link: http://peter.5gigs.com/
Reply With Quote
  #3 (permalink)  
Old 01-10-05, 06:30 AM
wwanthony wwanthony is offline
Newbie Coder
 
Join Date: Jan 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,
Thanks for such a quick reply the_mole.

Unfortunately that method doesnt work, it doesnt input anything into the database. I am using a TEXT table in mysql, is this correct? I have also tried varchar but that doest work either.

I hope you may have something else I can give a go, your help is very much appreciated.

Best Regards,
Anthony
Reply With Quote
  #4 (permalink)  
Old 01-10-05, 09:12 AM
the_mole001's Avatar
the_mole001 the_mole001 is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Australia
Posts: 96
Thanks: 0
Thanked 0 Times in 0 Posts
Hello Again,

I went ahead and tested the code to some degree and i now believe the issue lies in how you are inserting it into the database. My test script looked like:

PHP Code:

<?php

$codes 
null;
for (
$i=1$i<8$i++) { 
    
$codigo rand(1,10000); 
    
$codes .= $codigo.$i.'/n'


$link mysql_connect("localhost""""");
mysql_select_db("peter_users"$link);

$query mysql_query("INSERT INTO test (test) VALUES ('".$codes."')");
?>
and it worked fine, putting all of the data into the database as it should (I was using a TEXT field so there were no worries there). Because of the "/"'s in the $codes part, it was causing errors later on. I hope you can apply my idea to ur code easily enough.
__________________
Current Project: GGAC Website
Project Link: http://peter.5gigs.com/
Reply With Quote
  #5 (permalink)  
Old 01-10-05, 12:08 PM
wwanthony wwanthony is offline
Newbie Coder
 
Join Date: Jan 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
I've Done It!! Yessss

Hi,
sorry for the over excitement

It was indeed an error with the way I was inputting the data into mysql.
My method was SET name=$name etc when I changed to INSERT INTO table (etc,etc) VALUES ($etc, $etc) It worked first time!

There is only one more problem I am having
I wanted a prefix to go onto each line of the generated code eg:

pref+423243+suf
[ $codes .= $codigo."+".$i."#<br>"; ]
[ $codes .= "pref".$codigo."+".$i."#<br>"; ] <---Doesnt work?

I have managed to get the suffix but when I use the same method for prefix it spits out the wrong code Any tips you got there the_mole?

p.s. THANKYOU ever so much for your help, you truely are a lifesaver
Reply With Quote
  #6 (permalink)  
Old 01-10-05, 06:32 PM
the_mole001's Avatar
the_mole001 the_mole001 is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Australia
Posts: 96
Thanks: 0
Thanked 0 Times in 0 Posts
Hello Again,

I can't see any flaws in your little snippet there, but because it isnt the full code it is alot harder to judge where the issue is. I adapted that to my testing script which looks like:

PHP Code:

<?php

$codes 
null;
for (
$i=1$i<8$i++) { 
    
$codigo rand(1,10000); 
    
$codes .= "pref".$codigo."+".$i.'suf<br>'


$link mysql_connect("localhost""""");
mysql_select_db("peter_users"$link);

$query mysql_query("INSERT INTO test (test) VALUES ('".$codes."')");
if(
$query){
    echo 
"DONE";
} else {
    echo 
"ERROR".mysql_error();
}
?>
And then when i looked at my db I had, "pref7987+1suf<br>pref504+2suf<br>pref5480+3suf<br >...", so unless there is still an error in the entry into the db, it should be fine.
__________________
Current Project: GGAC Website
Project Link: http://peter.5gigs.com/
Reply With Quote
  #7 (permalink)  
Old 01-10-05, 08:41 PM
wwanthony wwanthony is offline
Newbie Coder
 
Join Date: Jan 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up All Fixed

Hi the_mole,
It was simply because I was using VARCHAR and I hadnt set enough maximum values

It is now working perfectly, thanks to you.

I truely appreciate the help you have offered me.
If there is any way I can help you please let me know.

Thankyou once again,
Anthony
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MYSQL Distinct Query problem FunkyM PHP 20 02-28-07 08:33 AM
Problem with Insert in MySql bionicsamir PHP 2 01-02-05 08:08 PM
Problem with MySQL AND 1jetsam PHP 1 11-16-04 09:50 PM
problem printing mysql data by "x rows/page" abtimoteo PHP 1 07-30-04 07:55 PM
Load Data Infile Problem (MySQL) Shaky2 PHP 1 03-26-04 04:48 PM


All times are GMT -5. The time now is 05:25 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.