PHP:如果来自$ _POST的数据与表中的数据匹配,则运行mysql_query

时间:2014-02-11 05:58:30

标签: php mysql post if-statement

我希望我的标题不会让人感到困惑。我想首先说我现在是一名程序员,并且是PHP和MySQL的业余爱好者,我用它来进行在线游戏。我已经在这里不知疲倦地工作了几天,没有成功。我一直在想在这里寻求帮助的想法,希望大家对我很轻松,不要完全撕掉我的代码!就像我说的,我是个业余爱好者。

基本上,我要做的是将$horsename数组中的$_POST数据与名为name的表中的horses相匹配。如果它们不匹配,则会将name的马添加到horses表中。如果它们匹配,它将继续,并将$_POST数组中的数据添加到每行的results表中。

即使$horsenamename中的horses匹配,我得到的问题(并且我多次玩弄此问题,每次都会出现不同的问题) } table,它尝试将新马添加到horses表中。它也没有转移到下一行数据,并试图一遍又一遍地添加相同的马。 (希望有意义!)

我正在从下面的这个页面粘贴我的大部分代码,以防万一我的代码更早出现此问题。请注意,此代码的一部分不是我自己的,我正在为其他人工作,所以如果事情在几个地方不完全一致,那就是原因。我正在研究的部分就是我上面提到的。

function stripslashes_deep($value) {
    $value = is_array($value) ?
        array_map('stripslashes_deep', $value) :
        stripslashes($value);
    return $value;
}

$results = str_replace("\r", '', trim($_POST['news'])); 
$data = array();
$lines = explode("\n", $results);
foreach ($lines as $place) {

    if (!empty($place)) {
        $data = array();
        $detail = explode(",", $place);
        if (!empty($detail)) {               
            $id = '';
            $show = $_POST['show'];
            $year = $_POST['year'];
            $association = $_POST['association'];
            $chpoints = $_POST['chpoints'];
            $rchpoints = $_POST['rchpoints'];
            $ttpoints = $_POST['ttpoints'];
            $chearnings = $_POST['chearnings'];
            $rchearnings = $_POST['rchearnings'];
            $ttearnings = $_POST['ttearnings'];
            $horsename = stripslashes(trim($detail[0]));
            $placement = stripslashes(trim($detail[1]));
            $class = stripslashes(trim($detail[2]));

            if($placement === 'CH'){
                $points = $chpoints;
            }
            else if ($placement === 'RCH') {
                $points = $rchpoints;
            }
            else {
                $points = $ttpoints;
            }
            if ($placement === 'CH') {
                $earnings = $chearnings;
            }
            else if ($placement === 'RCH') {
                $earnings = $rchearnings;
            }
            else {
                $earnings = $ttearnings;
            }

            $horses = mysql_query("SELECT name FROM horses") or die ('Error accessing database: ' . mysql_error());;
            while($row = mysql_fetch_array($horses)) {

                $storedname = addslashes(trim($row['name']));

                if ($storedname == $horsename) {
                    echo "The names do match for $horsename";
                } 
                else {
                    echo "The names do not match for $horsename";
                    $addhorse="INSERT INTO horses (id, owned_by, name, yob, color, breed, discipline, sire, dam, damsire, bred_by, gender)
                                        VALUES ('','25','$horsename','','','','','','','','','')";
                    mysql_query($addhorse) or die ('Error updating database: ' . mysql_error());
                    echo 'Added '. $horsename .' to Archive.';
                }
            }

            if (isset($_POST['news'])) {
                $query="INSERT INTO `results` (`id`, `show`, `year`, `place`, `name`, `class`, `points`)
                                        VALUES ('$id','$show','$year','$placement','$horsename','$class','$points')";
                mysql_query($query) or die ('Error updating database: ' . mysql_error());
                echo "Result successfully added!" ;
            }


        }; 
    };
};

要从上面抓一个剪辑,这就是我遇到问题的地方:

$horses = mysql_query("SELECT name FROM horses") or die ('Error accessing database: ' . mysql_error());;
while($row = mysql_fetch_array($horses)) {

    $storedname = addslashes(trim($row['name']));

    if ($storedname == $horsename) {
        echo "The names do match for $horsename";
    } 
    else {
        echo "The names do not match for $horsename";
        $addhorse="INSERT INTO horses (id, owned_by, name, yob, color, breed, discipline, sire, dam, damsire, bred_by, gender)
                                        VALUES ('','25','$horsename','','','','','','','','','')";
        mysql_query($addhorse) or die ('Error updating database: ' . mysql_error());
        echo 'Added '. $horsename .' to Archive.';
    }
}

如果需要news来自的页面,请告知我们。 提前谢谢!

2 个答案:

答案 0 :(得分:2)

问题是您正在查询数据库以获取每个马名称的列表。您正在遍历该列表,每次名称不匹配时,您都要插入新名称。您需要做的是查询特定名称。

SELECT * FROM horses WHERE name = '$horsename'

如果这返回一行,那么您就知道该马已经在数据库中。如果它没有返回任何行,那么您可以安全地插入一次。顺便说一句,你需要正确地转义输入以防止SQL注入,所以不要逐字使用我的代码。

答案 1 :(得分:0)

试试这个:

    $horses = mysql_query("SELECT name FROM horses") or die ('Error accessing database: ' . mysql_error());;
    $i = 0;
    $horsename = "";

    while($row = mysql_fetch_array($horses)) {

        $storedname = addslashes(trim($row['name']));

        if ($storedname == $horsename) {
            $i = 1;
        }
    }

    if($i == 1) {
        echo "The names do match for $horsename";
    }
    else {
        echo "The names do not match for $horsename";
        $addhorse="INSERT INTO horses (id, owned_by, name, yob, color, breed, discipline, sire, dam, damsire, bred_by, gender)
            VALUES ('','25','$horsename','','','','','','','','','')";
            mysql_query($addhorse) or die ('Error updating database: ' . mysql_error());
         echo 'Added '. $horsename .' to Archive.';
    }