doutuobao4004 2015-01-24 15:39
浏览 121

如何在插入php之前检查数据库中是否存在数据

$sqlorg = mysql_query("SELECT * FROM `organization`");
while($orgrows = mysql_fetch_array($sqlorg)) {
    //$dborgid = $orgrows['org_id'];
    $dborgnme = $orgrows['org_name'];
}

if ($dborgnme == $orgexist) {
    echo "<script type='text/javascript'>
            alert('Organization Name Already Used by other Organization');
            history.back();
        </script>";
} else {
    $orginsrt = mysql_query("INSERT INTO `organization`(`org_id`,`org_name`,`org_desc`,`category`,`vision`,`mission`,`col_id`,`image`) VALUES ('$orgid','$orgexist','$orgdesc','$orgcat','$orgvis','$orgmis','$getcol','$image')");

    echo "<script type='text/javascript'>
        alert('Proceed to next Step');</script>";
        //require ('orgsignup.php');
        header ('Location:orgsignup2.php');
    //echo "Not in the Record";

}
  • 写回答

1条回答 默认 最新

  • dongzhuang2030 2015-01-24 16:30
    关注

    There are multiple issues with this question, and as such can't easily be answered - I'm writing this "answer" as a quick guide to Kio Rii and to get more information:

    1) Don't use MySQL, use MySQLi for procedural DB/PHP interactions.

    2)

    while($orgrows = mysql_fetch_array($sqlorg)) {
        //$dborgid = $orgrows['org_id'];
        $dborgnme = $orgrows['org_name'];
    }
    

    The value $dborgnme will only ever hold the final value from all the rows fetched from the database. Consider reformatting the While statement to wrap outside the following if(){} ... else{}

    3) Add some context and information to your question - what are you checking for? Where do values such as $orgexist come from? What events do you want to occur if a data exists or what happens if a data doesn't exist?

    4) If you're only checking the name, you can better do it with a better SELECT statement as the current select is grabbing all the MySQL rows, so try

    take :

    $sqlorg = mysql_query("SELECT * FROM `organization`");
    

    and turn it into

    $sqlorg = mysql_query("SELECT * FROM `organization` WHERE org_name LIKE '%".$orgexist."%' ");
    

    which will only return you rows if the names are very similar. You can then use the output of this (count of rows returned) to carry on the script logic.

    5) Yes, my solution in part 4 is quick and dirty, and PDO or MySQLi prepared statements are much, MUCH better and more secure than old MySQL and variable injection into the SQL statement.

    Additional debugging:

    you would probably find this very useful:

    $orginsrt = mysql_query("INSERT INTO `organization`(`org_id`,`org_name`,`org_desc`,`category`,`vision`,`mission`,`col_id`,`image`) VALUES ('$orgid','$orgexist','$orgdesc','$orgcat','$orgvis','$orgmis','$getcol','$image')") or die("insert fail: ".mysql_error());
    

    This will tell you why an insert failed, if it did fail.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值