Domanda

I've been trying to accomplish this, but as other issues I just can't figured it out. I've been reading around for posibles solutions but non of them goes along with my code, or if they do I can't figure out how or where to use them.

I have a DB where a user sends records. The database consist in few tables containing the Following "Name, Lastname, Phone". If any of this values is duplicate, I would like my code to identify and Ignore the submission of the Form if ALL this VALUES already exist on the DB.

Here is my code:

        <?php

        $con = mysql_connect("HOST","USER","PASS");
        if (!$con)
          {
          die('Could not connect: ' . mysql_error());
          }

        mysql_select_db("testdb", $con);
        $sql="INSERT INTO people (Name, LastName, Phone)
        VALUES
        ('$_POST[Name]','$_POST[LastName]','$_POST[Phone]')";

        if (!mysql_query($sql,$con))
          {
          die('Error: ' . mysql_error());
          }

        echo "Record Added";

        mysql_close($con);
        ?>

Like I said, I've tried may ways but can't figure it out yet, I'm starting to learn, so excuse me if is something very simple. Thanks a lot everyone!!

È stato utile?

Soluzione

The mysql_* function are all deprecated now, and should NEVER be used. change your code to do something like the following:

//Set up a PDO connection to MySQL
$host = 'host_name';
$dbname = 'database_name';
$user = 'user_name';
$pass = 'user_pass';
try
{
    $DB = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
}
catch(PDOException $e)
{  
    echo $e->getMessage();  
}

//Determine whether the appropriate values have been passed.
if(isset($_POST['Name']))
{
    $name = $_POST['Name'];
}
else
{
    echo "You must provide a name!";
        exit; //This may not be what you want to do here, it's an example action
}

if(isset($_POST['LastName']))
{
        $name = $_POST['LastName'];
}
else
{
    echo "You must provide a last name!";
        exit; //This may not be what you want to do here, it's an example action
}

if(isset($_POST['Phone']))
{
        $name = $_POST['Phone'];
}
else
{
    echo "You must provide a phone number!";
        exit; //This may not be what you want to do here, it's an example action
}


//Set up the query using anonymous values
$sql="INSERT INTO people (Name, LastName, Phone) VALUES ('?','?','?')";
$sth = $DB->prepare($sql);

try
{
        //Attempt to execute the insert statement
        $sth->execute(array($_POST[Name], $_POST[LastName], $_POST[Phone]));
        echo "Record Added";

}
catch(PDOException $e)
{
        //If the insert failed, then you can handle the error, and determine
        //what further steps need to be taken.
        echo "Record Not Added";
}

Here's another question with a similar setting, that may also be useful to you:

https://stackoverflow.com/a/10414922/1507210

Altri suggerimenti

search in the table before insert

 <?php

        $con = mysql_connect("HOST","USER","PASS");
        if (!$con)
          {
          die('Could not connect: ' . mysql_error());
          }

        mysql_select_db("testdb", $con);

        $name =  mysql_real_escape_string($_POST[Name]);
        $LastName=  mysql_real_escape_string($_POST[LastName]);
        $Phone=  mysql_real_escape_string($_POST[Phone]);

         $search_res=mysql_query("SELECT * from people where Name='$Name' OR LastName='$LastName' OR Phone='$Phone'");

        if(mysql_num_rows($search_res) < 1){

             $sql="INSERT INTO people (Name, LastName, Phone)
             VALUES
                ('$Name','$LastName','$Phone')";

               if (!mysql_query($sql,$con))
              {
              die('Error: ' . mysql_error());
              }

        echo "Record Added";
       }else{
          echo "User Already exits";
       }

        mysql_close($con);
        ?>

Try this easy solution

$result = mysql_query("SELECT * FROM TABLE WHERE Column = 'value' ");

if( mysql_num_rows($result) < 1) {
    mysql_query("INSERT INTO table (column) VALUES ('value') ");
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top