dongqing6661
2016-08-22 17:39
浏览 57
已采纳

SyntaxError:JSON.parse:尝试inser到mysql时JSON数据的第1行第1列的意外字符

I am using new to using AJAX and PHP I am trying to send a post request to a file called insertvalue.php. I already created the table and database. I am trying to grab the value from a jquery slider through ajax and then insert that value into the mysql table. After that, I want to return the results from the mysql table.

When I try running the script I get the error "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data". I checked, and I am not sure what I did wrong. Any assistance would be appreciated. Thanks!

db_connect.php

$connect = mysqli_connect("localhost", "private", "private", "private");
// fake credentials for posting

if (mysqli_connect_errno())
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$createTable = "
    CREATE TABLE IF NOT EXISTS ClassValues (
        question VARCHAR(40) NOT NULL,
        slider_value INT(50) NOT NULL
    )
";

 if ($connect->query($createTable) === TRUE) {
   //  echo "Table ClassValues created successfully";
 } else {
   echo "Error creating table: " . $connect->error;
 }

js file with ajax to send js to php

 $.ajax({
     url: 'php/insertvalue.php',
     data: { 'one': value }, // slider value 
     type: 'post',
     dataType: 'json',
     success: function(x) {
         alert(x.one);
     },
     error: function(request, status, error) {
         alert(error);
     }
 });

insertvalue.php

include 'db_connect.php';  // database connection

$one = $_POST['one'];
$array = array('one'=>$one);

echo json_encode($array);

$query = "INSERT INTO ClassValues (question, slider_value) VALUES('Question 1', $one)";
mysql_query($query);

$selection = "SELECT slider_value FROM ClassValues";
$result = $conn->query($selection);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo "slider_value" . $row["slider_value"] . "<br/>";
    }
} else {
    echo "0 results";
}
$conn->close();

图片转代码服务由CSDN问答提供 功能建议

我使用new来使用AJAX和PHP我试图将一个post请求发送到一个名为insertvalue.php的文件 。 我已经创建了表和数据库。 我试图通过ajax从jquery滑块中获取值,然后将该值插入到mysql表中。 之后,我想从mysql表返回结果。 </ p>

当我尝试运行脚本时,我收到错误“SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符”。 我查了一下,我不确定我做错了什么。 任何援助将不胜感激。 谢谢!</ p>

db_connect.php </ p>

  $ connect = mysqli_connect(“localhost”,“private”,“private”,“private”  ); 
 //发布的虚假凭据
 
if(mysqli_connect_errno())
 {
 echo“无法连接到MySQL:”。  mysqli_connect_error(); 
} 
 
 $ createTable =“
 CREATE TABLE IF NOT EXISTS ClassValues(
 question VARCHAR(40)NOT NULL,
 slider_value INT(50)NOT NULL 
)
”;  
 
 if($ connect-&gt; query($ createTable)=== TRUE){
 // echo“表ClassValues已成功创建”; 
} else {
 echo“错误创建表:”。  $ connect-&gt;错误; 
} 
 </ code> </ pre> 
 
 

带有ajax的js文件将js发送到php </ p>

   $ .ajax({
 url:'php / insertvalue.php',
 data:{'one':value},//滑块值
类型:'post',
 dataType:'json',\  n success:function(x){
 alert(x.one); 
},
 error:function(request,status,error){
 alert(error); 
} 
}); \  n </ code> </ pre> 
 
 

insertvalue.php </ p>

  include'db_connect.php';  //数据库连接
 
 $ one = $ _POST ['one']; 
 $ array = array('one'=&gt; $ one); 
 
echo json_encode($ array); 
 
  $ query =“INSERT INTO ClassValues(question,slider_value)VALUES('Question 1',$ one)”; 
mysql_query($ query); 
 
 $ selection =“SELECT slider_value FROM ClassValues”; 
 $ result =  $ conn-&gt;查询($ selection); 
 
if($ result-&gt; num_rows&gt; 0){
 while($ row = $ result-&gt; fetch_assoc()){
 echo“slider_value”  。  $ row [“slider_value”]。  “&lt; br /&gt;”; 
} 
}其他{
 echo“0 results”; 
} 
 $ conn-&gt; close(); 
 </ code> </ pre> \  n </ div>

1条回答 默认 最新

相关推荐 更多相似问题