Re: Seeking example code

From: Date: Thu, 30 Nov 2000 16:18:13 +0000
Subject: Re: Seeking example code
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-28068@lists.php.net to get a copy of this message
These aren't the tightest, neatest code you can come up with, but they work for general stuff... Here are two functions I use to input a new record into my personals db, the first one checks to make sure the username is unique (I call the needed function using case statements which are dependent on hidden form fields): function username_exists( $_uname ) { $user = ""; $pass = ""; $db = "personals"; $db_connect = mysql_connect( "localhost", $user , $pass ); if ( ! mysql_select_db( $db, $db_connect) ) { $dberror = mysql_error(); return false; } $dup_name_result = mysql_query( "SELECT _uname FROM gay WHERE _uname = '".$_uname."'", $db_connect ); $num_rows_returned = mysql_num_rows( $dup_name_result ); if ( $num_rows_returned > 0 ) { print "that name is taken!"; exit; } // print "$dup_name_result<BR><BR>"; - for debugging only // print "$num_rows_returned"; - for debugging only } function add_to_db( $_fname, $_lname, $_email, $_uname, $_pword, $_sex, $_age, $_city, $_state, $_orientation, $_status, $_race, $_religion, $_height, $_weight, $_hair, $_eyes, $_education, $_occupation, $_interests, $_match, $_other, $_date, &$dberror ) { $user = ""; $pass = ""; $db = "personals"; $db_connect = mysql_connect( "localhost", $user , $pass ); if ( $_uname == "" || $_pword == "" ) { print "<BR><BR><center><font color=\"red\">You must enter both a username and password.<BR><BR></center></font>"; print_form_full_reg( $_fname, $_lname, $_email, $_uname, $_pword, $_sex, $_age, $_city, $_state, $_orientation, $_status, $_race, $_religion, $_height, $_weight, $_hair, $_eyes, $_education, $_occupation, $_interests, $_match, $_other, $_date ); exit; } if ( ! $db_connect ) { $dberror = "Couldn't connect"; return false; } if ( ! mysql_select_db( $db, $db_connect) ) { $dberror = mysql_error(); return false; } $db_query = "INSERT INTO gay ( _fname, _lname, _email, _uname, _pword, _sex, _age, _city, _state, _orientation, _status, _race, _religion, _height, _weight, _hair, _eyes, _education, _occupation, _interests, _match, _other, _date ) values ( '$_fname', '$_lname', '$_email', '$_uname', '$_pword', '$_sex', '$_age', '$_city', '$_state', '$_orientation', '$_status', '$_race', '$_religion', '$_height', '$_weight', '$_hair', '$_eyes', '$_education', '$_occupation', '$_interests', '$_match', '$_other', '$_date' )"; if ( ! mysql_query( $db_query, $db_connect ) ) { $dberror = mysql_error(); return false; } return true; } Here is a function I use to update info in a db: function update_db ( $_fname, $_lname, $_email, $_uname, $_pword, $_sex, $_age, $_city, $_state, $_orientation, $_status, $_race, $_religion, $_height, $_weight, $_hair, $_eyes, $_education, $_occupation, $_interests, $_match, $_other, $_date, &$dberror ) { $user = ""; $pass = ""; $db = "personals"; $db_connect = mysql_connect( "localhost", $user , $pass ); if ( ! $db_connect ) { $dberror = "Couldn't connect to update."; return false; } if ( ! mysql_select_db( $db, $db_connect) ) { $dberror = mysql_error(); return false; } $db_query = "UPDATE gay SET _fname='$_fname', _lname='$_lname', _email='$_email', _pword='$_pword', _sex='$_sex', _age='$_age', _city='$_city', _state='$_state', _orientation='$_orientation', _status='$_status', _race='$_race', _religion='$_religion', _height='$_height', _weight='$_weight', _hair='$_hair', _eyes='$_eyes', _education='$_education', _occupation='$_occupation', _interests='$_interests', _match='$_match', _other='$_other', _date='$_date' WHERE _uname='$_uname'"; $update_result = mysql_query( $db_query, $db_connect ); if ( ! $update_result ) { print "There has been an error updating your account, please try again later."; print "<BR>Return to <a href=\"http://www.blahblah.com/personals\">Personals Home Page</a>"; exit; } else { print "<BR>Your record has been updated, thank you for using the personals system.<BR>"; exit; } return true; } ----- Original Message ----- From: "Richard Jolitz" <rich@outposttrader.com> To: <php-general@lists.php.net> Sent: Thursday, November 30, 2000 10:58 AM Subject: [PHP] Seeking example code > Morning Folks! > > Being new to PHP and never doing anything the easy way when learning I'm > wondering if any of you can point me towards some example code that shows > how to: > > 1. check a mysql database for user info > 2. update that info > 3. create a new record > > I'm puzzling through some documentation but I tend to work better with > examples. > > Thanks! > > > > _________________________________________________________ > Serving All Your Telecommunication Needs: > http://ld.net/?outposttrade > Agents Needed NOW! : > http://telcom-mlm.com/bizop.cgi?outposttrade > Family Friendly ISP - Filters Porn/Hate : http://i4f.com/sales.cgi/311rj9805 > Great Gifts: http://outposttrader.com/ > Titanic Collectibles - Endorsed by Harland & Wolff: > http://outposttrader.com/titanic/ > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#28068) next »