|
|
||||||
|
||||||
| Index Link To US Private Messages Archive FAQ RSS | ||||||
| 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
|
||||||
|
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
||||
|
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); 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. |
|
||||
|
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. |
|
||||
|
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. |
|
||||
|
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:
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) |
|
||||
|
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. |
|
||||
|
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. |
|
||||
|
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. |
|
||||
|
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) |
|
||||
|
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. |
|
||||
|
Quote:
No luck required
__________________
Dan LeFree | Product Manager (Linux VPS Hosting) | Owner/Operator (Web development, marketing) |
|
||||
|
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 |
|
||||
|
Quote:
__________________
Join free dating sites and meet single people without paying a penny. |
|
||||
|
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. |
|
||||
|
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. |
|
||||
|
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:
__________________
Dan LeFree | Product Manager (Linux VPS Hosting) | Owner/Operator (Web development, marketing) Last edited by danlefree; 11-04-2009 at 01:23 AM. |
|
||||
|
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. |
|
||||
|
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. |
|
||||
|
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 |
|
||||
|
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. |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
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 |
|
WebProWorld |
Advertise |
Contact Us |
About |
Forum Rules |
MVP's |
Archive |
Newsletter Archive |
Top |
WebProNews
WebProWorld is an iEntry, Inc. ® site - © 2009 All Rights Reserved Privacy Policy and Legal iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509 |