当我运行下面提到的代码时,我得到错误:
Undefined offset:1 for $productSize = $product[1];//pack size.
我无法从数据库中获取数据。它返回null数组。
代码:
<?php
header('Content-type: application/json');
$con = mysql_connect('localhost', 'root', 'pass');
mysql_select_db('callingapp') or die('no connection');
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$currentDate = date("Y-m-d H:i:s");
/* Convert into sql time */
$currentDate = strtotime($currentDate);
$currentDateTime = time_php2sql($currentDate);
// Get data
$currentUserId = isset($_POST['id']) ? mysql_real_escape_string($_POST['id']) : ""; //TelecallerId
$callerId = isset($_POST['callerid']) ? mysql_real_escape_string($_POST['callerid']) : ""; //id of lead /student
$status = isset($_POST['status']) ? mysql_real_escape_string($_POST['status']) : ""; //status new call,follow up ,schedule
$notes = isset($_POST['notes']) ? mysql_real_escape_string($_POST['notes']) : ""; //comments
$callstatus = isset($_POST['callstatus']) ? mysql_real_escape_string($_POST['callstatus']) : ""; //answered/notanswered
$callstatus = strtolower($callstatus);
$callstatus = preg_replace('/\s+/', '', $callstatus);
$answered = isset($_POST['answered']) ? mysql_real_escape_string($_POST['answered']) : ""; //answer status if call status is answered
$not_answer = isset($_POST['not_answer']) ? mysql_real_escape_string($_POST['not_answer']) : ""; //notanswer status if call status is notanswered
$didnotlikestatus = isset($_POST['didnotlikestatus']) ? mysql_real_escape_string($_POST['didnotlikestatus']) : ""; //didnot like status if call status is answered and did not like
$datePost = isset($_POST['date']) ? mysql_real_escape_string($_POST['date']) : "";
/* Convert into sql time */
$dates = strtotime($datePost);
$date = time_php2sql($dates); //convert it into unix format
$datetimetotinsert = isset($_POST['dateTime']) ? mysql_real_escape_string($_POST['dateTime']) : ""; //Call later date and time
/* Convert into sql time */
$time = strtotime($datetimetotinsert);
$dateTime = time_php2sql($time); //convert it into unix format
$OrderDate = isset($_POST['todayDateTime']) ? mysql_real_escape_string($_POST['todayDateTime']) : ""; //order date
$productnameorder = isset($_POST['productnameorder']) ? mysql_real_escape_string($_POST['productnameorder']) : ""; //name of product ordered
print $productquantity = isset($_POST['productqty']) ? mysql_real_escape_string($_POST['productqty']) : ""; //Quantity of product ordered
$product_name = explode(",", $productnameorder); //explode product name and pack size
$finalArray = array();
$product_quantity = explode(",", $productquantity);
foreach ($product_name as $data) {
print_r($product_name);
$innerArray = array();
$product = explode(" ", $data);
$productName = $product[0]; //product name
$productSize = $product[1]; //pack size
//get product id,price from pm_productmaster_cstm and pp_productpricing_cstm respectively
$sqlProductDetails = "SELECT pm.id_c as id, pm.`status_c` AS pmStatus, pp.price_c, pp.`status_c` AS ppStatus,pp.id_c as pricingID
FROM `pm_productmaster_cstm` AS pm LEFT JOIN `pp_productpricing_cstm` AS pp ON pp.pm_productmaster_id_c = pm.id_c
WHERE pm.`productname_c` = '{$productName}' AND pm.`numdays_c` = '{$productSize}' AND pm.`status_c` = '1' AND pp.`status_c`= '1' ";
}
}
?>
答案 0 :(得分:2)
根据OP,print_r($product)
的输出给出:
Array (
[0] =>
)
因此导致的问题是因为数组索引超出范围。数组索引零基于。因此,获取数组第一个值的解决方案是使用:
$productSize = $product[0];