ambo Posted May 21, 2016 Share Posted May 21, 2016 <?php include_once 'dbconnect.php'; include_once 'securitycheck.php'; if(isset($_POST['btn-signup'])) { $cl_fname = mysql_real_escape_string($_POST['cl_fname']); $cl_lname = mysql_real_escape_string($_POST['cl_lname']); $cl_address = mysql_real_escape_string($_POST['cl_address']); $cl_city = mysql_real_escape_string($_POST['cl_city']); $cl_state = mysql_real_escape_string($_POST['cl_state']); $cl_zipcode = mysql_real_escape_string($_POST['cl_zipcode']); $cl_hphone = mysql_real_escape_string($_POST['cl_hphone']); $cl_cphone = mysql_real_escape_string($_POST['cl_cphone']); $cl_fname = trim($cl_fname); $cl_lname = trim($cl_lname); $cl_address = trim($cl_address); $cl_city = trim($cl_city); $cl_state = trim($cl_state); $cl_zipcode = trim($cl_zipcode); $cl_hphone = trim($cl_hphone); $cl_cphone = trim($cl_cphone); // Checks to see if the names from the form exist in DB $query1 = "SELECT cl_lname cl_fname FROM clientdata WHERE cl_lname='$cl_lname'"; $result1 = mysql_query($query1); $count1 = mysql_num_rows($result1); $query2 = "SELECT cl_fname FROM clientdata WHERE cl_fname='$cl_fname'"; $result2 = mysql_query($query2); $count2 = mysql_num_rows($result2); if($count1 == 0){ if(mysql_query("INSERT INTO clientdata(cl_fname,cl_lname,cl_address,cl_city,cl_state,cl_zipcode,cl_hphone,cl_cphone) VALUES('$cl_fname','$cl_lname','$cl_address','$cl_city','$cl_state','$cl_zipcode','$cl_hphone','$cl_cphone')")) { ?> <META http-equiv="refresh" content="0;URL=client_list.php"> <?php } else { ?> <script>alert('There was a Error');</script> <?php } } else{ if($count2 == 0){ if(mysql_query("INSERT INTO clientdata(cl_fname,cl_lname,cl_address,cl_city,cl_state,cl_zipcode,cl_hphone,cl_cphone) VALUES('$cl_fname','$cl_lname','$cl_address','$cl_city','$cl_state','$cl_zipcode','$cl_hphone','$cl_cphone')")) { ?> <META http-equiv="refresh" content="0;URL=client_list.php"> <?php } else { ?> <script>alert('There was a Error');</script> <?php } } else{ ?> <script>alert('Customer already exists');</script> <?php } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Runkys</title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body id="body"> <center> <?php include_once 'header.php'; ?> <div id="mainbody"> <form id="login-form" method="post"> <table id="inputtable" align="center" width="70%" border="0"> <tr> <td><input type="text" name="cl_fname" placeholder="First Name" required /></td> <td><input type="text" name="cl_lname" placeholder="Last Name" required /></td> </tr> <tr> <td><input type="text" name="cl_address" placeholder="Address" required /></td> <td><input type="text" name="cl_city" placeholder="City" required /></td> </tr> <tr> <td> <select name="cl_state"> <option value="CT">Connecticut</option> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="AZ">Arizona</option> <option value="AR">Arkansas</option> <option value="CA">California</option> <option value="CO">Colorado</option> <option value="DE">Delaware</option> <option value="DC">District Of Columbia</option> <option value="FL">Florida</option> <option value="GA">Georgia</option> <option value="HI">Hawaii</option> <option value="ID">Idaho</option> <option value="IL">Illinois</option> <option value="IN">Indiana</option> <option value="IA">Iowa</option> <option value="KS">Kansas</option> <option value="KY">Kentucky</option> <option value="LA">Louisiana</option> <option value="ME">Maine</option> <option value="MD">Maryland</option> <option value="MA">Massachusetts</option> <option value="MI">Michigan</option> <option value="MN">Minnesota</option> <option value="MS">Mississippi</option> <option value="MO">Missouri</option> <option value="MT">Montana</option> <option value="NE">Nebraska</option> <option value="NV">Nevada</option> <option value="NH">New Hampshire</option> <option value="NJ">New Jersey</option> <option value="NM">New Mexico</option> <option value="NY">New York</option> <option value="NC">North Carolina</option> <option value="ND">North Dakota</option> <option value="OH">Ohio</option> <option value="OK">Oklahoma</option> <option value="OR">Oregon</option> <option value="PA">Pennsylvania</option> <option value="RI">Rhode Island</option> <option value="SC">South Carolina</option> <option value="SD">South Dakota</option> <option value="TN">Tennessee</option> <option value="TX">Texas</option> <option value="UT">Utah</option> <option value="VT">Vermont</option> <option value="VA">Virginia</option> <option value="WA">Washington</option> <option value="WV">West Virginia</option> <option value="WI">Wisconsin</option> <option value="WY">Wyoming</option> </select> </td> <td><input type="text" name="cl_zipcode" placeholder="Zipcode" required /></td> </tr> <tr> <td><input type="text" name="cl_hphone" placeholder="Home Phone" required /></td> <td><input type="text" name="cl_cphone" placeholder="Cell Phone" required /></td> </tr> <tr> <td><button type="submit" name="btn-signup">Add Client</button></td> </tr> <tr> <td><a href="home.php">Cancel</a></td> </tr> </table> </form> </div> </center> </body> </html> I have a simple script and I am new to this so the basic thing is I have a form that I can input client data into a database for later recall... if you fill out the form it will add the info to database so far I am able to grab the data have the script check the last name if the last name is in the DB it then checks to see if the first name is in the database. if the first name isn't in the database then its a new client with same last name it will add.. the issue is after it pulls the last name and there is a duplicate it then checks all the rows for the first name so if you have fname1, lname1 fname2,lname2 and I want to add fname2,lname1 I cant any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/301226-help-if-data-exists/ Share on other sites More sharing options...
ginerjm Posted May 21, 2016 Share Posted May 21, 2016 Why do you look for just first or last instead of both at the same time? Quote Link to comment https://forums.phpfreaks.com/topic/301226-help-if-data-exists/#findComment-1533136 Share on other sites More sharing options...
Solution ambo Posted May 21, 2016 Author Solution Share Posted May 21, 2016 SELECT cl_lname ,cl_fname FROM clientdata WHERE cl_lname='$cl_lname' AND cl_fname='$cl_fname' so something like this Quote Link to comment https://forums.phpfreaks.com/topic/301226-help-if-data-exists/#findComment-1533137 Share on other sites More sharing options...
benanamen Posted May 21, 2016 Share Posted May 21, 2016 (edited) More importantly, you are using obsolete code that has been completely removed from Php. You need to use PDO with prepared statements. https://phpdelusions.net/pdo Also, counting on the name of a button to be submitted for your script to work is a bad idea. It will completely fail in certain circumstances. You need to use if ($_SERVER['REQUEST_METHOD'] == 'POST') Its about time you start using HTML5 as well. Using tables and obsolete html for page layout went out 20 years ago (align="center"). You use CSS for page formatting. You also dont need to trim each individual input. You can trim the entire POST array at one time. Your code is seriously outdated and needs to be completely re-written. Edited May 21, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/301226-help-if-data-exists/#findComment-1533138 Share on other sites More sharing options...
ambo Posted May 21, 2016 Author Share Posted May 21, 2016 Thank you just a new way of thinking I am blind sometimes Quote Link to comment https://forums.phpfreaks.com/topic/301226-help-if-data-exists/#findComment-1533139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.