检查is = mysqli做了什么,如果没有显示错误

时间:2014-04-09 09:24:55

标签: php mysqli

我有一个类似的mysqli查询:

$stmt = $mysqli->prepare("INSERT INTO vacancies(title,employer,hours,pay,pay_term,location,qualification,apply,desc,status,end_date,int_start,poss_start) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");

    $stmt->bind_param( 
    $_POST['title'], 
    $_POST['employer'],
    $_POST['hours'],
    $_POST['pay'],
    $_POST['pay_term'],
    $_POST['location'],
    $_POST['qualification'],
    $_POST['apply'],
    $_POST['desc'],
    $_POST['status'],
    $_POST['end_date'],
    $_POST['int_start'],
    $_POST['poss_start']);
    $stmt->execute(); 
    $stmt->close();
}

之后我又补充说:

if ($stmt->execute()) { // exactly like this!
        echo "Yup that executed";
    }else {
        echo "There was a problem with the statement";  
    }

但是,这些回声都不起作用。

如果行已成功输入且未显示错误,我希望显示成功消息。

按要求编辑

$con=mysqli_connect("localhost","xxx","xxx","xxx");
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else {
$stmt = $mysqli->prepare("INSERT INTO vacancies(title,employer,hours,pay,pay_term,location,qualification,apply,employdesc,status,end_date,int_start,poss_start) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");

        $stmt->bind_param( 
        $_POST['title'], 
        $_POST['employer'],
        $_POST['hours'],
        $_POST['pay'],
        $_POST['pay_term'],
        $_POST['location'],
        $_POST['qualification'],
        $_POST['apply'],
        $_POST['employdesc'],
        $_POST['status'],
        $_POST['end_date'],
        $_POST['int_start'],
        $_POST['poss_start']);
        $stmt->execute(); 
        $stmt->close();


    }

if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}

编辑,架构 Schema

1 个答案:

答案 0 :(得分:1)

desc是MySQL reserved word

选择其他名称或将其包装在反引号中

(title,employer,hours,pay,pay_term,location,qualification,apply,`desc`,status,end_date,int_start,poss_start)

另外,

替换为:(不知道您的数据库架构是什么,如果列是INT则修改)

$stmt->bind_param("sssssssssssss", 
$_POST['title'], 
$_POST['employer'],
$_POST['hours'],
$_POST['pay'],
$_POST['pay_term'],
$_POST['location'],
$_POST['qualification'],
$_POST['apply'],
$_POST['desc'],
$_POST['status'],
$_POST['end_date'],
$_POST['int_start'],
$_POST['poss_start']);

$stmt->execute(); 
$stmt->close();

添加错误报告:

if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;

编辑 - 测试数据库

这是我的测试代码:

<?php

DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');  
DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_NAME', 'xxx');

$db = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) 
OR die("could not connect");

$_POST['name'] = "Larry";
$_POST['pay'] = 50.5;

$sql = "INSERT INTO my_test_table (name, pay, date) VALUES (?,?,NOW())";
if (!$stmt = $db->prepare($sql)) {
    die($db->error);
}

$stmt->bind_param("ss", $_POST['name'], $_POST['pay']);
if (!$stmt->execute()) {
    die($stmt->error);
}

else{
echo "Data entered.";
}

$stmt->close();