简体   繁体   English

MySQL仅插入第一个字符

[英]MySQL inserts only first character

I have read other posts but my question is unique. 我读过其他帖子,但是我的问题很独特。

This in my php code 这在我的PHP代码

<?php
$link = mysqli_connect('localhost', 'root', '', 'mumbai');

$stmt = mysqli_prepare($link, "INSERT INTO single (`cus_id`, `mobile`, `date`, `credit`) 
                               VALUES (?,?,?,?)");

mysqli_stmt_bind_param($stmt, 'issi', $cus_id, $mobile, $date, $credit);

foreach ((array)$_POST['cus_id'] as $i => $cus_id) {
    $mobile = $_POST['mobile'][$i];
    $date = $_POST['date'][$i];                                         
    $credit  = $_POST['debuyt'][$i];

    mysqli_stmt_execute($stmt);
}                                           

if(!$stmt){ 
    echo "error". mysqli_error($link);
}
else{
    $_SESSION['s']="Payment successfully saved";
    header('location:final.php');
}
?>

Data types of my table single are as follows: 我的表中的数据类型single如下:

@cus_id -> int(11)

@mobile ->varchar(255)

@date->varchar(255)

@credit->int(255)

My problem is whenever I am submitting the form only one character is inserting in the database. 我的问题是,每当我提交表单时,数据库中只会插入一个字符。

My question has been solved. 我的问题已经解决。 Thanks goes to MCMXCII 感谢去MCMXCII

This is the corrected and solved answer. 这是纠正和解决的答案。

<?php
$link = mysqli_connect('localhost', 'root', '', 'mumbai');

$stmt = mysqli_prepare($link, "INSERT INTO single (`cus_id`, `mobile`, `date`, `credit`) 
                               VALUES (?,?,?,?)");

mysqli_stmt_bind_param($stmt, 'issi', $cus_id, $mobile, $date, $credit);

foreach ((array)$_POST['cus_id'] as $i => $cus_id) {
    $mobile = $_POST['mobile'];
    $date = $_POST['date'];                                         
    $credit  = $_POST['debuyt'];

    mysqli_stmt_execute($stmt);
}                                           

if(!$stmt){ 
    echo "error". mysqli_error($link);
}
else{
    $_SESSION['s']="Payment successfully saved";
    header('location:final.php');
}
?>

You're accessing the index of a string, which is a char. 您正在访问字符串的索引,即字符。 For example $var = 'something'; 例如$var = 'something'; $var[3] = 'e' . $var[3] = 'e' Drop the [$i] 删除[$i]

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

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