Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将来自循环的数据插入数据库(while)_Php_Loops_While Loop - Fatal编程技术网

Php 将来自循环的数据插入数据库(while)

Php 将来自循环的数据插入数据库(while),php,loops,while-loop,Php,Loops,While Loop,嗨,有这样的密码吗 for ($i=0; $i<=count($query)+1 ; $i++) { $sql ="SELECT * FROM $tabella[$i] WHERE id='1'"; $result=mysql_query($sql); $numrows=mysql_num_rows($result); while($row = mysql_fetch_array($result)) { $name=$row['name']; $surname=$row['surnam

嗨,有这样的密码吗

for ($i=0; $i<=count($query)+1 ; $i++)
 {
$sql ="SELECT * FROM $tabella[$i] WHERE id='1'";
$result=mysql_query($sql);
$numrows=mysql_num_rows($result);

while($row = mysql_fetch_array($result))
{
$name=$row['name'];
$surname=$row['surname'];

INSERT INTO student (name,surname) Values ($name,$surname)
} 

}
它只插入他找到的第一个表中的数据,因此只有一个名称和一个姓氏,而不是id=1的所有$name和$姓氏


如何解决此问题

以下是对脚本的编辑以及我发现的可能问题:

for ($i=0; $i<=count($query)+1 ; $i++)           //Why is there a +1?
 {
$sql ="SELECT * FROM $tabella[$i] WHERE id='1'"; //Your issue could be here, tabella[$1]
$result=mysql_query($sql);                       //may not be totally there
$numrows=mysql_num_rows($result);                //Try to echo your SELECT $sql query 
                                                 //to check

                                                 //Why do you have $numrows? I dont see
                                                 //that being used anywhere

while($row = mysql_fetch_array($result))
{
$name=$row['name'];
$surname=$row['surname'];

INSERT INTO student (name,surname) Values ($name,$surname) //Your INSERT statement 
                                                           //looks ok
                                                           //Try to use single/or double
                                                           //quotes around the values
                                                           //I like having `name`,  
                                                           //`surname` around fields
} 

}
查看评论。建议您使用准备好的/或PDO,但在大多数情况下 使用直接mysql函数的网站比较老,或者已经被编码过,所以没有
没有被重做的任何变化;这需要做很多工作。当你重新开始时,这是有意义的。希望能有帮助

ext/mysql已被弃用。考虑切换到一个更新的库。我认为你应该考虑使用而不是while循环。从$Tabel[$i ]替换$SQL= SELECT*,其中ID=“1”;对于$sql=SELECT*FROM$tabella[$i],其中id=$i;谢谢…我正在考虑…但是我找不到一种方法来选择所有表的所有字段。。我尝试了$sql=插入$tabella[$I]中的人员id、姓名、姓氏选择、姓名、姓氏;但它不起作用