iEntry 10th Anniversary Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read
IT Discussion Forum Having IT issues? Got IT questions? Who doesn't? If you can't get your Apache to work with your MySQL or your php is choking on your ODBC... Let's see if we can help you come up with some ideas.

Share Thread: & Tags

Share Thread:

Tags
check, insert, mysql, php

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-02-2009, 06:11 PM
morestar's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: Jun 2007
Location: Burlington, Ontario (Toronto)
Posts: 1,011
morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5
Default PHP, check if a record was inserted via MySQL...

So basically I'm trying to determine of a record was actually inserted into my database with the insert statement in MySQL.

My code is as follows:

Code:
$sQuery = "INSERT INTO `$database`.`Users` 
(`User` ,`Email` ,`Email_verified`)
VALUES (NULL , '$sEmail', '0')";
mysql_query($sQuery);
From here I would like to send an email to the new member but before I do I need to know for sure that the record was inserted.

I have already setup checks before this step, that makes sure everything is totally ok before the insert but I would like it as a last precaution.

If anyone knows the proper syntax to check if a record was inserted into the MySQL database that would be great. I'm still going to search the net and find it...if I do I will post it here...
__________________
Join free dating sites and meet single people without paying a penny.
Reply With Quote
  #2 (permalink)  
Old 11-02-2009, 07:44 PM
Uncle Dog's Avatar
WebProWorld Pro
 
Join Date: Apr 2008
Location: Scotland
Posts: 269
Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5
Default Re: PHP, check if a record was inserted via MySQL...

if (mysql_query($sQuery))
{
echo "Record inserted";
}
else
{
echo "Error inserting record: " . mysql_error();
}
__________________
There are 10 types of people in this world: those who understand binary and those who don't.
Reply With Quote
  #3 (permalink)  
Old 11-02-2009, 08:11 PM
morestar's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: Jun 2007
Location: Burlington, Ontario (Toronto)
Posts: 1,011
morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5
Default Re: PHP, check if a record was inserted via MySQL...

Thank you UD, once I get the fricken mail to send, I'll add in the insert check...

thanks again man...
__________________
Join free dating sites and meet single people without paying a penny.
Reply With Quote
  #4 (permalink)  
Old 11-02-2009, 09:16 PM
morestar's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: Jun 2007
Location: Burlington, Ontario (Toronto)
Posts: 1,011
morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5
Default Re: PHP, check if a record was inserted via MySQL...

Yep it worked! I can't believe how long it actually takes to write a perfectly error proof registration form...

I added javascript validation and then had to write server side validation in case the javascript is turned off or not working for the user...added a little ajax to check if the username exists - in real time...

whew...that's done...

Thanks Uncle Dog...
__________________
Join free dating sites and meet single people without paying a penny.
Reply With Quote
  #5 (permalink)  
Old 11-02-2009, 09:17 PM
danlefree's Avatar
WebProWorld Pro
 
Join Date: Jun 2005
Location: Seattle
Posts: 270
danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4
Default Re: PHP, check if a record was inserted via MySQL...

You might want to try variable assignment instead of wrapping the mysql_query() call in an if/else statement (things can get tricky otherwise).

PHP Code:
$flag_inserted mysql_query($sQuery);

if ( 
$flag_inserted ) {

  
// Success handling
  // . . .

} else {

  
// Error handling
  // . . .


(Then you can chain multiple conditions)

In the future you may want to look at the Return Values section of the PHP manual for functions you are using.
__________________
Dan LeFree | Product Manager (Linux VPS Hosting) | Owner/Operator (Web development, marketing)
Reply With Quote
  #6 (permalink)  
Old 11-02-2009, 09:22 PM
morestar's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: Jun 2007
Location: Burlington, Ontario (Toronto)
Posts: 1,011
morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5
Default Re: PHP, check if a record was inserted via MySQL...

Ya I looked at the link you provided before I posted here...I couldn't quite figure out the syntax or way of coding it with those examples...

Now what do you mean by chaining multiple conditions?
__________________
Join free dating sites and meet single people without paying a penny.
Reply With Quote
  #7 (permalink)  
Old 11-02-2009, 09:24 PM
morestar's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: Jun 2007
Location: Burlington, Ontario (Toronto)
Posts: 1,011
morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5
Default Re: PHP, check if a record was inserted via MySQL...

Ya I did as you mentioned..

Code:
$result=mysql_query($sQuery);
   if($result){
     /// do this
   } else {
    dothat();
}
__________________
Join free dating sites and meet single people without paying a penny.
Reply With Quote
  #8 (permalink)  
Old 11-03-2009, 02:46 AM
wige's Avatar
Moderator
WebProWorld Moderator
 
Join Date: Jun 2006
Location: United States
Posts: 2,661
wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9
Default Re: PHP, check if a record was inserted via MySQL...

Another (tricky) way to do it is to get the insert id of the newly created record, assuming you are entering a new row into the database. This can be used in two ways - to confirm that the row was created successfully, and to perform additional work on the entry or to link other tables to the new entry. The function, run after the insert query, is mysql_insert_id(), which will return 0 if no new id was created. You can get more info and some samples here: PHP: mysql_insert_id - Manual

Another good method, which I often use, is to simply test if an error has been set. If the error id is 0, the previous command executed correctly, otherwise call the error handler... if (mysql_errno()) echo mysql_error;
__________________
The best way to learn anything, is to question everything.

Last edited by wige; 11-03-2009 at 02:50 AM.
Reply With Quote
  #9 (permalink)  
Old 11-03-2009, 05:06 AM
danlefree's Avatar
WebProWorld Pro
 
Join Date: Jun 2005
Location: Seattle
Posts: 270
danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4
Default Re: PHP, check if a record was inserted via MySQL...

Quote:
Originally Posted by morestar View Post
Now what do you mean by chaining multiple conditions?
There may be some cases where multiple queries must succeed (or other conditions must be met) before you perform an action - for example, if your form requires that the user be inserted and a registration e-mail be sent, you might want to cancel the user insert if there is a problem sending the e-mail.

(The mail() command will return FALSE on error as well)
__________________
Dan LeFree | Product Manager (Linux VPS Hosting) | Owner/Operator (Web development, marketing)
Reply With Quote
  #10 (permalink)  
Old 11-03-2009, 12:17 PM
morestar's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: Jun 2007
Location: Burlington, Ontario (Toronto)
Posts: 1,011
morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5
Default Re: PHP, check if a record was inserted via MySQL...

Very well wige and danlefree, thank you for your advice(s). I'm noticing that my php coding skills aren't nearly as advanced as some of the code I'm seeing out there...after I launch this new site I just might get a more experienced someone to take a peek at the code and optimize it as much as possible.

the sign up form is done except now I have to code to prevent sql injection...

wish me luck!
__________________
Join free dating sites and meet single people without paying a penny.
Reply With Quote
  #11 (permalink)  
Old 11-03-2009, 07:38 PM
danlefree's Avatar
WebProWorld Pro
 
Join Date: Jun 2005
Location: Seattle
Posts: 270
danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4
Default Re: PHP, check if a record was inserted via MySQL...

Quote:
Originally Posted by morestar View Post
the sign up form is done except now I have to code to prevent sql injection...

wish me luck!
How about we just wish that you use prepared statements?

No luck required
__________________
Dan LeFree | Product Manager (Linux VPS Hosting) | Owner/Operator (Web development, marketing)
Reply With Quote
  #12 (permalink)  
Old 11-03-2009, 08:23 PM
mushroom's Avatar
WebProWorld Pro
 
Join Date: Feb 2004
Location: Queen Charlotte B. C. Canada
Posts: 287
mushroom RepRank 0
Default Re: PHP, check if a record was inserted via MySQL...

The code I like to use
Code:
if (mysql_affected_rows() ==1)
{     # YOUR ACTION
}
__________________
Irony: That for most people the most "trusted" web site on the planet is for a company the has been convicted of criminal activity.

Both Security and SuSe start with "S". www.oldslides.com
Reply With Quote
  #13 (permalink)  
Old 11-03-2009, 09:06 PM
morestar's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: Jun 2007
Location: Burlington, Ontario (Toronto)
Posts: 1,011
morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5
Default Re: PHP, check if a record was inserted via MySQL...

Quote:
Originally Posted by danlefree View Post
How about we just wish that you use prepared statements?

No luck required
hm, I'll be looking into this danlefree...thank you!
__________________
Join free dating sites and meet single people without paying a penny.
Reply With Quote
  #14 (permalink)  
Old 11-03-2009, 09:58 PM
morestar's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: Jun 2007
Location: Burlington, Ontario (Toronto)
Posts: 1,011
morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5
Default Re: PHP, check if a record was inserted via MySQL...

Quoted from the page on Prepared Statements: however, if other portions of the query are being built up with unescaped input, SQL injection is still possible.

what would be considered unescaped input? I know it involves quotes but with a prepared statement when would unescaped input come into play - where?
__________________
Join free dating sites and meet single people without paying a penny.
Reply With Quote
  #15 (permalink)  
Old 11-03-2009, 10:37 PM
morestar's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: Jun 2007
Location: Burlington, Ontario (Toronto)
Posts: 1,011
morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5
Default Re: PHP, check if a record was inserted via MySQL...

so what would be the major difference between editing my existing code with addslashes();
compare to stored procedures...I have a feeling it's because stored procedures are cleaner and more advanced...

so wouldn't the following suffice?


Code:
<?php 
 
function sanitize_data($input_data) {
  return htmlentities(stripslashes($input_data), ENT_QUOTES);
} 
?>
__________________
Join free dating sites and meet single people without paying a penny.

Last edited by morestar; 11-03-2009 at 10:52 PM.
Reply With Quote
  #16 (permalink)  
Old 11-04-2009, 01:16 AM
danlefree's Avatar
WebProWorld Pro
 
Join Date: Jun 2005
Location: Seattle
Posts: 270
danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4
Default Re: PHP, check if a record was inserted via MySQL...

I think that mysql_real_escape_string() is the function you are looking for - combined with sprintf() it should handle all your edge cases (the Ctype functions may also be useful, if you would like to perform strict validation to avoid truncated inputs when calls to sprintf() are made and a regexp would be overkill) - and that is only if you are not using prepared statements to handle all variables in your queries.

The warning on the prepared statements document is intended to address kludges like this one:

PHP Code:
$stmt $dbh->prepare("INSERT INTO REGISTRY SET name=:name, value=:value, id=" $_GET['id'] );
$stmt->bindParam(':name'$name);
$stmt->bindParam(':value'$value); 
i.e. if one were using prepared statements without understanding the reason prepared statements are preferable over vulnerable statements
__________________
Dan LeFree | Product Manager (Linux VPS Hosting) | Owner/Operator (Web development, marketing)

Last edited by danlefree; 11-04-2009 at 01:23 AM.
Reply With Quote
  #17 (permalink)  
Old 11-04-2009, 11:05 AM
morestar's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: Jun 2007
Location: Burlington, Ontario (Toronto)
Posts: 1,011
morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5
Default Re: PHP, check if a record was inserted via MySQL...

whew! this is confusing but I'll get it...I'll get back to you with some results in the future but thank you all for your help and suggestions...I know I need to definitely write this code properly...
__________________
Join free dating sites and meet single people without paying a penny.
Reply With Quote
  #18 (permalink)  
Old 11-04-2009, 06:57 PM
Uncle Dog's Avatar
WebProWorld Pro
 
Join Date: Apr 2008
Location: Scotland
Posts: 269
Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5
Default Re: PHP, check if a record was inserted via MySQL...

It seems like a lot of people want you to run before you can walk.
Might I recommend a gentle stroll through PHP MySQL Introduction
__________________
There are 10 types of people in this world: those who understand binary and those who don't.
Reply With Quote
  #19 (permalink)  
Old 11-04-2009, 07:18 PM
morestar's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: Jun 2007
Location: Burlington, Ontario (Toronto)
Posts: 1,011
morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5
Default Re: PHP, check if a record was inserted via MySQL...

lol Uncle Dog you're funny...I'm not that basic in SQL and PHP. No where near that !! I've got PHP, ColdFusion, ASP - SQL under my belt...PM me, I'll send you my resume...

__________________
Join free dating sites and meet single people without paying a penny.

Last edited by morestar; 11-04-2009 at 07:36 PM. Reason: addendum
Reply With Quote
  #20 (permalink)  
Old 11-04-2009, 07:38 PM
Uncle Dog's Avatar
WebProWorld Pro
 
Join Date: Apr 2008
Location: Scotland
Posts: 269
Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5
Default Re: PHP, check if a record was inserted via MySQL...

It's not that basic.
__________________
There are 10 types of people in this world: those who understand binary and those who don't.
Reply With Quote
  #21 (permalink)  
Old 11-05-2009, 12:20 AM
morestar's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: Jun 2007
Location: Burlington, Ontario (Toronto)
Posts: 1,011
morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5morestar RepRank 5
Default Re: PHP, check if a record was inserted via MySQL...

Quote:
Originally Posted by Uncle Dog View Post
It's not that basic.
I do like that site though...and I like tizag.com, really good examples...

So once I get a moment and get back into this, I'll touch base with you guys...thanks for the help too...
__________________
Join free dating sites and meet single people without paying a penny.
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > IT Discussion Forum

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
Current Loan rates dynamicly inserted into site DCnotPC Webmaster Resources Discussion Forum 2 01-18-2008 03:44 PM
single quote in mysql record kruser Database Discussion Forum 6 08-28-2007 11:16 PM
How a message sent through email can be inserted in database on serverside? sinnan Web Programming Discussion Forum 1 08-07-2007 09:24 AM
How do I remove the Dreamweaver Check In/Check Out Feature?? WebMasterKrames Graphics & Design Discussion Forum 2 03-28-2007 12:22 PM
Why is this line being inserted...? Dragonsi Internet Security Discussion Forum 4 11-30-2004 06:11 PM


All times are GMT -4. The time now is 02:18 PM.



Search Engine Optimization by vBSEO 3.3.0