PHP不希望插入到数据库中
这个代码有什么错PHP不希望插入到数据库中,php,Php,这个代码有什么错 <?php /* * Following code will create a new product row * All product details are read from HTTP Post Request */ mysql_select_db("dataparkir"); $con = mysql_connect("localhost","root","") or die("error connection"); if($con) {
<?php
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
mysql_select_db("dataparkir");
$con = mysql_connect("localhost","root","") or die("error connection");
if($con)
{
echo "connection success";
}
// check for required fields
$lantai = $_POST['lantai'];
$waktu_masuk = $_POST['waktu_masuk'];
$waktu_keluar= $_POST['waktu_keluar'];
$id_user = $_POST['id_user'];
$qr_code = $_POST['qr_code'];
//echo "input data: " . $lantai." ".$waktu_masuk." ".$waktu_keluar." ".$id_user." ".$qr_code." ";
// mysql inserting a new row
$result = mysql_query($con,"INSERT INTO transaksi (lantai, waktu_masuk, waktu_keluar, id_user, qr_code) VALUES($lantai, $waktu_masuk, $waktu_keluar, $id_user,'$qr_code')");
// check if row inserted or not
if ($result) {
echo mysql_insert_id();
} else {
echo '0';
}
?>
您确定要使用mysql\u查询而不是mysqli\u查询吗。尝试将所有mysql\u查询更改为mysqli\u查询,因为此示例:
$result = mysql_query($con,"INSERT INTO transaksi (lantai, waktu_masuk, waktu_keluar, id_user, qr_code) VALUES($lantai, $waktu_masuk, $waktu_keluar, $id_user,'$qr_code')");
应该是
$result = mysqli_query($con,"INSERT INTO transaksi (lantai, waktu_masuk, waktu_keluar, id_user, qr_code) VALUES($lantai, $waktu_masuk, $waktu_keluar, $id_user,'$qr_code')");
将第一个参数作为查询,第二个参数作为连接
将第一个参数作为连接,第二个参数作为查询
mysql_查询已被弃用,因此建议使用mysqli_查询此代码有太多错误 使用不推荐使用且未维护的mysql扩展 在建立连接之前调用mysql\u select\u db SQL注入漏洞 无错误检查 与时俱进
// I'm going to assume you can add some verification around these POST params.
// Here's a simple check
if (!isset($POST['lantai'], $_POST['waktu_masuk'], $_POST['waktu_keluar'], $_POST['id_user'], $_POST['qr_code'])) {
throw new Exception('Not all required POST parameters are set. ' . print_r($_POST, true));
}
$lantai = $_POST['lantai'];
$waktu_masuk = $_POST['waktu_masuk'];
$waktu_keluar= $_POST['waktu_keluar'];
$id_user = $_POST['id_user'];
$qr_code = $_POST['qr_code'];
$con = new mysqli('localhost', 'root', '', 'dataparkir');
if ($con->connect_errno) {
throw new Exception($con->connect_error, $con->connect_errno);
}
$con->set_charset('utf8'); // change if not appropriate
if (!$stmt = $con->prepare('INSERT INTO transaksi (lantai, waktu_masuk, waktu_keluar, id_user, qr_code) VALUES (?, ?, ?, ?, ?)')) {
throw new Exception($con->error, $con->errno);
}
$stmt->bind_param('siiis', $lantai, $waktu_masuk, $waktu_keluar, $id_user, $qr_code);
if (!$stmt->execute()) {
throw new Exception($stmt->error, $stmt->errno);
}
echo $con->insert_id;
放置mysql\u选择\u dbdataparkir;连接后。加上$lantai、$waktu_masuk、…-值“$lantai”、“$waktu_masuk”、…”的报价,。。。。mysql_query$con,$con在末尾。这是本地主机还是远程服务器上的错误?也许PHP版本改为5.5,mysql扩展被破坏?在文件顶部添加错误报告错误报告全部;ini设置“显示错误”,1;在开发过程中。参考。占位符修复/防止了这么多问题,新代码不应该使用mysql_uu函数。我已经将其更改为mysqli_u查询,现在错误显示:无法输入数据:“字段列表”中的未知列“$lantai”是的,这是我java文件中的$lantai:String lantai_String=;lantai_string=Integer.tostringlandai,我从这里解析:params.addnew BasicNameValuePairlantai,lantai_string;你确定你在文件中超过了$lantai吗$兰泰=$_POST[“兰泰”];是的,我确定,我已经在我的java文件中写了这个:HttpPost-HttpPost=new-HttpPost;将所有插入的值用单引号括起来。值“$lantai”、“$waktu_masuk”、“$waktu_keluar”、“$id_user”、“$qr_code”最终有人发现OP在设置连接之前试图选择数据库。@Darren对该问题的第一个评论表明了这一点too@Darren啊哼。。。你叫什么?超过20分钟。以前-@Phil+1我本打算提出一个类似的答案,但在看到OP将代码与java结合使用后,我很快放弃了这个想法,担心这只会导致更大的问题。不是真正的恐惧,你知道我的意思-@弗雷德二世-哦,我的上帝,请原谅我公然的愚蠢,只要给我一记耳光,然后和我的脸联系起来!
// I'm going to assume you can add some verification around these POST params.
// Here's a simple check
if (!isset($POST['lantai'], $_POST['waktu_masuk'], $_POST['waktu_keluar'], $_POST['id_user'], $_POST['qr_code'])) {
throw new Exception('Not all required POST parameters are set. ' . print_r($_POST, true));
}
$lantai = $_POST['lantai'];
$waktu_masuk = $_POST['waktu_masuk'];
$waktu_keluar= $_POST['waktu_keluar'];
$id_user = $_POST['id_user'];
$qr_code = $_POST['qr_code'];
$con = new mysqli('localhost', 'root', '', 'dataparkir');
if ($con->connect_errno) {
throw new Exception($con->connect_error, $con->connect_errno);
}
$con->set_charset('utf8'); // change if not appropriate
if (!$stmt = $con->prepare('INSERT INTO transaksi (lantai, waktu_masuk, waktu_keluar, id_user, qr_code) VALUES (?, ?, ?, ?, ?)')) {
throw new Exception($con->error, $con->errno);
}
$stmt->bind_param('siiis', $lantai, $waktu_masuk, $waktu_keluar, $id_user, $qr_code);
if (!$stmt->execute()) {
throw new Exception($stmt->error, $stmt->errno);
}
echo $con->insert_id;