douba3378 2017-03-22 05:43
浏览 15

动态查询查询值并按下按钮单击PHP

I have a page where I have two columns one is Attribute name and Attribute Value. I have a button named Add Attribute Value which adds text boxes to the screen so that an Attribute Name can have multiple Attribute values. I have added the text boxes with a variable i in javascript.

I need to use the "i" variable and also access the text in the textbox using the name "'mytext'+i".

The code written is below:

<html>
<head><title>Add Attributes</title></head>
<body>
<script language="javascript">
var i = 1;
function changeIt()
{

my_div.innerHTML = my_div.innerHTML +"<br>&emsp;&emsp;&emsp; &emsp;&emsp;&emsp;&emsp; &emsp;&emsp;&emsp;&nbsp;&nbsp;<input type='text' name='mytext'+ i><br>"
i++;
}
</script>
<form action= "" method = "POST">
<h1 align = "center"><u>Attribute Management</u></h1>

 &nbsp;&nbsp;&nbsp;&nbsp;
 Attribute Name Attribute Value <br>
 <br><input type="text" name="attname" > 
     <input type="text" name="attvalue">
     <input type="button" value="Add Attribute Value" onClick="changeIt()">
<div id="my_div"></div>

 <b> </b><br>
          <br><br><input type="submit" name = "submit" value = "Add Attribute" > &nbsp; 
          <input type="submit" name = "submit1" value = "User Application" >
</form>
</body>
</html>

<?php

if(isset($_POST['submit'])){
    $att_name = $_POST['attname'];

    $query_string1 = "ALTER TABLE users ADD $att_name varchar(20)";
    $query_string2 = "ALTER TABLE attributes ADD $att_name varchar(20)";
    $part_string = "(";
    for($x = 0;$x<= i;$x++){
        $att_value= $_POST['mytext'.$x];
        if($x = 0){
        $part_string = $part_string.$att_value;
        }
        else{
        $part_string = ",".$part_string.$att_value;
        }
    }
    $part_string= $part_string.")";
    $query_string3 = "INSERT INTO attributes ('$att_name') VALUES '$part_string'";
    $connect = mysqli_connect("localhost","root", "","nets") or die("Couldn't connect to database");        
    /*$query1 = mysqli_query($connect,$query_string1);
    $query2 = mysqli_query($connect,$query_string2);
    $query3 = mysqli_query($connect,$query_string3);*/
}
if(isset($_POST['submit1'])){
header('Location: RegisteredUsers.php');
}
?>

Get an error of "i" is not defined and "Undefined index: mytext0". I have to collect all the attribute values and fire a query with all the values from the text box together.

Please suggest.

  • 写回答

1条回答 默认 最新

  • doulin6761 2017-03-22 06:33
    关注

    The error of "Undefined index: mytext0" is come due to no such input field is defined, because you have initialised the i with 1 so, just make it 0.

    And also you have a issue in the html, which you are adding, just change your script block with following :

    var i = 1;
    function changeIt()
    {
    
    my_div.innerHTML = my_div.innerHTML +"<br>&emsp;&emsp;&emsp; &emsp;&emsp;&emsp;&emsp; &emsp;&emsp;&emsp;&nbsp;&nbsp;<input type='text' name='mytext"+ i+"'><br>"
    i++;
    }
    <html>
    <head><title>Add Attributes</title></head>
    <body>
    
    <form action= "" method = "POST">The error of "Undefined index: mytext0" is come due to no such input field is defined, because you have initialised the i with 1 so, just make it 0. 
    
    And also you have a issue in the html, which you are adding, just change your script block with following :
    
    <h1 align = "center"><u>Attribute Management</u></h1>
    
     &nbsp;&nbsp;&nbsp;&nbsp;
     Attribute Name Attribute Value <br>
     <br><input type="text" name="attname" > 
         <input type="text" name="attvalue">
         <input type="button" value="Add Attribute Value" onClick="changeIt()">
    <div id="my_div"></div>
    
     <b> </b><br>
              <br><br><input type="submit" name = "submit" value = "Add Attribute" > &nbsp; 
              <input type="submit" name = "submit1" value = "User Application" >
    </form>
    </body>
    </html>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集