查询重复项失败

时间:2014-03-06 14:44:08

标签: php

我需要帮助来确认我的代码是否正确并且没有php错误。我想要做的是查询MySql以查找值($ box),如果该值存在,则将错误发回jquery。现在这是我的问题。当我直接在浏览器窗口中运行我的代码时,只有一个白色屏幕。我尝试了各种echo和var_dump,但仍然是空白的。没有任何价值被发送回jquery,所以我不知道如何继续。

我相当缺乏经验,所以欢迎任何指导或评论(好的或坏的)。感谢

<?php
    session_start();

    // Connection config
    function runSQL($rsql) {
    $hostname = "localhost";
    $username = "root";
    $password = "";
    $dbname   = "sample";
    $connect = mysql_connect($hostname,$username,$password) or die ("Error: could not connect to database");
    $db = mysql_select_db($dbname);
    $result = mysql_query($rsql) or die (mysql_error()); 
    return $result;
    mysql_close($connect);

    // test vars from jquery form
    $status = mysql_real_escape_string($_REQUEST['status']);
    $company = mysql_real_escape_string($_REQUEST['company']);
    $requested = mysql_real_escape_string($_REQUEST['requested']);
    $activity = mysql_real_escape_string($_REQUEST['activity']);
    $address = mysql_real_escape_string($_REQUEST['address2']);
    $service = mysql_real_escape_string($_REQUEST['service']);
    $box = mysql_real_escape_string($_REQUEST['box_rtv']);
    $authorised = mysql_real_escape_string($_SESSION['kt_name_usr']);
    $dept = mysql_real_escape_string($_REQUEST['rtv_dept']);

    // Split the box if multiples
    $array = split('[,]', $_REQUEST['box_rtv']);

    // Loop to split if multiple request and check DB for dupe entries
    foreach ($array as $box) {
    $sql = "SELECT item FROM act WHERE item = '$box'";
    $result = runSQL($sql) or die(mysql_error());

    // If there are dupe entries, send message to jquery
    if (mysql_num_rows($result)>0) {
        echo 'Error';
        return; 
    }
    } else {

        // If no dupes, then enter values into DB.
        $form = array();
        foreach ($array as $box) {
        $form = array('dept'=>$dept,
        'company'=>$company,
        'address'=>$address,
        'service'=>$service,
        'box'=>$box,
        'destroydate'=>$destroydate,
        'authorised'=>$authorised,
        'submit'=>$submit);

        $sql = "INSERT INTO `temp` (service, activity, department, company, address, user, date, item, new) VALUES ('$service', '$activity', '$dept', '$company', '$address', '$requested', NOW(), '$box', 1)";
        $result = runSQL($sql) or die(mysql_error());
    }
    }
?>

0 个答案:

没有答案