使用jquery sql插入多行并且php不起作用

Posted

技术标签:

【中文标题】使用jquery sql插入多行并且php不起作用【英文标题】:Insert multiple rows using jquery sql and php is not working 【发布时间】:2021-04-16 22:06:34 【问题描述】:

我正在使用以下脚本尝试将新行插入我的数据库。由于某种原因,只插入了第一行,就像没有发布其他行一样。我找不到问题所在,我没有收到错误日志,也看不到任何拼写错误或差异。

有人知道我做错了什么吗?

php 和 SQL:

if(isset($_POST['submitnewacc']))

    for($i=0;$i<count($_POST['slno']);$i++)
    
        $name = $_POST['name'][$i];
        $email = $_POST['accmail'][$i];
        $password = $_POST['password'][$i];
        $bullets = $_POST['bullets'][$i];
        $notes = $_POST['notes'][$i];
        $rang = $_POST['rang'][$i];
        $betaald = $_POST['betaald'][$i];
        $paydate = $_POST['paydate'][$i];
        
        if ( trim($name) != '')  $nameacc = trim($name); 
        if ( trim($email) != '')  $emailacc = trim($email); 
        if ( trim($password) != '')  $passwordacc = trim($password); 
        if($betaald =='')  $betaald = 'No'; 
        
        if($nameacc != '' && $emailacc != '' && $passwordacc != '' && $bullets >= '0')
        

            $stmt16 = $mysqli->prepare("
            INSERT INTO account (name,email,password,bullets,rang,betaald,testpay,door,group_name,notes)
                                VALUES (trim(?),trim(?),trim(?),trim(?),trim(?),trim(?),trim(?),trim(?),trim(?),trim(?))");
            $stmt16->bind_param('ssssssssss',$nameacc,$emailacc,$passwordacc,$bullets,$rang,$betaald,$paydate,$usernamesession,$groupname,$notes);  
            $stmt16->execute();
            $stmt16->close();
     
            if($stmt16 == TRUE)
            
                $log_page = "Newaccount";
                $log_account = filter_var($name, FILTER_SANITIZE_STRING);
                $datum = filter_var(date ("Y-m-d H:i:s"), FILTER_SANITIZE_STRING); 
                $message = filter_var($usernamesession." added a new account: ".$log_account." (rang: ".$rang.", bullets: ".$bullets.", 
    , paying: ".$betaald.", notes: ".$notes.")", FILTER_SANITIZE_STRING);
                $userip = htmlspecialchars(filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_STRING)); 

                $stmt12 = $mysqli->prepare("INSERT INTO logs (user_ID, log_action, log_time, log_ip, log_account, log_page, 
    log_owner, log_group) VALUES (trim(?),trim(?),trim(?),trim(?),trim(?),trim(?),trim(?),trim(?))");
                $stmt12->bind_param('isssssss',$userid,$message,$datum,$userip,$log_account,$log_page,$usernamesession,$groupname);  
                $stmt12->execute();
                $stmt12->close();
    
                $_SESSION['addacc'] = '<p><b><font color="orange">You have succesfully added the accounts to the database!</p>
    </font></b></ br>' ;
    
                header('Location: ' . 'lijst_myaccounts.php');
                exit(); 
            

            //echo '<div class="alert alert-success" role="alert">Submitted Successfully</div>';
        
        else
        
            echo '<div class="alert alert-danger" role="alert">Error Submitting in Data</div>';
        
    

表格

<table class="userprof" align='left' >
    <tr>
    <td class="footer">Options: </td>
    <td class="footer" >
    <button type="button" name="addrow" id="addrow" class="example_e">Add New Row</button>
    </td>
    </tr> </table><br /><br />


<br/><br/><br/>
<script src="java/sorttable.js"></script>
<script src="js/jquery.min.js"></script> 
<script src="js/bootstrap.min.js"></script>


<form class="form-horizontal"action="<?php htmlspecialchars($filterpage); ?>" method="post">
  <div class="divTable blueTable" style="float:left !important;">
    <div class="divTableHeading">
      <div class="divTableRow">
        <div class="divTableHead" style="width:0px !important;"></div>
        <div class="divTableHead">Account Name</div>
        <div class="divTableHead">Emailadress</div>
        <div class="divTableHead">Password</div>
        <div class="divTableHead">Rang</div>
        <div class="divTableHead">Bullets</div>
        <div class="divTableHead">Notes</div>
        <div class="divTableHead">Paying</div>
        <div class="divTableHead">Paying untill</div>
        <div class="divTableHead" style="width:2px !important;"></div>
      </div>
    </div>
    <div class="divTableBody">
      <div class="divTableRow">
        <div class="divTableCell"><input type="hidden" style="width:2px !important;" class="form-control sl" 
        name="slno[]" id="slno" value="1" readonly=""></div>
        
        <div class="divTableCell"><input type="text" style="width:190px !important;font-weight: normal !important;"
         class="form-control" name="name[]" id="acc_name" placeholder="Enter Account Name" required minlength="3" maxlength="150"></div>
         
         <div class="divTableCell"><input type="email" style="width:210px !important;font-weight: normal !important;"
         class="form-control" name="accmail[]" id="acc_email" placeholder="mail@mail.nl" required minlength="3" maxlength="150"
         ></div>
         
         <div class="divTableCell"><input type="text" style="width:130px !important;font-weight: normal !important;"
         class="form-control" name="password[]" id="acc_pass" placeholder="Enter Password" required minlength="3" maxlength="50"></div>
         
        <div class="divTableCell">
          <select name="rang[]" id="rang_acc" style="width:80px !important;font-weight: normal !important;" class="form-control">
            <option value="Bacteria">Bacteria</option>
            <option value="Low Life">Low Life</option>
            <option value="Apprentice">Apprentice</option>
            
          </select>
        </div>
        <div class="divTableCell"><input type="number" style="width:60px !important; font-weight: normal !important;" 
        required pattern="[0-9]+" value="0" onClick="this.select()" name="bullets[]" id="acc_bullets" class="form-control"></div>
        
        <div class="divTableCell"><input type="text" name="notes[]" id="acc_notes" maxlength="250" 
        style="width:210px !important; font-weight: normal !important;" placeholder="OPTIONAL enter notes" class="form-control"/></div>
        
        <div class="divTableCell">
        <label class="switch">
        
        <input type="checkbox" name="betaald[]" id="acc_betaald"  value="Yes" checked/>
        <div class="slider round">
        <span class="on">Yes</span>
        <span class="off">No</span>
        </div>
        </label></div>
        
        <div class="divTableCell"><input type="text" name="paydate[]" value="2020-01-01" 
        style="width:100px !important; font-weight: normal !important; color:#F00" 
        id="acc_paydate" required pattern="[0-9]4-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])"  
    class="form-control" /></div>
        <div class="divTableCell"></div>
       
      </div>
      
    </div>
  </div>
  <br/>

  <button type="submit" name="submitnewacc" class="example_e">Add account(s) to database!</button>
</form>

脚本

<script type="text/javascript">
const tableRow = (i) => return `
    <div class="divTableRow">
      <div class="divTableCell" >
        <input type="hidden" class="form-control sl" style="width:2px !important;" name="slno[]" value="$ i " readonly="">
      </div>
      <div class="divTableCell">
        <input type="text" style="width:190px !important;font-weight: normal !important;" 
        class="form-control" name="name[]" id="acc_name$ i " placeholder="Enter Account Name" 
        required minlength="3" maxlength="150" >
      </div>
      <div class="divTableCell"><input type="email" style="width:210px !important;font-weight: normal !important;"
         class="form-control" name="accmail[]" id="acc_email$ i " placeholder="mail@mail.nl" required minlength="3" 
         maxlength="350" ></div>
         
         <div class="divTableCell"><input type="text" style="width:130px !important;font-weight: normal !important;"
         class="form-control" name="password[]" id="acc_pass$ i " placeholder="Enter Password" required minlength="3" maxlength="150"
       ></div>
         
      <div class="divTableCell">
        <select name="rang[]" id="rang_acc$ i " style="width:80px !important;font-weight: normal !important;"
         class="form-control">
          <option value="Bacteria">Bacteria</option>
          <option value="Low Life">Low Life</option>
          <option value="Apprentice">Apprentice</option>
      
        </select>
      </div>
      
      <div class="divTableCell"><input type="number" style="width:60px !important; font-weight: normal !important;" 
        required pattern="[0-9]+" value="0" onClick="this.select()" name="bullets[]" id="acc_bullets$ i " class="form-control"></div>
        <div class="divTableCell"><input type="text" name="notes[]" id="acc_notes$ i " maxlength="250" 
        style="width:210px !important; font-weight: normal !important;" placeholder="OPTIONAL enter notes" class="form-control"/></div>
        
      <div class="divTableCell">
        <label class="switch">
        
        <input type="checkbox" name="betaald[]" id="acc_betaald$ i "  value="Yes" checked/>
        <div class="slider round">
        <span class="on">Yes</span>
        <span class="off">No</span>
        </div>
        </label></div>
      
      <div class="divTableCell"><input type="text" name="paydate[]" id="acc_paydate$ i " value="2020-01-01"
         style="width:100px !important; font-weight: normal !important; color:#F00"
         required pattern="[0-9]4-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" class="form-control" /></div>
    
      <div class="divTableCell">
        <input type="button" class="btnRemove example_b" value="Delete"/>
      </div>
    </div>
  `


$('#addrow').click(function() 
  var length = $('.sl').length;
  var i = parseInt(length) + parseInt(1);
  var newrow = $('.divTableBody').append(tableRow(i));
);

// Removing event here
$('body').on('click', '.btnRemove', function() 
  $(this).closest('div').parent().remove()
);
    </script>

【问题讨论】:

" 就像其他行没有发布"...您需要再调试一下,可以肯定的是。使用浏览器的开发者工具查看实际发布的。并在你的 PHP 中添加一些日志以查看 $_POST 数据是什么样的,代码采用什么路径,实际执行了多少插入查询,是否有任何失败,等等。 只是为了清楚。我不是专业的开发人员。我是新手,正在尝试边做边学。我以检查控制台而闻名,但我不知道在哪里可以找到发布的内容。发布的行是脚本不适应创建更多行的行。 这些说明适用于 Chrome,但大多数其他浏览器的工具类似:加载您的表单。打开开发者工具。转到网络选项卡。确保勾选“保留日志”框。然后添加一些行,填写一些数据,然后提交表单。您应该会看到对 .php 脚本的 POST 请求。点击请求。在出现的信息的“标题”选项卡中,向右滚动到底部,您应该会看到“表单数据”部分。进去看看。 在 PHP 端,您可以使用var_dump($_POST); 使 PHP 输出它接收到的数据的视图(它应该与浏览器显示的数据相同已提交)。 谢谢。照你说的做了。两者都显示了我发布的实际帖子..现在该怎么办.. 【参考方案1】:

这段代码:

header('Location: ' . 'lijst_myaccounts.php');
exit(); 

需要在您的for 循环结束后移至。否则它将在循环的第一次迭代期间退出脚本。

【讨论】:

我删除了 exit();。现在它起作用了!谢谢,没想到! 不,不要删除它,这是一个错误。按照我的建议移动它。 像这样移动它。 ` else echo '错误提交数据'; header('位置:' . 'lijst_myaccounts.php');出口(); ` 我明白了。是的,这就是我想要你做的。

以上是关于使用jquery sql插入多行并且php不起作用的主要内容,如果未能解决你的问题,请参考以下文章

ajax调用后使用jQuery插入记录不起作用

PHP- MySQL 数据库插入操作不起作用 [重复]

从php执行时mssql批量插入不起作用

使用jQuery $ .ajax方法显示包含MySQL表数据的JSON [关闭]

使用控制器中的 prepareStatement 向表中插入多行失败

将用户输入从文本框插入到数据库(使用 mysql 的 PHP 到 PHPMYADMIN)代码由于某种原因不起作用