PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

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 Dev Shed Forums Sponsor:
  #1  
Old February 6th, 2013, 07:40 PM
fruanthony fruanthony is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 fruanthony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 30 m 42 sec
Reputation Power: 0
Problem with PDO insert script

When i Run the script it displays this erro i di not know what to do
ERROR: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'chapter=sdasd' at line 1

this is the script
if (isset($_POST['chapter'])AND isset($_POST['verse']))
{
try {
$pdo_options[PDO::ATTR_ERRMODE]=PDO::ERRMODE_EXCEPTION ;
$Dbc= new PDO('mysql:host=localhost;dbname=gths','fruanthony','admin',$pdo_options);
$message=$Dbc->query("SELECT info FROM bible where verse=".$_POST['verse']."AND chapter=".$_POST['chapter'] );
while($results = $message->fetch()){
$results['info'];
}
}
catch(Exception $e)
{die ('ERROR: '.$e->getMessage());
exit;
}
}

Reply With Quote
  #2  
Old February 6th, 2013, 08:27 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,872 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 1 h 24 m 30 sec
Reputation Power: 813
Hi,

Do not insert raw values into query strings.

You should be glad that you stumbled upon this error and didn't have to learn it "the hard way" with some script kiddie f*cking up your database.

PDO has a great feature called "prepared statement". That's what you should use (see the link above). Don't just inject some POST parameters into the query string, because this tears a big security hole in your application -- the quotes you accidentally inserted into the query might as well have been malicious SQL purposely injected by an attacker.

And I hope "admin" isn't your actual database password?

Reply With Quote
  #3  
Old February 6th, 2013, 09:47 PM
BarryG BarryG is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Location: Sydney Australia
Posts: 131 BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 7 h 51 m 28 sec
Reputation Power: 83
Quote:
Originally Posted by fruanthony
"SELECT info FROM bible where verse=".$_POST['verse']."AND chapter=".$_POST['chapter'] );


And this will expand to something like

Code:
SELECT info FROM bible where verse=3AND chapter=16 );


because of the missing space before AND, and the parser doesn't know what to make of the term after 3AND so you get the error pointing to the code right after the error.

But like Jacques said, don't do this . It's a BIG SECURITY HOLE. Use the prepared statements available in PDO and stay safe.

Reply With Quote
  #4  
Old February 7th, 2013, 12:49 AM
simplypixie simplypixie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 104 simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 18 h 33 m 57 sec
Reputation Power: 11
You are also missing your closing quote mark at the end of your query
PHP Code:
"SELECT info FROM bible where verse=".$_POST['verse']." AND chapter=".$_POST['chapter']."); 

Reply With Quote
  #5  
Old February 7th, 2013, 03:45 AM
fruanthony fruanthony is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 fruanthony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 30 m 42 sec
Reputation Power: 0
i have updated the query but it is does not display anything
if (isset($_POST['chapter'])AND isset($_POST['verse']))
{
try {
$pdo_options[PDO::ATTR_ERRMODE]=PDO::ERRMODE_EXCEPTION ;
$Dbc= new PDO('mysql:host=localhost;dbname=gths','fruanthony','admin',$pdo_options);
$message=$Dbc->prepare("SELECT info FROM bible where verse= ? " );
$message->execute(array(
$_POST['verse']

));
while($results = $message->fetch()){
$results['info'];
//var_dump($results);
echo "<br>";
}
//var_dump($results);
}
catch(Exception $e)
{die ('ERROR: '.$e->getMessage());
}
}

Reply With Quote
  #6  
Old February 7th, 2013, 04:45 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,872 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 1 h 24 m 30 sec
Reputation Power: 813
What is this supposed to display when you don't echo anything except line breaks?

By the way, you should remove this "try-catch" stuff. It's completely useless, because what you're doing there is what the exception would do, anyway. And it's generally a bad idea to display internal error messages.

Reply With Quote
  #7  
Old February 7th, 2013, 05:40 AM
fruanthony fruanthony is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 fruanthony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 30 m 42 sec
Reputation Power: 0
Quote:
Originally Posted by Jacques1
What is this supposed to display when you don't echo anything except line breaks?

By the way, you should remove this "try-catch" stuff. It's completely useless, because what you're doing there is what the exception would do, anyway. And it's generally a bad idea to display internal error messages.

i thought
this was what was going to echo the resulut
<echo $a['info'];>

Reply With Quote
  #8  
Old February 7th, 2013, 05:55 AM
Aurum84 Aurum84 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 74 Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 23 h 5 m 49 sec
Reputation Power: 17
Quote:
Originally Posted by fruanthony
i thought
this was what was going to echo the resulut
<echo $a['info'];>


True, but you are not echoing anything in the loop.

Instead of keep calling fetch() you can also use fetchAll(), and iterate over that resulset

Reply With Quote
  #9  
Old February 7th, 2013, 06:05 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,872 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 1 h 24 m 30 sec
Reputation Power: 813
Quote:
Originally Posted by Aurum84
Instead of keep calling fetch() you can also use fetchAll(), and iterate over that resulset


You shouldn't call any "fetch" method at all unless you actually physically need the rows in an array.

Simply loop over the statement object, it's iterable:
PHP Code:
 $message->execute();
foreach (
$message as $row) {
    ...





@ fruanthony:

We need your full code. In the snippet above, there is no "$a".

Last edited by Jacques1 : February 7th, 2013 at 06:16 AM.

Reply With Quote
  #10  
Old February 7th, 2013, 06:49 AM
fruanthony fruanthony is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 fruanthony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 30 m 42 sec
Reputation Power: 0
Quote:
Originally Posted by Jacques1
You shouldn't call any "fetch" method at all unless you actually physically need the rows in an array.

Simply loop over the statement object, it's iterable:
PHP Code:
 $message->execute();
foreach (
$message as $row) {
    ...





@ fruanthony:

We need your full code. In the snippet above, there is no "$a".

thata is all about the code the only thing i have omitted is the php open and close tags
what i want to do is to display the result of that query could u help me????

Reply With Quote
  #11  
Old February 7th, 2013, 07:39 AM
fruanthony fruanthony is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 fruanthony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 30 m 42 sec
Reputation Power: 0
thata is all about the code the only thing i have omitted is the php open and close tags
what i want to do is to display the result of that query could u help me??

Reply With Quote
  #12  
Old February 7th, 2013, 01:03 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,872 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 1 h 24 m 30 sec
Reputation Power: 813
I asked you to post the full code you currently use, because the second code snippet makes absolutely no sense given the first snippet. What is "$a"? There is no "$a" in the first code. Maybe you mean $results? I don't know, so please write down the code. That shouldn't be too hard.

What's also weird is that sometimes you just write down an expression like $results['info'] without doing anything with it. That has no effect at all. It's like writing down "1 + 1;" in a single line. You have to actually output that like in
PHP Code:
echo 'Hallo world!'

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Problem with PDO insert script

Developer Shed Advertisers and Affiliates



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

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap