Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-09-2007, 09:06 AM   #1 (permalink)
pungi
Recruit
 
Join Date: Feb 2006
Posts: 17
pungi is on a distinguished road
Php/query will not work

Hi,
I am trying to insert a value into an exisitng table but it does not insert the value, just a blank nothing. Apart from that it is working. The main part of the script is:

$query = "INSERT INTO $sched (task) VALUES ('$task')";

$i=0;
for($i=0;$i<12; $i++){
if ($_POST["task$i"]<>''){
$task=addslashes($_POST["task$i"]);
mysql_query($query) or die(mysql_error());
}
}

Thanks in advance,
Pungi
pungi is offline   Reply With Quote
Old 04-09-2007, 11:14 AM   #2 (permalink)
pungi
Recruit
 
Join Date: Feb 2006
Posts: 17
pungi is on a distinguished road
Hi,
I was convinced that the query was ok as im sure i tested it but i was wrong. I still havent found the problem but it definately is concerned with
"VALUES ('$task')" part of the query as the following query inserted 'jack' into the table:

$array= array(jack);
$query = "INSERT INTO $sched (task) VALUES ('$array[0]')";
Thanks, and sorry for posting this into the php forum when it was an sql error,
pungi
pungi is offline   Reply With Quote
Old 04-09-2007, 12:20 PM   #3 (permalink)
pungi
Recruit
 
Join Date: Feb 2006
Posts: 17
pungi is on a distinguished road
Hi,
Ignore the last post for I dont have a clue with whats wrong with it, it just inserts null values into the table.
Thanks,
pungi
pungi is offline   Reply With Quote
Old 04-09-2007, 02:36 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
try this:
PHP Code:
<?
$i
=0;
for(
$i=0;$i<12$i++){

  
$task $_POST["task".$i]
  if (
$task) {

    
$query "INSERT INTO ".$sched." (task) VALUES ('".mysql_real_escape_string($task)."')";
    
mysql_query($query) or die(mysql_error());
  }
}
?>
__________________
Mike
sde is offline   Reply With Quote
Old 04-09-2007, 02:48 PM   #5 (permalink)
pungi
Recruit
 
Join Date: Feb 2006
Posts: 17
pungi is on a distinguished road
Hi,
Thanks a bunch. It works but for now, i will call it magic and try to understand it at a later date.
Thanks again,
Pungi
pungi is offline   Reply With Quote
Old 04-09-2007, 03:04 PM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
i insist u look at the code

you were setting your $query variable once, .. before the loop. you needed to set it in each iteration of the loop.

you were also setting $task in your query string. that contained nothing.

in the loop, you then modified $task as it should have been, .. but the variable $task does not maintain its reference inside the $query string.
__________________
Mike
sde is offline   Reply With Quote
Old 04-09-2007, 04:16 PM   #7 (permalink)
pungi
Recruit
 
Join Date: Feb 2006
Posts: 17
pungi is on a distinguished road
Thank you also for the explanation. I understand it now; probably would not have without it.
pungi is offline   Reply With Quote
Reply

Bookmarks

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't get loop to work rockybalboa Java 5 03-20-2005 03:54 PM
IsNumeric: Will this work? keeling Standard C, C++ 6 03-19-2005 10:13 AM
San Diego Tech. work. Admin Lounge 10 02-03-2003 06:19 PM
Getting X to work in Debian w00t Linux / BSD / OS X 7 08-25-2002 01:44 PM


All times are GMT -8. The time now is 04:01 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting