Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.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从循环插入数据库?_Php_Jquery_Mysql_Ajax - Fatal编程技术网

如何使用PHP从循环插入数据库?

如何使用PHP从循环插入数据库?,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,当我在没有数据的情况下插入时会出现问题。所以我希望你能帮我解决这个问题 这是我的档案: students.php <form id="student_form" method="POST" action=""> <?php if(mysql_num_rows($q)>0){ ?> <table border="0" dir="ltr" align="center

当我在没有数据的情况下插入时会出现问题。所以我希望你能帮我解决这个问题

这是我的档案: students.php

        <form id="student_form" method="POST"   action="">
<?php
    if(mysql_num_rows($q)>0){           

                ?>


    <table   border="0" dir="ltr" align="center"cellpadding='0' cellspacing='0' >

        <tr> <th>Student ID</th><th>Name</th><th>AVG</th><th>Joining Year</th><th>Message</th><th>Sending Message</th> </tr>

        <?php while($row = mysql_fetch_array($q)){ ?>
        <tr>

            <td id="stud_id[]"> <?php echo $row['studant_ID']; ?></td>
            <td> <?php echo $row['studant_Name']; ?></td>
            <td> <?php echo $row['Average']; ?></td>
            <td> <?php echo $row['year']; ?></td>
            <td> <input id="message1[]" name="message1[]" type="text" size="25px" /></td>
            <td><input name="submit[]" id="submit[]" type="submit"  value="Send"  /> </td>
        </tr>

<?php }}
我通过jquery和ajax连接两个文件


$(“#学生表格”)。在(“提交”,功能(活动){
event.preventDefault();
$.ajax({
类型:“POST”,
url:“insert_message.php”,
数据:$(this).serialize(),
成功:功能(数据){
$(“#inner_contant”).append(data+”
;//您可以在这里调用一些函数来读取数据库值并显示 }, }); });
首先-在
$\u POST
数组中传递到服务器的所有数据都是从具有
name
属性的输入字段中获取的(除非您有一些自定义js处理程序)

所以

下一步-您希望从这些按钮中得到什么:

<input name="submit[]" id="submit[]" type="submit" value="Send" />

当然,您应该删除
mysql\uuu
函数,并使用更新的API-
PDO
mysqli

从页面中删除表单

<tr>

            <td id="stud_id"> <?php echo $row['studant_ID']; ?>
              <input type="hidden" name="stud_id" value="<?php echo $row['studant_ID']; ?>"/>
            </td>
            <td> <?php echo $row['studant_Name']; ?></td>
            <td> <?php echo $row['Average']; ?></td>
            <td> <?php echo $row['year']; ?></td>
            <td> <input id="message1" name="message1" type="text" size="25px" /></td>
            <td><button class="submit" type="submit" />Send </button> </td>
        </tr>

$\u POST['message1']
是一个数组。和
$\u POST['stud\u id']
不存在。如何解决此问题?显示ajax请求的js。添加名称属性:按下按钮时仅在数据库中插入一条记录我想通过按下按钮向一名学生发送消息,以及将消息和学生id插入数据库。正如我已经说过的,为每一对创建一个单独的
表单
,或者创建一些js处理程序,一次只发送一对字段。如果你有时间,你能帮我做些什么吗?看起来一切都很好,但它没有进入插入页面,当我按下按钮时,数据库中的数据未更新?网络和控制台显示什么?我使用本地主机Wampserver和Firfox BrowserSubmit的父级
is
td
。你在那里找不到任何
输入
。我要感谢你的时间、努力和一切,我希望你成为最好的@madalin ivascu
<td id="stud_id[]"> <?php echo $row['studant_ID']; ?></td>
<td>
    <input type="hidden" name="stud_id[]" value="<?php echo $row['studant_ID']; ?>" />
</td>
<input name="submit[]" id="submit[]" type="submit" value="Send" />
foreach ($_POST['stud_id'] as $key => $id) {
     // find the message
     $msg = $_POST['message1'][$key];
     $query1 = "INSERT INTO `message` (`rcvrid`, `senderid`, `msg`)    VALUES ('$id', '22011111', '$msg'); ";
     mysql_query($query1);
}
<tr>

            <td id="stud_id"> <?php echo $row['studant_ID']; ?>
              <input type="hidden" name="stud_id" value="<?php echo $row['studant_ID']; ?>"/>
            </td>
            <td> <?php echo $row['studant_Name']; ?></td>
            <td> <?php echo $row['Average']; ?></td>
            <td> <?php echo $row['year']; ?></td>
            <td> <input id="message1" name="message1" type="text" size="25px" /></td>
            <td><button class="submit" type="submit" />Send </button> </td>
        </tr>
$(".submit").on("click", function(event) {
                event.preventDefault();
                $.ajax({
                    type: "POST",
                    url: "insert_message.php",
                    data: {stud_id:$(this).closest('tr').find('input[name="stud_id"]').val(),message1:$(this).closest('tr').find('input[name="message1"]').val()},
                    success: function(data) {
                        $("#inner_contant").append(data+"<br/>");//instead this line here you can call some function to read database values and display
                    },
                });
            });
echo json_encode(array('message'=>'Data updated/Error'));