我拥有的代码无法在数据库中创建值

时间:2019-05-25 14:17:46

标签: php

我对此代码有一点问题。当我运行它时,它不会在数据库中创建任何值并显示出来。

  

解析错误:语法错误,出现以下意外情况:“ INSERT”(T_STRING)   /home/vol6_8/epizy.com/epiz_23744660/htdocs/main/test.php,第8行

这是我的代码:

include("config.php);

// Create connection
$conn = new mysqli($dbhost, $dbuser, $dbpasswd, $dbname);

// prepare and bind
$stmt = $conn->prepare("INSERT INTO phpbb_crany ('crany_id', 'crany_name', 'crany_breed', 'crany_gender', 'crany_level', 'crany_born', 'crany_mother', 'crany_father', 'crany_element', 'crany_user') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

$stmt->bind_param("ssssssssss",1, $cn, $cb, $cg, $cl, $cb, $cm, $cf, $ce, $cu);

// set parameters and execute
$cn = $_POST['petname'];

$cb = "jgh";

$cg = rand(0,1);

$cl = "1";

$cb = $_SERVER['REQUEST_TIME'];

$cm = 0;

$cf = 0;

$ce = 1;

$cu = 1;

$stmt->execute();

echo "New records created successfully";

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

我正在尝试使用php和mysql。有什么问题吗?

1 个答案:

答案 0 :(得分:0)

您的代码中存在逻辑错误。您的代码(bind_param()函数`在设置变量之前已被执行。

尝试一下:

include("config.php");

$stmt = $conn->prepare("INSERT INTO phpbb_crany ('crany_id', 'crany_name', 'crany_breed', 'crany_gender', 'crany_level', 'crany_born', 'crany_mother', 'crany_father', 'crany_element', 'crany_user') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

$cb = "jgh";
$cg = rand(0,1);
$cl = "1";
$cb = $_SERVER['REQUEST_TIME'];
$cm = 0;
$cf = 0;
$ce = 1;
$cu = 1;
$stmt->bind_param("ssssssssss",1, $cn, $cb, $cg, $cl, $cb, $cm, $cf, $ce, $cu);// <---- CHANGED - line moved

$stmt->execute();
echo "New records created successfully";
$stmt->close(); $conn->close();