首先,我对PHP一无所知。这是我用来将数据添加到mySQL库的PHP脚本:
<?php
header("Access-Control-Allow-Origin: *");
//==MAN2==adf
$hostname = "localhost"; //This is probably correct
$username = "BLERB"; // Your MySQL username
$password = "BLERB"; // Your MySQL Password
$database = "BLERB"; // The name of your database
$table = "BLERB"; // Your actual table to hold the data
//$table2 = ""; //You can use multiple tables to organize your database!
// Make a MySQL Connection no changes need to be made here
//==MAN3==
$dbh = new PDO("mysql:host=$hostname; dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
/*
STEP 2 Potential Variables passed from URL - list them all here.
*/
//==MAN4==
$f = $_POST['f']; //na=new account sv=save ld=load onformation and au=authorize access
$pname = $_POST['n'];
$pip = $_POST['ip'];
$pnp = $_POST['np'];
$pop = $_POST['op'];
$psp = $_POST['sp'];
$pword = $_POST['pass'];
//==MAN5==
$salt = "gameMaker"; // CHANGE THIS TO SOMETHING SECRET
$epword = crypt($pword,$salt); // This encrypts the password and sets it to a variable.
//==MAN6==
//Functions Start
//This function will create a new user account or save file
//==MAN7==
function create_account($dbh,$table,$pip,$pnp,$pop,$psp,$epword,$pname) //declare function Part between () is all the variables you will need for this function.
{
try
{
$stmt = $dbh->prepare("INSERT INTO $table (p_name, p_word, p_ip, p_np, p_op, p_sp) VALUES (:pname, :pword, :pip, :pnp, :pop, :psp)"); //Prepare statement for instert
$stmt ->bindparam(':pname', $pname, PDO::PARAM_STR); //Build inserts array
$stmt ->bindparam(':pword', $epword, PDO::PARAM_STR);
$stmt ->bindparam(':pip', $pip, PDO::PARAM_STR);
$stmt ->bindparam(':pnp', $pnp, PDO::PARAM_STR);
$stmt ->bindparam(':pop', $pop, PDO::PARAM_STR);
$stmt ->bindparam(':psp', $psp, PDO::PARAM_STR);
$stmt->execute(); //Execute array
echo "1"; //Everything worked!
}
catch (PDOExecption $ex)
{
echo "0"; //Something went wrong :(
}
$dbh = null;
}
// This function will save information to an existing account
//==MAN8==
function save_info($dbh, $table, $pname, $pip, $pnp, $pop, $psp)
{
try
{
$stmt = $dbh->prepare("UPDATE $table SET p_ip=?, p_np=?,p_op=?,p_sp=? WHERE p_name=?");
$stmt->execute(array($pip,$pnp,$pop,$psp,$pname));
echo "1";
}
catch (PDOExecption $ex)
{
echo "0";
}
$dbh = null;
}
// This function will pull account information
//==MAN9==
function load_info($dbh,$table,$pname)
{
{
$stmt = $dbh->query("SELECT * FROM $table WHERE p_name = '$pname'");
$result = $stmt->fetchObject();
echo $result->p_name.",".$result->p_ip. "," .$result->p_np. "," .$result->p_op. "," . $result->p_sp;
$dbh = null;
}
}
// This function will simply check if a user exists in the system - useful to auhorize thier aceess.
//==MAN10==
function auth($dbh, $table, $epword, $pname)
{
$stmt = $dbh->query("SELECT COUNT(*) from $table WHERE p_name = '$pname' AND p_word = '$epword'");
$result = $stmt->fetchColumn();
if ($result <= 0)
{
echo '0';
}
else
{
echo '1';
}
}
// This function is used for nothing more than testing that the script and database are working.
//==MAN11==
function connection_test($dbh,$table,$pname)
{
$stmt = $dbh->query("SELECT COUNT(*) from $table");
$result = $stmt->fetchColumn();
if ($result <= 0)
{
echo 'Could not communicate with database. Check your setup, username & pass, or your database is empty.';
}
else
{
echo 'Everything seems good you have '. $result .' row(s) in your database!';
}
}
// This determines which function to call based on the $f parameter passed in the URL.
//==MAN12==
switch($f)
{
case na: create_account($dbh,$table,$pip,$pnp,$pop,$psp,$epword,$pname); break;
case sv: save_info($dbh, $table,$pname,$pip,$pnp,$pop,$psp); break;
case ld: load_info($dbh,$table,$pname); break;
case au: auth($dbh,$table,$epword,$pname); break;
case ts: connection_test($dbh,$table,$pname); break;
default: echo"error";
}
?>
我在日志中收到这些错误:
[08-Aug-2017 21:50:59 America / Denver] PHP注意:未定义的索引:ip在第43行的/home1/codedgam/public_html/games/draw-online/gameSync.php [08-Aug-2017 21:50:59 America / Denver] PHP注意:未定义索引:第44行/home1/codedgam/public_html/games/draw-online/gameSync.php中的np [08-Aug-2017 21:50:59 America / Denver] PHP注意:未定义的索引:在第45行的/home1/codedgam/public_html/games/draw-online/gameSync.php中的操作 [08-Aug-2017 21:50:59 America / Denver] PHP注意:未定义的索引:sp在第46行的/home1/codedgam/public_html/games/draw-online/gameSync.php [08-Aug-2017 21:50:59 America / Denver] PHP注意:未定义索引:在第47行传入/home1/codedgam/public_html/games/draw-online/gameSync.php [08-Aug-2017 21:50:59 America / Denver] PHP注意:使用未定义的常数na - 假设&#39; na&#39;在第161行的/home1/codedgam/public_html/games/draw-online/gameSync.php [08-Aug-2017 21:50:59 America / Denver] PHP注意:使用未定义的常量sv - 假设&#39; sv&#39;在第162行的/home1/codedgam/public_html/games/draw-online/gameSync.php中 [08-Aug-2017 21:50:59 America / Denver] PHP注意:使用未定义的常量sr - 假设&#39; sr&#39;在第163行的/home1/codedgam/public_html/games/draw-online/gameSync.php [08-Aug-2017 21:50:59 America / Denver] PHP注意:使用未定义的常量ld - 假设&#39; ld&#39;在第164行的/home1/codedgam/public_html/games/draw-online/gameSync.php
引起错误的相关行是43-47:
$stmt ->bindparam(':pword', $epword, PDO::PARAM_STR);
$stmt ->bindparam(':pip', $pip, PDO::PARAM_STR);
$stmt ->bindparam(':pnp', $pnp, PDO::PARAM_STR);
$stmt ->bindparam(':pop', $pop, PDO::PARAM_STR);
$stmt ->bindparam(':psp', $psp, PDO::PARAM_STR);
第161-164行:
case na: create_account($dbh,$table,$pip,$pnp,$pop,$psp,$epword,$pname); break;
case sv: save_info($dbh, $table,$pname,$pip,$pnp,$pop,$psp); break;
case sr: save_and_return($dbh, $table,$pname,$pip,$pnp,$pop,$psp); break;
case ld: load_info($dbh,$table,$pname); break;
case au: auth($dbh,$table,$epword,$pname); break;
case ts: connection_test($dbh,$table,$pname); break;
default: echo"error";
因为我不了解PHP,所以我真的不知道如何解决这个问题。我尝试过搜索解决方案,但没有看到任何类似我的代码。
非常感谢任何帮助。