数据没有INSERT到db ...需要建议

时间:2013-04-29 07:30:56

标签: php mysql

这是我希望我的SQL语句如何将数据插入db:

INSERT into tbl_main (ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name)

ars_nophone_numbercreate_date的值将来自我的textareas ...这些textareas中的每一行将对应于我对我的文本字段的固定值是category_1category_2statusresolved_datetrouble_type_priorityban_typeemployee_id_name

所以我的textareas中的每一行在插入我的sql时都是一个新行,所有要创建的行都会从我的textfields中添加固定值......

但是每当我尝试INSERT数据时...它给我一条消息说它成功但是当我检查db时没有数据,下面是我的sql代码,请告知我的代码是否正确或是否可以做得更好,

error_reporting(E_ALL);
    require 'include/DB_Open.php';

    if(isset($_POST['DBLoad']))
    {
$Category2 = $_POST['Category2'];
    $Category3 = $_POST['Category3'];
    $Status = $_POST['Status'];
    $Date = $_POST['Date'];
    $Severity = $_POST['Severity'];
    $BanType = $_POST['BanType'];
    $XiD = $_POST['XiD'];
    $Ticket = $_POST['Ticket'];

    //Process the input textareas into arrays
    $PhoneNumber = array_map('mysql_real_escape_string', explode("\n", $_POST['PhoneNumber']));
    $Createdate = array_map('mysql_real_escape_string', explode("\n", $_POST['Createdate']));
    $RemedyTicketNo = array_map('mysql_real_escape_string', explode("\n", $_POST['PhoneNumber']));

    //Determine the values with the least amount of elements
    $min_count = min($PhoneNumber, $Createdate, $RemedyTicketNo);

    //Create array to hold INSERT values
    $values = array();

    //Create the INSERT values
    for($index=0; $index<$min_count; $index++)
    {
    $values[] = "('$RemedyTicketNo[$index]','$PhoneNumber[$index]','$Createdate[$index]',
    '$Category2','$Category3','$Status','$Date','$Severity','$BanType','$XiD')";  
    }


    $sql=mysql_query("INSERT into tbl_main
        (ars_no,phone_number,create_date,category_1,category_2,status,resolved_date,trouble_type_priority,ban_type,employee_id_name)
        VALUES " . implode (',',$values));
    if (!$sql) {
    die('Invalid query: ' . mysql_error());
    }

    header("Location: smp_backend_test.php");
    }

我想我在这里发现了错误,我已经回复了我的INSERT声明并且只做了这个:

 INSERT into tbl_main (ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name) VALUES ('('1', '1', '1'), ('2', '2', '2')', 'SMP_Backend', 'Pending Request', 'Resolved', '2013-04-28', '5', 'I', 'AAA')

我想要它做的是我的textarea中的每一行都与我的textfields值相关联,有没有办法编辑我的INSERT语句以便它在下面执行,它应该这样做:

INSERT into tbl_main (ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name) 
    VALUES ('('1', '1', '1', 'SMP_Backend', 'Pending Request', 'Resolved', '2013-04-28', '5', 'I', 'AAA'), ('2', '2', '2', 'SMP_Backend', 'Pending Request', 'Resolved', '2013-04-28', '5', 'I', 'AAA')')

3 个答案:

答案 0 :(得分:1)

首先,双引号中的数组值必须使用大括号{} -

$values[] = "('{$RemedyTicketNo[$index]}','{$PhoneNumber[$index]}','{$Createdate[$index]}',
'$Category2','$Category3','$Status','$Date','$Severity','$BanType','$XiD')";  
}

另外,请尝试将die('Invalid query: ' . mysql_error())添加到查询结尾 -

$sql=mysql_query("INSERT into tbl_main
      (ars_no,phone_number,create_date,category_1,category_2,status,resolved_date,trouble_type_priority,ban_type,employee_id_name)
    VALUES " . implode (',',$values)) or die('Invalid query: ' . mysql_error());

header("Location: smp_backend_test.php");
即使$sql失败,

mysql_query()也会返回true,因此if (!$sql) {die('Invalid query: ' . mysql_error());}将无法执行,or die(..)


请注意,从文档http://us2.php.net/manual/en/function.mysql-query.php开始,{PHP从2005年起,mysql_*扩展名已弃用,将来也会被删除。相反,应使用MySQLiPDO_MySQL扩展名。有关详细信息,另请参阅MySQL: choosing an API指南和related FAQ

答案 1 :(得分:1)

$min_count = min(count($PhoneNumber), count($Createdate), count($RemedyTicketNo));

答案 2 :(得分:0)

而不是

$sql=mysql_query("INSERT into tbl_main
    (ars_no,phone_number,create_date,category_1,category_2,status,resolved_date,trouble_type_priority,ban_type,employee_id_name)
    VALUES " . implode (',',$values));
if (!$sql) {
die('Invalid query: ' . mysql_error());
}

使用

$sql=mysql_query("INSERT into tbl_main
    (ars_no,phone_number,create_date,category_1,category_2,status,resolved_date,trouble_type_priority,ban_type,employee_id_name)
    VALUES " . implode (',',$values)) or die('Invalid query: ' . mysql_error());

使用PDO或Mysqli代替mysql_ *函数。