我是PHP,MYSQL的新手,试图将某种数据添加到我用html和php代码制作的表中。我的数据库是在phpmyadmin,它只是办公用品和东西等随机列表。我将在下面提供该表的信息。现在我也做了一个插入,如果你单击它,你就会进入输入字段,你可以在其中添加或插入数据到数据库。我回应了如果实际插入插入数据的结果是什么,如果它没有工作弹出的东西。顺便说一句,我没有任何错误,所以我真的不确定问题是什么。还请你忍受我的英语不好,所以我无法正确解释。这是代码,有4个单独的文件: 这是数据库的链接代码:
<?php
$servername = 'localhost';
$user ='mysql1_user';
$password ='ABCD1234';
$databasename ='mysqldb1';
$con= mysqli_connect($servername, $user, $password, $databasename);
if(!$con){
die("Wrong Entry for database". mysqli_connect_error());
}
else{
//echo "Correct Entry";
}
?>
这是主要代码:
<html>
<head>
<title>MYSQL</title>
<script src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
</head>
<body>
<a href="f1.php?page=insert">INSERT</a><br><br><br>
<?php
@$v1=$_GET['page'];
if($v1=='insert'){
$insertp = <<<t1
Name: <input id="name">
Quantity: <input id="qty">
Type: <input id="type">
Price: <input id="price">
<button id="add">Add</button>
<script>
$("add").click(function(){
$.post("insert.php", {
name: $("#name").val(),
qty: $("#qty").val(),
type: $("#type").val(),
price: $("#price").val(),
},
function(result){
$("body").append(result);
})
})
</script>
t1;
echo $insertp;
}
else{
require "config/con1.php";
echo "<input type='search'><button id='search'>Search</button>";
$sql1="SELECT *FROM products
-- WHERE name='Pen' ORDER BY price DESC WHERE type='Office' AND price BETWEEN 10 and 700 ";
$result= mysqli_query($con, $sql1);
$value = mysqli_num_rows($result);
echo "<table border=2>";
while($line = mysqli_fetch_assoc($result)){
echo "<tr><td>".$line ['name']. "</td><td>".$line["qty"]."</td><td>".$line['type']."</td><td>".$line['price']."</td><td><a href=edit.php?id=".$line['id']."><img src='pencil.png' width='50'></a></td><td><img src='delete.png' width='50' class='delete' id=".$line['id']."></td></tr>";
}
echo "</table>";
}
?>
<script>
$("#search").click(function(){
var varval=$("input[type='search']").val();
$.post("search.php", {vtext: varval}, function (value){
$("table").html(value)
})
})
$(".delete").click(function(){
var delid=$(this);
$.post("delete.php", {deleteid:$(this).attr("id")}, function(result){
$(delid).parent().parent().hide(2000)
});
})
</script>
</body>
</html>
以下是插入代码的位置:
<?php
require "config/con1.php";
$name=$_POST['name'];
$qty=$_POST['qty'];
$type=$_POST['type'];
$price=$_POST['price'];
$secure_name=mysqli_real_escape_string($con, $name);
$secure_qty=mysqli_real_escape_string($con, $qty);
$secure_type=mysqli_real_escape_string($con, $type);
$secure_price=mysqli_real_escape_string($con, $price);
$sql="INSERT INTO products(name, qty, type, price) VALUES ('$secure_name', '$secure_qty', $secure_type, $secure_price)";
if(mysqli_query($con, $sql)){
echo "One detail has been inserted into the database";
}
else{
echo "Wrong ". mysqli_error($con);
}
?>
答案 0 :(得分:1)
$('add')应为$('#add')。如果要绑定ID,则需要添加#。
答案 1 :(得分:-1)
将INSERT查询更改为:
$sql="INSERT INTO products(name, qty, type, price) VALUES ('$secure_name', '$secure_qty', '$secure_type', '$secure_price')";