php 3个循环,For循环只发布数组的前3个元素 - PHP(For loop is only posting first 3 elements of array - PHP)...
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)...相关推荐
- 问题:数组的循环左移。要求设计三种算法,将数组中的N个元素能够实现循环左移p个位置。
问题重述:数组的循环左移.要求设计三种算法,将数组中的N个元素能够实现循环左移p个位置. 算法1: 先将数组中的前p个元素存放在一个临时数组中,再将余下的N-p个元素左移p个位置,最后将前p个元素从临 ...
- php if foreach个数记录,php foreach循环只返回数组中第一次迭代的值
我似乎无法确定为什么我的foreach循环能够循环所有5个生成的ProductionOrderID,但只返回第一个ID的数据. 我的理解是数组正确循环,因为你可以在这里看到当前结果:https://i ...
- Swift for循环:用于索引,数组中的元素?
本文翻译自:Swift for loop: for index, element in array? Is there a function that I can use to iterate ove ...
- 在循环递增一次的数组中插入元素
文章目录 题目 思路 如何建立左右区间? 如何查找最高点? 那我们怎么判断 `num` 到底处于什么样的位置呢? 如何确定插入位置? 插入元素 代码 题目 给一个只循环递增一次的数组 res,res ...
- 二、Vue基础语法学习笔记——事件监听v-on、条件判断(v-if、v-else-if、v-else、v-show)、循环遍历(v-for遍历数组对象,key属性、检测数组更新)、图书案例、双向绑定
四.事件监听 在前端开发中,我们需要经常和用于交互. 这个时候,我们就必须监听用户发生的时间,比如点击.拖拽.键盘事件等等 在Vue中如何监听事件呢?使用v-on指令 v-on介绍 作用:绑定事件监听 ...
- php函数内的循环,PHP 循环列出目录内容的函数代码
PHP 循环列出目录内容的函数代码 复制代码 代码如下: function list_files($dir) { if(is_dir($dir)) { if($handle = opendir($di ...
- javascripts-for循环-while循环-标签的选择与操作
小结 document对象,根据id获取标签对象 document.getElementById(ID值)document对象,根据标签获取对象们 document.getElementsByTagN ...
- pythonwhile循环love_while循环简介
## 使用 while 循环 你可以使用while循环来数数,例如,下面的while循环从1数到5: ``` counting.py current_number = 1 while current_ ...
- 任意重循环(循环阶数不定、循环层数不定)
----哆啦刘小洋 原创,转载需说明出处 文章目录 1 简介 2 普通循环 3 任意重循环算法 4 任意重循环代码 4.1 准备 4.2 第2节代码重写 5 模块化 5.1 AnyLoop1 5.2 ...
最新文章
- 如何根据字典中值的大小,对字典中的项排序
- kinect数据读取
- Java 8的方法参考进一步限制了重载
- 一个死去的网站shige.laiyo.com
- 在banana pi 上测试 《S2A》Snap/Scratch to Arduino
- idea搭建springcloud项目_Eureka搭建分布式SpringCloud项目
- ssh java_java中的SSH的各自作用是什么-百度经验
- 百度文库解决复制问题
- web前端笔试题——JS
- iPhone 12 Pro测量人的身高
- Windows 性能监视器API
- PostgreSQL下载和安装教程
- Maven报:Unable to import maven project: See logs for details
- 实验楼linux命令,GitHub - encorechow/linux-command: Linux命令大全搜索工具,内容包含Linux命令手册、详解、学习、搜集。...
- Python海龟画图 画一个爱心 赶快给女朋友来一个
- 彻底搞懂内存屏障(上)
- redhat 7 手册
- Google Earth Engine(GEE)——S2影像异常值
- js添加多marker 高德地图_h5高德地图javascript调起app导航定位多个marker
- set_、set_allocated_、mutable_、add_的使用
热门文章
- 用openCV去除文字中乱入的线条
- 用Java2D画出树的结构图
- 通用c/c++的Makefile模版
- 2016视觉目标跟踪总结
- jdbc获取mysql 列信息_JDBC获取数据库信息:获取表中各列的信息
- 比特币交易平台 php,比特币PHP离线交易开发包
- parquet to mysql_在hive中使用parquet (CDH4.3)
- 清除浏览器缓存之后为什么还是显示旧的html页面_H5缓存机制浅析-移动端Web加载性能优化...
- mysql limit优化_MySQL:教你学会如何做性能分析与查询优化
- MySQL-BETWEEN AND范围查询问题