简体   繁体   English

将项目名称插入phpmyadmin表时出错

[英]Having error when inserting my item name to my phpmyadmin table

SO i want to insert my item id and together with my item name into a distribution_item,it only insert item id but not the name because of the array i use item[$i] whenever there is a item in the table row, so it get the data and insert. 所以我想将我的商品ID和商品名称一起插入distribution_item,它只插入商品ID,而不是商品名称,因为只要表行中有一个商品,我都会使用item [$ i]数组,因此获取数据并插入。

@$recipient = $_POST['recipient'];
@$address = $_POST['address'];
@$contact = $_POST['contact'];
@$date = $_POST['in_date'];
@$itemID = $_POST['id'];
@$remark = $_POST['remark'];
@$spec_remark = $_POST['spec_remark'];
$itemBalance = $_POST["count"];
$count = count($itemID);

// authentication to the database
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "hopeplace";

//Create connection
$Conndb = mysqli_connect($servername, $username, $password, $dbName);

// Check connection
if (!$Conndb) {
  die("Connection failed: " . $Conndb->connect_error);
}
else {


    // select database
    mysqli_select_db($Conndb, $dbName);


    $full_name = "SELECT * FROM recipient WHERE `FULL_NAME` = '$recipient'";
    $result = mysqli_query($Conndb, $full_name);
    $rec = mysqli_fetch_array($result);
    $recipient_id = $rec['HP_ID'];

    $item_id = "SELECT ITEM_NAME FROM inventory WHERE ITEM_ID = '$itemID'";
    $result2 = mysqli_query($Conndb, $item_id);
    $rec2 = mysqli_fetch_array($result2);
    $item_name  = $rec2["ITEM_NAME"];
    $string = implode(',',$item_id);

    $sql = "SHOW TABLE STATUS WHERE `Name` = 'distribution';";
    $result = mysqli_query($Conndb, $sql);
    $data = mysqli_fetch_assoc($result);

    $DISTRIBUTION_ID = $data['Auto_increment'];

    // Add into distribution table
        $sql = "INSERT INTO distribution(DISTRIBUTION_ID,HP_ID,FULL_NAME, ADDRESS, CONTACT, DISTRIBUTION_DATE, SPEC_REMARK) VALUES ('$DISTRIBUTION_ID','$recipient_id','$recipient', '$address', '$contact', '$date', '$spec_remark')";

    if (mysqli_query($Conndb, $sql)) {
        //Add item into distribution_item table
        $item_count = 0;
        for ($i=0; $i<$count; $i++){
            $sql = "INSERT INTO distribution_item (DISTRIBUTION_ID, ITEM_ID,ITEM_NAME,OUT_QUANTITY,REMARK) VALUES ('$DISTRIBUTION_ID', '$itemID[$i]','$string[$i]','$itemBalance[$i]', '$remark[$i]')";

            if (mysqli_query($Conndb, $sql)){

                $out = "UPDATE inventory set QUANTITY = QUANTITY - '$itemBalance[$i]' where ITEM_ID= '$itemID[$i]'";
                mysqli_query($Conndb, $out);
                //echo "<p>Item $itemID[$i] has been added to $DISTRIBUTION_ID</p>";
                $item_count++;
            } else {
                echo "Error: $sql <br />" . mysqli_error($Conndb);
            }
        }

        if ($item_count == $count){
            echo "<div>
            <script>
                    window.alert('Record added successfully!');

            </script>
            </div>";
        }
    } else {
        echo "Error: $sql <br />" . mysqli_error($Conndb);
    }



}
   mysqli_close($Conndb);

?> ?>

it pop out an error like this 它弹出这样的错误

Notice: Array to string conversion in C:\\xampp\\htdocs\\hopeplace\\distribution\\add_distribution.php on line 55 注意:第55行的C:\\ xampp \\ htdocs \\ hopeplace \\ distribution \\ add_distribution.php中的数组到字符串的转换

Warning: implode(): Invalid arguments passed in C:\\xampp\\htdocs\\hopeplace\\distribution\\add_distribution.php on line 56 警告:implode():第56行的C:\\ xampp \\ htdocs \\ hopeplace \\ distribution \\ add_distribution.php中传递的参数无效

it seems like i have to convert my array to string so i can pass the value into table. 似乎我必须将数组转换为字符串,以便可以将值传递到表中。 my database table is something like this 我的数据库表是这样的

DISTRIBUTION_ID | DISTRIBUTION_ID | ITEM_NAME | ITEM_NAME | ITEM_NAME| ITEM_NAME |

    1                       1                     APPLE
    1                       2                     ORANGE

The problem here is that $string[$i] cannot be found because no $string array exists, since $string = implode(', ',$item_id) can only work if $item_id is an array. 这里的问题是,由于不存在$string数组,因此找不到$string[$i] ,因为$string = implode(', ',$item_id)仅在$item_id是数组的情况下才能工作。

In the question, $item_id = "SELECT ITEM_NAME FROM inventory WHERE ITEM_ID = '$itemID'"; 在问题中, $item_id = "SELECT ITEM_NAME FROM inventory WHERE ITEM_ID = '$itemID'"; gives $item_id as a string. 给出$item_id作为字符串。 That is why the warning error indicates that the implode function cannot work. 因此,警告错误表示内爆功能无法正常工作。 The notice error shows that you cannot get an array element from the $string variable, and this is because that variable is not an array. 通知错误表明您无法从$string变量获取数组元素,这是因为该变量不是数组。

To get $string as an array, you would have to correct the following code: 要获得$string作为数组,您必须更正以下代码:

 $item_id = "SELECT ITEM_NAME FROM inventory WHERE ITEM_ID = '$itemID'";
    $result2 = mysqli_query($Conndb, $item_id);
    $rec2 = mysqli_fetch_array($result2);
    $item_name  = $rec2["ITEM_NAME"];
    $string = implode(',',$item_id);

For example, the above code could be changed to the following: 例如,上面的代码可以更改为以下代码:

$string = [];
$item_id = "SELECT ITEM_NAME FROM inventory WHERE ITEM_ID = '$itemID'";
$result2 = mysqli_query($Conndb, $item_id);
while($rec2 = mysqli_fetch_array($result2){
$string[] = $rec2["ITEM_NAME"];
}

Then in the sql query for inserting the item name you would first define the counting variable as $count = count($string) ; 然后在用于插入项目名称的sql查询中,您首先将计数变量定义为$count = count($string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM