我创建了一个表,用于在数据库中输入“Keys”以及从数据库中删除它们的选项它完美地显示了表,但问题是我无法将其输入到数据库中即使细节已经连接起来。
<?php
define(DBUSER,"N/A");
define(DBPWD, "N/A");
define(DBNAME, "N/A");
define(DBHOST, "N/A");
$dbConn = dbconnect();
$txt = "<table align=\"center\">";
$txt .= "<tr><td>KeyCode</td><td>Date</td><td>Sold</td><td>Email</td></tr>";
$sql = "SELECT * FROM `benkeys` where keycode = 1";
$res = mysql_query($sql, $dbConn)
or die(mysql_error());
if($res && mysql_num_rows($res)) {
}
$row=mysql_fetch_assoc($res);
mysql_close($dbConn);
$cal = "js_del_key('".$row["keycode"]."'); return false;";
$txt .= "<tr><td>".$row['keycode']."</td><td>".$row['datum']."</td>";
$txt .= "<td>".$row['sold']."</td><td>".$row['email']."</td>";
$txt .= "<td><input type=\"button\" name=\"del\" value=\"DELETE\" onclick=\"".$cal."\"></td></tr>";
$cal = "js_add_key(); return false;";
$txt .= "<tr><td><input type=\"text\" size=\"46\" id=\"nkey\" /></td><td>".date("Y-m-d")."</td>";
$txt .= "<td>N</td><td> </td>";
$txt .= "<td><input type=\"button\" name=\"addkey\" value=\"ADD NEW\" onclick=\"".$cal."\"></td></tr>";
$txt .= "</table>";
echo $txt;
function closeConn(){
mysql_close();
}
return($txt);
function p_adm_del_key($key)
{
$link = dbconnect();
$sql = "delete from `benkeys` where `keycode`='".$key."' limit 1";
mysql_query($sql);
mysql_close($link);
return(p_adm_list_keys());
}
function p_adm_add_key($key)
{
$link = dbconnect();
$sql = "insert into `benkeys` (`keycode`,`datum`,`sold`) values ('".$key."','".date("Y-m-d")."','N')";
$returnValue = mysql_query($sql, $link);
mysql_close($link);
return $returnValue; //This will return true/false if the query succeeded or not
}
if (p_adm_add_key('some-key')) {
//Success!
} else {
//Failure. ;(
}
function dbconnect()
{
$link = mysql_connect(DBHOST, DBUSER, DBPWD) or die ("Error: ".mysql_error());
mysql_select_db(DBNAME) or die("Could not select database: ".mysql_error());
return($link);
}
<?
答案 0 :(得分:0)
你的dbconnect()
函数指的是一些常量(DBHOST,DBUSER,DBPWD,DBNAME),但我看不到它们是在任何地方定义的。定义它们或将它们作为参数传递给函数。
只需更改脚本的顶部:
$username="N/A";
$password="N/A";
$database="N/A";
$host="N/A";
$dbConn = mysql_connect($host, $username, $password, $database)
or die("Could not connect: " + mysql_error());
mysql_select_db($database, $dbConn)
or die("Could not find database: " + mysql_error());
......对此:
define(DBUSER,"N/A");
define(DBPWD, "N/A");
define(DBNAME, "N/A");
define(DBHOST, "N/A");
$dbConn = dbconnect();
function p_adm_add_key($key)
{
$link = dbconnect();
$sql = "insert into `benkeys` (`keycode`,`datum`,`sold`) values ('".$key."','".date("Y-m-d")."','N')";
$returnValue = mysql_query($sql, $link);
mysql_close($link);
return $returnValue; //This will return true/false if the query succeeded or not
}
然后你可以像这样调用add key函数:
if (p_adm_add_key('some-key')) {
//Success!
} else {
//Failure. ;(
}