PHP Applications
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Applications

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
  #1  
Old May 31st, 2007, 11:41 PM
lordchriz lordchriz is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Dumaguete City, Oriental Negros,Philippines
Posts: 26 lordchriz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 55 m 55 sec
Reputation Power: 0
Send a message via Yahoo to lordchriz
Insert data to myql problem

helo anybody help me solve this code below: hope u can help me plss

Code:
<?php
  include("conn.php");
  
		  $mode=$_GET["mode"];
		  if($mode=="add") {
		    $coupon_url=$_POST["coupon_url"];
			$store_name=$_POST["store_name"];
			$coupon_title=$_POST["coupon_title"];
			$description=$_POST["description"];
		  	$category=$_POST["category"];
			$valid_from=$_POST["valid_from"];
			$valid_thru=$_POST["valid_thru"];
		  	}
$sql="insert into sub_coupon(coupon_url,store_name,coupon_title,desc  ription,category,valid_from,valid_thru) values('$coupon_url','$store_name','$coupon_title'  ,'$description','$category','$valid_from','$valid_  thru')";
$result=mysql_query($sql,$connection) or die(mysql_error());
?>


error:

Notice: Undefined index: mode in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 462

Notice: Undefined variable: coupon_url in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 472

Notice: Undefined variable: store_name in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 472

Notice: Undefined variable: coupon_title in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 472

Notice: Undefined variable: description in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 472

Notice: Undefined variable: category in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 472

Notice: Undefined variable: valid_from in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 472

Notice: Undefined variable: valid_thru in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 472
__________________
im lve web designing

Reply With Quote
  #2  
Old June 13th, 2007, 12:21 PM
icandothat's Avatar
icandothat icandothat is offline
Moderator
Click here for more information.
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 1,526 icandothat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 23 h 14 m 36 sec
Reputation Power: 3
looks like these variables are not in the post. do a little loop check. also, these are only notices, not failures, in the script. You can hide these by setting

error_reporting(E_ERROR);

at the top of the page or better yet in a prepend file. This will simply hide any warnings or notices but display errors. Hiding these types of warnings or for that matter all errors, on a production server is a good idea. When errant warnings go off it's not only ugly but it gives away information about your code.
__________________
There is no spoon.

Reply With Quote
  #3  
Old June 14th, 2007, 11:13 PM
akong_mata akong_mata is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Location: makati city
Posts: 15 akong_mata User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 21 m 57 sec
Reputation Power: 0
Smile kikid...

icandothat is right.. you're using a GET method which is definitely does not coincide with your variables declaration that which POST is being used.. and read some basics for ISSET..

===============================================
Quote:
Originally Posted by lordchriz
helo anybody help me solve this code below: hope u can help me plss

Code:
<?php
  include("conn.php");
  
		  $mode=$_GET["mode"];
		  if($mode=="add") {
		    $coupon_url=$_POST["coupon_url"];
			$store_name=$_POST["store_name"];
			$coupon_title=$_POST["coupon_title"];
			$description=$_POST["description"];
		  	$category=$_POST["category"];
			$valid_from=$_POST["valid_from"];
			$valid_thru=$_POST["valid_thru"];
		  	}
$sql="insert into sub_coupon(coupon_url,store_name,coupon_title,desc  ription,category,valid_from,valid_thru) values('$coupon_url','$store_name','$coupon_title'  ,'$description','$category','$valid_from','$valid_  thru')";
$result=mysql_query($sql,$connection) or die(mysql_error());
?>


error:

Notice: Undefined index: mode in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 462

Notice: Undefined variable: coupon_url in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 472

Notice: Undefined variable: store_name in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 472

Notice: Undefined variable: coupon_title in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 472

Notice: Undefined variable: description in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 472

Notice: Undefined variable: category in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 472

Notice: Undefined variable: valid_from in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 472

Notice: Undefined variable: valid_thru in F:\server\htdocs\coupon\admin\mnagelist\listing.ph p on line 472

Reply With Quote
  #4  
Old June 15th, 2007, 09:31 AM
cwf's Avatar
cwf cwf is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 343 cwf User rank is Private First Class (20 - 50 Reputation Level)cwf User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 8 m 34 sec
Reputation Power: 2
I'll take a guess. Since all of the get/post data are undefined (mode could be get and the rest could be post), it is likely that the form and form processing code is in the same file and the form processing code is being unconditionally executed or that the "form" is improperly coded.

You would need to post at least your form code and/or all of your code to get more specific help with it.

Warning and notice messages indicate either improperly written code or data (missing or unexpected content) that the code was not written to handle properly. Turning off warning/notice messages (they are still written to the log file, slowing everything down and filling up the log) just hides what is really wrong with the code and in this case would have gotten a "why is my query not inserting into my database" or if only the mode variable worked "why are the values in the record null" questions.

Code can and should always detect if a variable is set (and for variables from outside your code, validate them to insure they don't contain spam/code injection) before blindly using that variable and code can and should always test if a function call returned a false/failure value before blindly proceeding with using the results from that function call. Easily, 85% of the msyql problems in programming forums are found by adding error checking and error reporting to the msyql_query(...) function call.

There are only a few cases where code cannot detect/validate for a condition that would generate a warning/notice, such as when accessing a remote file and the remote server might be down... In the few cases where properly written code can generate a warning/notice, use the @ to suppress the possible error, but then test for the false/failure value that would be returned by the function call and output a meaningful user message - "could not connect..."

Sorry to turn this into a lecture on error checking and error reporting in properly written code, but in development and learning situations, until code has been throughly tested and is ready for release, everyone should be getting PHP to help find parse and runtime errors by turning on full error reporting. Once code has been tested with every possible what-if data input and is bullet-proof (tests data and function calls and outputs meaningful error messages) you won't care what the error reporting settings are on a production server, because your code won't be triggering any PHP warnings/notices...

Last edited by cwf : June 15th, 2007 at 09:37 AM. Reason: add little

Reply With Quote
  #5  
Old June 15th, 2007, 12:42 PM
icandothat's Avatar
icandothat icandothat is offline
Moderator
Click here for more information.
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 1,526 icandothat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 23 h 14 m 36 sec
Reputation Power: 3
I agree with the spirit of your post but in the end I want my production server to hide errors. If one slips past me I don't want the world seeing it. However, on my dev server I have error_all set. PHP should really be written like C where variables are declared before use.

Reply With Quote
  #6  
Old June 15th, 2007, 09:06 PM
akong_mata akong_mata is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Location: makati city
Posts: 15 akong_mata User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 21 m 57 sec
Reputation Power: 0
exactly!

exactly!

Reply With Quote
  #7  
Old August 13th, 2007, 02:31 PM
hammer65 hammer65 is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Location: Nebraska
Posts: 55 hammer65 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 16 m 53 sec
Reputation Power: 1
There's way more trouble than that here. What good is eliminating notice errors if the script is wide open to SQL Injection. Never put values directly from input $_GET, $_POST or $_COOKIE into an SQL query. They must be escaped with an escape_string function appropriate with the database system you are using (not addslashes, magic_quotes or htmlspecialchars or any other non database encoding). Your server will get owned in short order, if you don't.

Reply With Quote
  #8  
Old August 13th, 2007, 03:08 PM
icandothat's Avatar
icandothat icandothat is offline
Moderator
Click here for more information.
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 1,526 icandothat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 23 h 14 m 36 sec
Reputation Power: 3
escaping does very little to protect you from sql injection. Well constructed queries and logic are your best protection.
Comments on this post
hammer65 disagrees: Seriously. Thst statement is incorrect in the extreme. Please at least do some research on the
subject before misguiding new coders.

Last edited by icandothat : August 13th, 2007 at 03:10 PM. Reason: elaboration

Reply With Quote
  #9  
Old August 13th, 2007, 03:26 PM
hammer65 hammer65 is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Location: Nebraska
Posts: 55 hammer65 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 16 m 53 sec
Reputation Power: 1
I can scarcely believe you said that. Have you even researched the subject?

It doesn't matter how you "construct" a query. If someone wants to insert syntactically correct SQL into an input control on a form, and you don't escape certain characters that are part of the SQL language to guard against it, your server is wide open to attack. Using mysql_real_escape_string or prepared statements with MySQL is the only way to avoid such attacks.

Addslashes and magic_quotes_gpc are vulnerable because they do not take character sets into account. This link explains the vulnerability.

I am shocked that as a mod on this forum, you aren't aware of this.
Comments on this post
icandothat agrees: Hammer's right. I think I was drunk when I said this. I'm getting help.

Reply With Quote
  #10  
Old August 14th, 2007, 02:31 PM
icandothat's Avatar
icandothat icandothat is offline
Moderator
Click here for more information.
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 1,526 icandothat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 23 h 14 m 36 sec
Reputation Power: 3
I am perfectly aware of the dangers of sql injection. To that end, one is better off avoiding the problem rather than just fighting it head on. Case in point. In a login script one might do something like this.
Code:
$sql = "select count(*) from users where username = $username and pass = $pass";
//pseudo code//
//do query
//get count
if($count >0){
// let the person log in.
}


now the code above has a few issues but the one I want to illustrate is the potential for SQL injection. someone could make their password something like
"bob or password is not null"
(This is off the top of my head so you'll forgive me the small leap of logical faith)
You could try to escape the $_POST values and be very clever but there is generally someone out there more clever than you, well at least than me.
However you could appraoch it thusly

Code:
$sql = "select password from users where username  = $username and rownum = 1";
//pseudo code//
//run query
//get result
if($password == $result['password']){
  //let the person login . 
}



in this way you avoid the problem rather than taking it on head on.

Of course I would never recommend inserting values directly from the post without escaping them but that was not the original posters question so I didn't address it. I was not trying to give the impression that cleaning your data is not important.

Finally, Many times people do not post their actual code here but instead a version of it that gives the general idea what what they are trying to do. It's pointless to pick apart every little detail of a post.

Last edited by icandothat : August 14th, 2007 at 02:34 PM.

Reply With Quote
  #11  
Old August 14th, 2007, 03:20 PM
hammer65 hammer65 is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Location: Nebraska
Posts: 55 hammer65 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 16 m 53 sec
Reputation Power: 1
I'm aware that sometimes people post "test" scripts to a forum as are you, but many new coders are not. There are far too many tutorials and books out there that still teach the same insecure practices over and over. it gives the language a bad rep which effects client's decisions to use PHP and makes 3rd party code a dangerous mess. We are also all consumers on the web, we even give personal infomation to this site, so we do have a stake in how other coders program web apps.

One misinformed coder could wipe out everything a person owns, if they have our personal information in their hands. Thus it is incumbent on the developer community to try to head these things off. Even if we occasionally offend in the process.

If a new coder doesn't have a grasp of PHP notice errors, they certainly won't understand automatically, any SQL "tricks" to somehow avoid escaping values. The simplest is with numerical data and using intval or force casting. We know this stuff they don't. Not saying anything at all is a little risky.

When someone says "Yeah, i know, i figured i would worry about security later.". Okay, maybe they will, maybe they won't, do they even know what "security" in PHP means.

Saying that escaping does "little" to combat SQL injection is simply false. The SQL parser will not execute any escaped SQL any more than the PHP parser would execute escaped PHP code. Until I hear otherwise from the likes of Mr. Shiflet or other established PHP security experts, I would be foolish to think otherwise.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Applications > Insert data to myql problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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 On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |