我希望通过使用函数单击插入和更新按钮来插入和更新客户数据,但是当我单击插入或更新按钮时,没有插入或更新数据是什么问题我的代码?
<form action="Customer.php" method="post">
<div>
<form>
Phone No <input type="number" placeholder="Search" name="phoneno" />
First Name <input type="text" name="FirstName" />
Last Name<input type="text" name="LastName" />
Phone No<input type="number" name="CustomerPhoneNo" />
Address<input type="text" name="Address" />
Customer ID<input type="number" name="CustomerID" />
<input type="Submit" value="Add Customer" style="font-size:20px" onClick="insert()">
<input type="Submit" value="Update Customer" style="font-size:20px" onClick="update()">
</form>
</div>
"Customer.php"
<?php
$dbhost="127.0.0.1";
$dbname="root";
$dbuser="info";
$dbpsd="";
$link = mysqli_connect("$dbhost", "$dbuser", "$dbpsd", "$dbname");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$phoneno = mysqli_real_escape_string($link, $_POST['phoneno']);
$FirstName = mysqli_real_escape_string($link, $_POST['FirstName']);
$LastName = mysqli_real_escape_string($link, $_POST['LastName']);
$CustomerPhoneNo = mysqli_real_escape_string($link, $_POST['CustomerPhoneNo']);
$Address=mysqli_real_escape_string($link, $_POST['Address']);
$CustomerID = mysqli_real_escape_string($link, $_POST['CustomerID']);
function insert(){
$sql = "INSERT INTO clientinfo(phoneno, FirstName, LastName,CustomerPhoneNo,Address,CustomerID) VALUES ('$phoneno', '$FirstName', '$LastName','$CustomerPhoneNo','$Address','$CustomerID')";
echo "<span>Data Inserted successfully...!!</span>";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
function update(){
$sql="UPDATE clientinfo SET FirstName='$FirstName', LastName='$LastName',Address='$Address',CustomerID='$CustomerID WHERE phoneno='$phoneno' ";
if (mysqli_query($link, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($link);
}
}
close connection
mysqli_close($link);
?>
答案 0 :(得分:0)
答案 1 :(得分:0)
您不能使用onclick标签来确定所请求的页面PHP功能 一个简单的方法。在customer.php文件中替换以下代码。
替换:
function insert()
with:
if($_POST['Submit'] == 'Add Customer')
并替换:
function update()
with:
if($_POST['Submit'] == 'Update Customer')
答案 2 :(得分:0)
你正在调用像js函数这样的php函数。他们不这样做。将它们定义为 -
function insert($link, $post){
$phoneno = mysqli_real_escape_string($link, $post['phoneno']);
$FirstName = mysqli_real_escape_string($link, $post['FirstName']);
$LastName = mysqli_real_escape_string($link, $post['LastName']);
$CustomerPhoneNo = mysqli_real_escape_string($link, $post['CustomerPhoneNo']);
$Address=mysqli_real_escape_string($link, $post['Address']);
$CustomerID = mysqli_real_escape_string($link, $post['CustomerID']);
$sql = "INSERT INTO clientinfo(phoneno, FirstName, LastName,CustomerPhoneNo,Address,CustomerID) VALUES ('$phoneno', '$FirstName', '$LastName','$CustomerPhoneNo','$Address','$CustomerID')";
echo "<span>Data Inserted successfully...!!</span>";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
并将它们称为 -
insert($link, $_POST); // if it is inserting