简体   繁体   English

mysql_query不向我的数据库中插入任何内容

[英]mysql_query not inserting anything into my database

I have this bit of code here: 我在这里有这段代码:

<?php
error_reporting(E_ALL);
include 'DB.php';

$con = mysql_connect($host,$user,$pass)
    or die("Error: ".mysql_error());
$dbs = mysql_select_db($databaseName, $con);

$name = $_POST['name'];
$date = date('Y-m-d');
$amount = $_POST['amount'];
$timPaid = $_POST['timPaid'];
$rennyPaid = $_POST['rennyPaid'];

$sql = "INSERT INTO $tableName (`name`, `date`, `amount`, `timpaid`, `rennypaid`)
        VALUES ('$name', '$date', '$amount', '$timPaid', '$rennyPaid')";

$result = mysql_query($con, $sql)
    or die("Error: ".mysql_error());

mysqli_close($con);
?>

DB.php is my database settings. DB.php是我的数据库设置。 I call a query to it on page load and it connects and pulls data fine, so I know it's not an issue there. 我在页面加载时对其调用查询,它可以连接并提取数据,因此我知道这不是问题。 I also don't get any errors. 我也没有任何错误。 I get a status code 200 OK on the post. 我在帖子中收到状态码200 OK。

Here's the ajax post: 这是ajax帖子:

var name = $('#name').val();
var amount = $('#amount').val();
var timPaid = $('#timPaid').val();
var rennyPaid = $('#rennyPaid').val();
var data = $('#newSubmissionForm').serialize();

$.ajax({
    url: 'insert.php',
    data: data,
    type: 'post',
    success: function()
    {
        window.location.href = '';
    }
 });

Does it have something to do with me serializing it? 与序列化有关吗?

I hope this is enough info. 我希望这是足够的信息。 Thanks! 谢谢!

mysql_query requires first parameter as query and second parameter as connection object (optional) mysql_query需要第一个参数作为查询,第二个参数作为连接对象(可选)

change this 改变这个

$result = mysql_query($con, $sql);

to

$result = mysql_query($sql, $con);

Also you used mysql for connection and query but you used mysqli to close the connection. 同样,您使用mysql进行连接和查询,但是使用mysqli来关闭连接。

You have likely just switched the $sql and $con statement. 您可能刚刚切换了$ sql和$ con语句。 in mysql_query $sql should be the first parameter. mysql_query $ sql中的第一个参数。 It's easy to forget, since mysqli_query should have $con as the first. 容易忘记,因为mysqli_query应该以$ con作为第一个。 :/ :/

php die() statement is the same as exit, and will end the script with status 200. Likely you DO get an error in the output. php die()语句与exit相同,并且将以状态200结束脚本。您可能会在输出中得到错误。 Try viewing it in eg developer console (Chrome) 尝试在开发者控制台(Chrome)中查看它

  1. check in internet explorer which code is generated (inspect element) 在Internet Explorer中检查生成了哪些代码(检查元素)
  2. add to the beginning of your session php file 添加到会话php文件的开头
 <?php echo '<pre>'; var_dump($_POST); die(); ?> 

This will show the values that are sent to the database. 这将显示发送到数据库的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM