gpt4 book ai didi

php - MySQL 和 PHP 插入查询失败

转载 作者:行者123 更新时间:2023-11-29 08:51:38 25 4
gpt4 key购买 nike

我知道这里没有足够的验证,只是进行一些测试。 $result 总是返回空?我的查询不好吗?我是 PHP 新手,我还没有完全掌握如何将变量连接到字符串中。使用 OOP 形式,因为我非常熟悉它和概念。

另外,我知道这段代码非常草率...只是想直接深入 =) `

$page = new Page();

$page->title = "Add a New Item";
$page->DisplayHeader();
$page->DisplaySidebar();

if (isset($_POST['submit']))
{
// make short variable names
$name = trim($_POST['name']);
$level = intval($_POST['level']);
$slot = strtolower($_POST['slot']);
$hp = intval($_POST['hp']);
$mana = intval($_POST['mana']);
$mvs = intval($_POST['mvs']);
$int = intval($_POST['int']);
$wis = intval($_POST['wis']);
$str = intval($_POST['str']);
$dex = intval($_POST['dex']);
$con = intval($_POST['con']);
$p_ac = intval($_POST['p_ac']);
$m_ac = intval($_POST['m_ac']);
$saves = intval($_POST['saves']);
$hit = intval($_POST['hit']);
$dam = intval($_POST['dam']);


$queryOk = 1;

if (empty($name) || empty($level) || empty($slot))
{
echo '<h3>Please enter all the required fields</h3>';
$queryOk = 0;
}

// Instantiate database object and connect
@ $db = new mysqli('*host*', '*user*', '*pass*', '*database*');

// Check connection to
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database, try again later';
}

$query = "INSERT INTO items (name, level, slot, hp, mana, mvs, int, wis, str, dex, con, p_ac, m_ac, saves, hit, dam)".
"V ALUES ('$name', $level, '$slot', $hp, $mana, $mvs, $int, $wis, $str, $dex, $con, $p_ac, $m_ac, $saves, $hit, $dam)";

$result = $db->query($query);

if (!$result)
{
echo '<h3>Error: Item was not entered. (Your webmaster sucks)</h3>';
}
else {
echo "<p>The items \"$name\" was successfully entered into the database. <a href=\"equipment.php\>Back to Equipment or add another item.</a></p>";
}

$db->close();
}`

最佳答案

如果 VALUES 中的空格实际上在您的代码中,则会导致查询失败

更新

如果这不是错误的原因,请使用 $mysqli->error 查看发生了什么错误。

if (!$result)
{
echo '<h3>'$mysqli->error' (Your webmaster sucks)</h3>';
}

关于php - MySQL 和 PHP 插入查询失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11044078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com