For循环只发布数组的前3个元素 - PHP(For loop is only posting first 3 elements of array - PHP)

我有阵列,可能有无限量的元素。 我试图将它们输入到我的数据库中,但它只输入数组的前3个元素。 我的阵列看起来像这样

array(4) {

["ID"]=>

array(5) {

[0]=>

string(1) "1"

[1]=>

string(1) "2"

[2]=>

string(1) "3"

[3]=>

string(1) "4"

[4]=>

string(1) "5"

}

["firstname"]=>

array(5) {

[0]=>

string(5) "Steve"

[1]=>

string(3) "Dan"

[2]=>

string(3) "Jim"

[3]=>

string(4) "Adam"

[4]=>

string(5) "James"

}

["surname"]=>

array(5) {

[0]=>

string(5) "Smith"

[1]=>

string(6) "Colins"

[2]=>

string(6) "Knight"

[3]=>

string(5) "Lamar"

[4]=>

string(4) "Rays"

}

["submit"]=>

string(5) "Enter"

}

这是我的for循环和sql语句

$a[0] = $_SESSION['ID'];

$a[1] = $_SESSION['firstname'];

$a[2] = $_SESSION['surname'];

for ($i = 0; $i

$id = $_SESSION['ID'][$i];

$fname = $_SESSION['firstname'][$i];

$sname = $_SESSION['surname'][$i];

$query = "INSERT INTO orders(id, firstname, surname) VALUES('$id', '$fname', '$sname')";

if (mysqli_query($connection, $query)) {

echo "New record created successfully";

} else {

echo "Error: " . $query . "
" . mysqli_error($connection);

}

}

这只会发布每个数组的0,1,2个元素。 如何更改它以便将所有元素插入到我的数据库中?

I have arrays which can potentially have unlimited amount of elements in them. I am trying to input them into my db but it only inputs the first 3 elements of the arrays. My arrays looks like this

array(4) {

["ID"]=>

array(5) {

[0]=>

string(1) "1"

[1]=>

string(1) "2"

[2]=>

string(1) "3"

[3]=>

string(1) "4"

[4]=>

string(1) "5"

}

["firstname"]=>

array(5) {

[0]=>

string(5) "Steve"

[1]=>

string(3) "Dan"

[2]=>

string(3) "Jim"

[3]=>

string(4) "Adam"

[4]=>

string(5) "James"

}

["surname"]=>

array(5) {

[0]=>

string(5) "Smith"

[1]=>

string(6) "Colins"

[2]=>

string(6) "Knight"

[3]=>

string(5) "Lamar"

[4]=>

string(4) "Rays"

}

["submit"]=>

string(5) "Enter"

}

this is my for loop and sql statement

$a[0] = $_SESSION['ID'];

$a[1] = $_SESSION['firstname'];

$a[2] = $_SESSION['surname'];

for ($i = 0; $i

$id = $_SESSION['ID'][$i];

$fname = $_SESSION['firstname'][$i];

$sname = $_SESSION['surname'][$i];

$query = "INSERT INTO orders(id, firstname, surname) VALUES('$id', '$fname', '$sname')";

if (mysqli_query($connection, $query)) {

echo "New record created successfully";

} else {

echo "Error: " . $query . "
" . mysqli_error($connection);

}

}

this will only post the 0,1,2 elements of each array. How can I change it so it insert all elements to my db?

原文:https://stackoverflow.com/questions/35096190

更新时间:2019-11-23 08:48

相关问答

注意:您的代码输出array()主要原因是您在发送/处理异步(AJAX)请求之前重定向客户端 基本上移动window.location = "AddtoDatabase.php"; 到成功回调,就像下面提到的那样。 第一个问题:您应该使用对象字面值(〜= assoc array in php),而不是使用数组。 为此,请更改此位: var dataArray = new Array(7);//<== NEVER do this again, btw

dataArray[0]= "routeID:"

...

你可以做的最好的事情就是把最后的调用放在while循环之外,并改变你的while循环逻辑,以便它尽早退出。 代替: while ($post = getPost()) {

printPost($post);

}

做点什么 $last = getPost();

$post = getPost();

while ($post != null) {

printPost($last);

$last = $post;

$post = getPost();

}

printSp

...

你需要做的是 删除提交按钮并添加 第二个在页面中创建一个名为

同 $("#submit").on('.submit','click',function(e) {

e.preventDefault();

注意:您的提交也可以工作,但您必须使用jquery停止其默认

...

for ($i = 0; $i

此循环将进行三次,因为$a数组有3个项目(ID,名字,姓氏)。 将count($a)更改为count($a[0])并且它应该有效。 虽然这不是最好的解决方案(如果这三个阵列中的一个具有比其他阵列更多/更少的项目怎么办?...) for ($i = 0; $i

This cycle will go three times because $a array has 3 items (ID, firs

...

foreach在数组副本上工作,而不是原始数组(在特定条件下)。 这就是为什么你没有看到循环中反映出来的变化。 循环引用时,您将获得预期的输出: foreach ($j as &$i)

{

// ...

}

输出: 1

2

3

5

5

5

foreach works on a copy of the array, not the original array (under certain conditions). That's why you're not seeing the chan

...

人们说你需要使用count()来限制你的循环。 但是,如果你使用count()那么目的是什么? 因为你的问题说你想在不使用count()情况下查看计数。 你必须相当长时间地提高限制并让循环继续,直到你的数组中没有更多的元素然后你可以打破循环 $count = 0;

for ($i = 0; $i < 1000000; $i++) {

if(isset($temp[$i]))

$count++;

else

break;

}

print_r(

...

你需要在循环后进行内implode 。 否则你每次都会附加到odg_list 。 for ($i=1;$i<6;$i++) {

$odg_ids[] = $odg[$i];

}

$odg_list = implode("','", $odg_ids[$i]);

我不知道你从哪里得到6 ,但你也可以使用array_slice来获得第1到第6个元素(你省略了第0个元素)。 You need to do the implode after the loop. Otherwise you kee

...

http://php.net/manual/en/function.array-fill.php <?php

var_dump( array_fill( 0, 5, 1 ) );

?>

http://php.net/manual/en/function.array-fill.php <?php

var_dump( array_fill( 0, 5, 1 ) );

?>

问题在于这个电话: .val(jsonLab)

jsonLab是JS中保存的对象数组。 因此,将其设置为jQuery对象的val()将意味着在其上调用toString() 。 结果是[object Object] 。 这是发送到您的PHP逻辑的,因此错误。 要解决此问题,您需要在将JSON设置为文本字段的值时手动对其进行字符串化: $('').val(JSON.stringify(jsonLab)).appendTo('

...

php 3个循环,For循环只发布数组的前3个元素 - PHP(For loop is only posting first 3 elements of array - PHP)...相关推荐

  1. 问题:数组的循环左移。要求设计三种算法,将数组中的N个元素能够实现循环左移p个位置。

    问题重述:数组的循环左移.要求设计三种算法,将数组中的N个元素能够实现循环左移p个位置. 算法1: 先将数组中的前p个元素存放在一个临时数组中,再将余下的N-p个元素左移p个位置,最后将前p个元素从临 ...

  2. php if foreach个数记录,php foreach循环只返回数组中第一次迭代的值

    我似乎无法确定为什么我的foreach循环能够循环所有5个生成的ProductionOrderID,但只返回第一个ID的数据. 我的理解是数组正确循环,因为你可以在这里看到当前结果:https://i ...

  3. Swift for循环:用于索引,数组中的元素?

    本文翻译自:Swift for loop: for index, element in array? Is there a function that I can use to iterate ove ...

  4. 在循环递增一次的数组中插入元素

    文章目录 题目 思路 如何建立左右区间? 如何查找最高点? 那我们怎么判断 `num` 到底处于什么样的位置呢? 如何确定插入位置? 插入元素 代码 题目 给一个只循环递增一次的数组 res,res ...

  5. 二、Vue基础语法学习笔记——事件监听v-on、条件判断(v-if、v-else-if、v-else、v-show)、循环遍历(v-for遍历数组对象,key属性、检测数组更新)、图书案例、双向绑定

    四.事件监听 在前端开发中,我们需要经常和用于交互. 这个时候,我们就必须监听用户发生的时间,比如点击.拖拽.键盘事件等等 在Vue中如何监听事件呢?使用v-on指令 v-on介绍 作用:绑定事件监听 ...

  6. php函数内的循环,PHP 循环列出目录内容的函数代码

    PHP 循环列出目录内容的函数代码 复制代码 代码如下: function list_files($dir) { if(is_dir($dir)) { if($handle = opendir($di ...

  7. javascripts-for循环-while循环-标签的选择与操作

    小结 document对象,根据id获取标签对象 document.getElementById(ID值)document对象,根据标签获取对象们 document.getElementsByTagN ...

  8. pythonwhile循环love_while循环简介

    ## 使用 while 循环 你可以使用while循环来数数,例如,下面的while循环从1数到5: ``` counting.py current_number = 1 while current_ ...

  9. 任意重循环(循环阶数不定、循环层数不定)

    ----哆啦刘小洋 原创,转载需说明出处 文章目录 1 简介 2 普通循环 3 任意重循环算法 4 任意重循环代码 4.1 准备 4.2 第2节代码重写 5 模块化 5.1 AnyLoop1 5.2 ...

最新文章

  1. sql中varchar(n),nvarchar(n) 长度性能及所占空间分析
  2. 大学毕业标语计算机学院,大学毕业横幅标语创意
  3. 机器学习理论《统计学习方法》学习笔记:奇异值分解(SVD)
  4. 将DataFrame数据如何写入到Hive表中
  5. Service-Oriented Architecture,SOA(转)
  6. 测试AS3的性能9800粒子
  7. 【SQL】实验二 SQL数据查询
  8. 新浪微博表情jQuery插件 v1.2——方便地实现表情插入功能
  9. 2017.08.18【NOIP提高组】模拟赛B组 沙耶的玩偶(doll)
  10. linux so 加壳,[android] 从加壳的so文件中抽出symbols
  11. Python 人工智能入门须知
  12. Node.js:时间处理库:moment.js用法
  13. 陌生人的善意 amp;amp;amp;amp; 绝对不要因为陌生人而生气
  14. 利用matlab实现三体问题(双星、3星、多星运动)
  15. 计算机应用基础 第2版 在线作业 1答案,天津大学《计算机应用基础》在线作业一二【标准答案】...
  16. asp.net正则表达式的使用
  17. vs2017控制台应用程序调用DLL
  18. 河池学院计算机宿舍,【2021】河池学院学校食堂环境与寝室宿舍介绍_优优招生网...
  19. 数组——洛谷#P1046 陶陶摘苹果(Python实现)
  20. python final 类 和 方法

热门文章

  1. python udp client
  2. yolt 卫星图像进行快速目标识别的新方法
  3. win nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are depr
  4. Ubuntu 16.04 安装 miniconda
  5. 高斯混合模型--GMM(Gaussian Mixture Model)
  6. java访问map_java.map使用
  7. java的销毁方法_销毁Spring Bean的三种方法
  8. java的total_Java LabelResourcePool.totalNum方法代码示例
  9. 二级计算机为让利消费者,计算机二级office题库训练题(2)
  10. mysql piress_由MySql漏洞导致电脑被入侵(特征为新增加名为piress的帐户)所想到的...