当我尝试向sql提交约会请求时,总是出现同样的错误

xt0899hw  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(235)

我对sql和php很陌生。
我试图提交这个表格到mysql服务器,但我不断得到这个错误…我不知道哪里的错误是。。。
错误:将值('123 main st','chihuahua','los angeles','blankemail@gmail.com“,”john“,”doe“,”yes“,”iris“,”dog“,”(555)123-4568“,”ca“,”90001“)整数值不正确:第1行“neuterdorspayed”列的“yes”
代码如下:

<?php

$servername = "localhost";
$username = "root";
$password = "pwdpwd";
$dbname = "pet_shop";

$db = new mysqli($servername, $username, $password, $dbname);

if (mysqli_connect_errno()) {
echo 'Could not connect: ' . mysql_error();
} else {

function clean_data($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

$firstname = clean_data($_POST['aptfirstname']);
$lastname = clean_data($_POST['aptlastname']);
$address = clean_data($_POST['aptaddress']);
$city = clean_data($_POST['aptcity']);
$state = clean_data($_POST['aptstate']);
$zip = clean_data($_POST['aptzip']);
$phonenumber = clean_data($_POST['aptphonenumber']);
$email = clean_data($_POST['aptemail']);
$petname = clean_data($_POST['aptpetname']);
$neutered = clean_data($_POST['aptneutered']);
$pettype = clean_data($_POST['aptpettype']);
$breed = clean_data($_POST['aptbreed']);

if ($neutered == "true") {
    $neutered = "Yes";
} else {
    $neutered = "No";
}

if ($pettype == "Cat") {
    $breed = ""; 
}

$sql = "INSERT INTO grooming (Address, Breed, City, Email, FirstName, LastName, NeuteredOrSpayed, PetName, PetType, PhoneNumber, State, Zip) VALUES ('$address', '$breed', '$city', '$email', '$firstname', '$lastname', '$neutered', '$petname', '$pettype', '$phonenumber', '$state', '$zip')";

if ($db->query($sql) === TRUE) {
    echo "success"; 
} else {
    echo "Error: " . $sql . "<br>" . $db->error;
}

$db->close();
}

?>

如果你需要更多的上下文到表单代码,我也可以发布,只是在这里发布了很多

vc9ivgsu

vc9ivgsu1#

这是一个简单的数据类型错误,数据库中名为neuted的列的数据类型为 Integer 你要插入的值是“是”,这绝对不是 integer .
尝试将整数1和0分别作为yes和no。

8e2ybdfx

8e2ybdfx2#

很清楚。。正如@berto99和@pete等人提到的。。
列“neuturedorspayed”的数据类型是int,因此在代码中应该是这样的

if ($neutered == "true") {
    $neutered = 1; # it was 'Yes' --> string, need to be int, so 1
} else {
    $neutered = 0; # it was 'No' --> string, need to be int, so 0
}

相关问题