0

Below I have a simple form field. This field, has an INPUT next to "name your card" with a name='name'. Well, I want to be sure that people cannot duplicate the name that is entered into this, as it goes into my database and users are able to retrieve the information based on this name field. I'm having a tough time figuring out how to avoid duplicated names in this name='name' field. Any help would extremely appreciative.

Here is my home page

<div class="field class">
<form action='newlist.php' method='POST'>
Name Your Card <input class="ha" type='text' name='name' required/>
<textarea class="hello" type='text' name='one' placeholder=''></textarea> <p>
<textarea class="hello" type='text' name='two'></textarea> <p>
<input type='submit' value='create'/>

and here is the page that the home page form field is directed to

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "christmas";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql="INSERT INTO list (name, one)
VALUES
('$_POST[name]','$_POST[one]' , '$_POST[two]')";

if ($conn->query($sql) === TRUE) {
    echo "We have successfully saved your name ";  echo  "Be sure to share your list,  '"; echo  $_POST['name'] ; echo "' with friends!";

} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
kenny
  • 438
  • 4
  • 14
  • how about a lookup saying it is already taken, and a unique key in the table to enforce it (no dupes at table level) – Drew Nov 04 '15 at 22:09
  • That is exactly what I would want. I just wouldn't know how to do that. – kenny Nov 04 '15 at 22:10
  • 1
    so, setup a UNIQUE constraint (that's the best way). Btw, if that's your actual code, you didn't close your form. You can also see this Q&A http://stackoverflow.com/questions/22252904/check-if-row-exists-with-mysqli to check if a row exists. – Funk Forty Niner Nov 04 '15 at 22:14
  • Thanks Fred -ii-. Yeah the form is closed in my actual code. I will check out that link. – kenny Nov 04 '15 at 22:16
  • 1
    You're also open to some serious SQL injection here. Edit: You're welcome @kenny *Cheers* – Funk Forty Niner Nov 04 '15 at 22:16

0 Answers0