douqian2524 2013-05-06 13:13
浏览 22
已采纳

如何将不同数组变量中的数据添加到php中的sql表中

I have been trying to insert data into a table(mySql) from 3 arrays using php. Each time i run the script i get a long list on unsuccesful entries. I am new to php and sql, what am i doing wrong here ? how do i get the query string to recognize these variables ? i have tried searching around but could not make sense of much.

$id=array();
$comp=array();
$mobname=array();

for($x=0;$x<$arrlength;$x++)
{

  if(mysqli_query($con,"INSERT INTO Umobile VALUES ($id[$x],$comp[$x],$mobname[$x])"))
     echo "added",$id[$x]," ",$comp[$x]," ",$mobname[$x];
  else
     echo " unsuccessful ";

    //echo $id[$x]," ",$comp[$x]," ",$mobname[$x];
   echo "<br/>";  
 }
  • 写回答

1条回答 默认 最新

  • dqpkea9486 2013-05-06 13:28
    关注

    By using this kind of arrays, you assume that your three arrays have the same lengths: $arrlength.

    If this statement is true, you could have special characters into your arrays and the query may fail.

    You must see at mysqli prepare to escape your values before the insert.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效