73565

validation check on 3 fields to check duplication

Question:

i am new to php but trying to learn new things. i have a table in database with three fields. id, city and name. what i want is to check if all these three fields are equal by getting value from text box, then it show error else it would show me a link to redirect to a new page. but i am not able to run this query properly tried several things like count etc.

<form> <input id="text1" type="text" name="id" size="20" > <input id="text1" type="text" name="name" size="20" > <input id="text1" type="text" name="city" size="20" > <input id="text1" type="submit" name="submit" size="20" > </form> <?php $con=mysqli_connect("localhost","root","","test"); $check="SELECT * FROM eval WHERE id = '$_POST[id]' AND name = '$_POST[name]' AND city = '$_POST[city]' "; if( if all this data is same then ) { echo "Record already exist...<br/>"; } else { echo "<a href='NewUser.php'> New Sign Up </a> } ?>

Answer1:

<form> <input id="text1" type="text" name="id" size="20" > <input id="text1" type="text" name="name" size="20" > <input id="text1" type="text" name="city" size="20" > <input id="text1" type="submit" name="submit" size="20" > </form> <?php $con=mysqli_connect("localhost","root","","test"); $check="SELECT COUNT(*) FROM eval WHERE id = '$_POST[id]' AND name = '$_POST[name]' AND city = '$_POST[city]' "; $result = mysqli_query($con,$check); $count = mysqli_fetch_array($result); if(current($count) > 0) { echo "Record already exist...<br/>"; } else { echo "<a href='NewUser.php'> New Sign Up </a> } ?>

You have to fire the SQL Query to the database using mysqli_query(). Afterwards your checking if count of the rows is bigger than 0, which means you found 1 or more exact duplicated rows.

SELECT COUNT(*) Automatically counts the retrieved rows.

Answer2:

Okay so you have a HTML form and some PHP code that is to handle what happens when the form is submitted.

So, when the form is submitted then in order for PHP to register this submission is needs to know that the form was submitted.

You have a form that might work well on the client side if you were processing the form input via JavaScript but in PHP it is a little different.

So your form would need two additional things:

1.) A method attribute which specifies how the form data will be sent.

2.) An action defining where the form data will be sent to.

The most common methods would be GET and POST. And as you have the PHP code the handles the form on the same page as the form, the action will be the page itsself. So you have now:

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="text" name="id"> <input type="text" name="name"> <input type="text" name="city"> <input type="submit" name="clicked" value="formSubmit"> </form>

In PHP in order for you to register this input, you can use the event that occurs when the Submit button has been clicked. So you will have:

<?php # If the submit button was clicked if (isset($_POST['clicked']) && $_POST['clicked'] == 'formSubmit') { # Access the form inputs in php $id = $_POST['id']; $name = $_POST['name']; $city = $_POST['city']; # Helper to output mysql errors function processError($link) { echo "Error: Unable to connect to MySQL.<br>"; echo "Debugging errno: " . $link->errno . "<br>"; echo "Debugging error: " . $link->error . "<br>"; exit; } # Connect to the database // Create a database connection for PHP to use $link = mysqli_connect($DBHost, $DBUser, $DBPass, $DBName); // Ensure the connection is active if (!$link) { echo "Error: Unable to connect to MySQL.<br>"; echo "Debugging errno: " . mysqli_connect_errno() . "<br>"; echo "Debugging error: " . mysqli_connect_error() . "<br>"; exit; } // Sets encoding type to uft8 if (!mysqli_set_charset($link, 'utf8')) { processError($link); } # Do query // Build query $query = 'SELECT `id` '; $query .= 'FROM `eval` '; $query .= 'WHERE `id` = ? '; $query .= 'AND `name` = ? '; $query .= 'AND `city` = ? '; $query .= 'LIMIT 1 '; // Prepare the statement if (!$stmt = $link->prepare($query)) { processError($link); } // Bind in form values to prevent sql injection if (!$stmt->bind_param('iss', $id, $name, $city)) { processError($link); } // Execute the query if (!$stmt->execute()) { processError($link); } // Store the result $stmt->store_result(); // Store the number of rows returned $num_rows = $stmt->num_rows; // Close the statement $stmt->close(); # Check if the record exists if ($num_rows == 1) { echo 'Record already exist...<br/>'; } // No record exists else { echo '<a href="NewUser.php"> New Sign Up </a>'; } } ?>

Hope that helps you :-)

Recommend

  • What is 1NF truly?
  • Working with a severely limited interpreted language
  • How to find the last occurrence of a certain observation in grouped data in R?
  • Using Linq to get the last N number of rows that have duplicated values in a field
  • TFS edit build log using custom activity
  • Is Android development possible on netbooks? [closed]
  • How to resize image if the image is bigger than Textbox.But it not increase scale if smaller
  • Selection Sort, For Java
  • Chrome breakpoint on radio doesn't fire
  • Android application not restoring state when installed from .apk, works fine from eclipse
  • Javascript unload page condition
  • How to resolve dependencies from one gradle project to another gradle project in my Eclipse workspac
  • Specifying virtual keyboard type for EditText in XML
  • Android onKey w/ virtual keyboard
  • How do I Dispose a HttpResponseMessage in my Web Api Method?
  • Python cosine function precision [duplicate]
  • HttpClient: disabling chunked encoding
  • Trying to get the char code of ENTER key
  • pyodbc doesn't report sql server error
  • uniform generation of points on 3D box
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Exchange data b/w iOS devices using Bluetooth 4.0
  • PHP - How to update data to MySQL when click a radio button
  • Counter field in MS Access, how to generate?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • vba code to select only visible cells in specific column except heading
  • When should I choose bucket sort over other sorting algorithms?
  • Display Images one by one with next and previous functionality
  • How do you troubleshoot character encoding problems?
  • Do I've to free mysql result after storing it?
  • Transpose CSV data with awk (pivot transformation)
  • PHP: When would you need the self:: keyword?
  • SQL merge duplicate rows and join values that are different
  • Acquiring multiple attributes from .xml file in c#
  • Understanding cpu registers
  • Sorting a 2D array using the second column C++
  • reshape alternating columns in less time and using less memory
  • How can I use threading to 'tick' a timer to be accessed by other threads?