AJAX / PHP - 创建SQL表的延迟时间

问题描述:

I have a simple HTML form:

var submitForm=function() {
    var i={};
    i[0]=$('#sname').val();
    var json=JSON.stringify(i);
    $.ajax({
      url: './form.php',
      type: 'POST',
      data: {data: json},
      success: function() {
        alert('Section Added.');
      }
    });
}
<input type="text" id="sname">
<button onClick="submitForm()">submit</button>

In PHP, I take this value and insert it into a database, such as:

    $data=json_decode($_POST['data'], true);
    $section=$data[0];
    include './sql/write-section.php';
    $sql="CREATE TABLE ".$section." (revdate varchar(250), title varchar(250), descrip varchar(255), permissions varchar(99999), file varchar(255))";
    $result = $conn->query($sql);
    $conn->close();

The issue is, that the table isn't being created. Initially, I thought it was an issue in my code, however if I were to replace line 2 in the PHP with just $section="test";, it works fine.

Additionally, if I add in some lines between the POST and SQL query, it sometimes works.

It is a very odd behavior and I am thinking there is some sort of async or lag issue between JS/PHP/SQL. Any ideas?

Thanks!

</div>

我有一个简单的HTML表单: p>

p>

  var submitForm = function(){
 
 var i = {}; 
 
 i [0] = $  ('#sname')。val(); 
 
 var json = JSON.stringify(i); 
 
 $ .ajax({
 
 url:'。/ form.php',
  
类型:'POST',
 
数据:{data:json},
 
成功:function(){
 
 alert('Section Added。'); 
 
} 
 
  
}); 
 
}  code>  pre> 
 
 
 &lt; input type =“text  “id =”sname“&gt; 
 
&lt; button onClick =”submitForm()“&gt;提交&lt; /按钮&gt;  code>  pre> 
 
  div> 
 
   div> 
 
 
 
 

在PHP中,我将此值并将其插入数据库,例如: p>

  $ data = json_decode($  _POST ['data'],true); 
 $ section = $ data [0]; 
包括'./sql/write-section.php'; 
  $ sql =“CREATE TABLE”。$ section。“(revdate varchar(250),title varchar(250),descrip varchar(255),permissions varchar(99999),file varchar(255))”; 
 $ result =  $ conn-&gt; query($ sql); 
 $ conn-&gt; close(); 
  code>  pre> 
 
 

问题是,表格不在 创建。 最初,我认为这是我的代码中的一个问题,但是如果我只用 $ section =“test”; code> code>替换PHP中的第2行,它工作正常 。 p>

此外,如果我在POST和SQL查询之间添加一些行,它有时 em>有效。 p>

它 是一个非常奇怪的行为,我认为JS / PHP / SQL之间存在某种异步或滞后问题。 有什么想法吗? p>

谢谢! p> div>

So after days of investigation and some annoyance, I found the issue, and it's really stupid.

The query I have doesn't allow for spaces (or at least in the SQL version I'm using I can't use spaces). So when trying I was trying a few different things like:

  • Test Section
  • 123
  • Test Section 1

Well, the reason why it worked once and never again is best for that one time it worked I put in "Test" for the table name.

I am re-structuring the script to allow spaces.