我编写了以下内容,并将其发布数据,因为我可以在控制台中看到它。但是,它似乎没有添加到数据库中。我认为这可能是我在process.php文件中的条件:add_new,但我不确定。
不是PHP上的大人物如此不确定如何看到它失败的原因但是它成功返回到AJAX,我认为但不是添加到数据库中,所以我认为它可能就是我上面所说的。
if($_POST['action'] == "add_new"){
jQuery和Form:
<script type="text/javascript">
$(document).ready(function(){
$('div.success').hide();
$("form#add_new").submit(function() {
var started = "<?php echo time(); ?>";
var duration = $('#duration').val();
var ticket_price = $('#ticket_price').val();
var win_percentage = $('#win_percentage').val();
var description = $('#description').val();
var available = $('#available').val();
var action = "add_new";
$.ajax({
type: "POST",
url: "/admin/process.php",
data: { started: started, duration: duration, ticket_price: ticket_price, win_percentage: win_percentage, description: description, available: available, action: action },
dataType: 'json',
success: function(data){
$('form#add_new').hide(function(){$('div.success').fadeIn();});
}
});
return false;
});
});
<div class="success">successfully added new ticket</div>
<form id="add_new" method="post" class="form-horizontal">
<fieldset>
<div class="control-group">
<label for="duration" class="control-label">Duration (days)</label>
<div class="controls">
<input type="text" id="duration" class="input-xlarge" value="" />
</div>
</div>
<div class="control-group">
<label for="ticket_price" class="control-label">Ticket Price</label>
<div class="controls">
<input type="text" id="ticket_price" name="ticket_price" class="input-xlarge" value="" />
</div>
</div>
<div class="control-group">
<label for="available" class="control-label">Available to be Won(%)</label>
<div class="controls">
<input type="text" id="available" name="available" class="input-xlarge" value="" />
</div>
</div>
<div class="control-group">
<label for="win_percentage" class="control-label">Percentage of Winners</label>
<div class="controls">
<input type="text" id="win_percentage" name="win_percentage" class="input-xlarge" value="" />
</div>
</div>
<div class="control-group">
<label for="description" class="control-label">Description</label>
<div class="controls">
<textarea rows="4" id="description" name="description" class="input-xlarge"></textarea>
<span class="help-block">Automatic resize</span> </div>
</div>
<div class="control-group">
<div class="controls">
<button class="btn btn-gebo" type="submit">Save changes</button>
</div>
</div>
</fieldset>
</form>
process.php
:
<?php
include "../utils.php";
// Add new raffle ticket
if($_POST['action'] == "add_new"){
$started = $_POST['started'];
$duration = $_POST['duration'];
$ticket_price = $_POST['ticket_price'];
$win_percentage = $_POST['win_percentage'];
$description = mysql_real_escape_string($_POST['description']);
$available = $_POST['available'];
my_query("INSERT INTO " . $db_prefix . " (lotteries(started, duration, ticket_price, win_percentage, description,available) values) VALUES ('$started','$duration','$ticket_price','$win_percentage','$description','$available')");
mysql_query($add_to_shelf) or die(mysql_error());
}
?>
答案 0 :(得分:0)
错误太多,但一开始就没有名为my_query
的功能。在PHP的第二行。如果我是你,我会首先使用基本的GET变量或其他东西检查PHP代码是否正常工作,然后处理POST变量。
完成后,您的代码将受 mysql注入的约束。您需要查看停止这些已弃用的mysql_
函数并转到支持的类型PDO
。
答案 1 :(得分:0)
首先,我建议
1)检查你的PHP是否被调用 2)检查它在做什么以及结果是什么。
如果您没有工具箱,可以使用以下功能:
function PrintLog($string)
{
if (! BDEBUG)
return ;
$logDir = LOGPATH;
if (! file_exists($logDir))
{
umask(000);
if (! mkdir($logDir, 0777))
ErrorLog("Failed to create Directory $logDir");
chmod($logDir, 0777);
}
$fplog = @fopen($logDir.'/'.'debug-'.date('Y-m-d').'.log', 'a');
if ($fplog)
{
fwrite($fplog, date("d-m-Y H:i:s")."\t".$string);
fwrite($fplog, "\n");
fclose($fplog);
}
}
function PrintrLog(&$arrayToDump, $strName = '')
{
ob_start();
print_r($arrayToDump);
PrintLog("$strName = ".ob_get_contents());
ob_end_clean();
}
在PHP脚本中插入那些函数和代码:
define("BDEBUG", true);
define("LOGPATH", "./log/");
PrintrLog( $_POST);
然后,看一下生成的日志文件。 如果没有,看看你的Apache日志,可能会有一些错误将在这里等你修复。 如果log在这里,检查POST是否应该如何,那么它可能是你的SQL中的一个问题。 我对你的数据库架构了解不多,但直观地说,我会写更多类似的东西:
$my_query = "INSERT INTO lotteries (started, duration, ticket_price, win_percentage, description,available) VALUES ('$started', '$duration', '$ticket_price', '$win_percentage', '$description', '$available')";
PrintLog($my_query); // Take it from logs, and run it manually inPHPmyAdmin to debug it eaily
您也可以在工具箱中添加此类功能:
function ExecQuery($Cnx, $baseName, $query, &$sqlError)
{
PrintLog($query);
$result = mysql_query($query, $Cnx);
if (! $result)
{
$sqlError = "ERROR: Failed to execute query '$query' [".mysql_errno().': '.mysql_error().']';
PrintLog($sqlError);
return false;
}
return $result;
}
我希望这能帮助你进行当前和未来的调查; - )
PS:您还应该考虑使用这样的函数来系统地避免SQL注入:
function getPostValue($name) {
if (! isset($_POST[$name]))
return '';
return(mysql_real_escape_string($_POST[$name]));
}
答案 2 :(得分:0)
In God I trust是对的。使用PDO更安全。这是一个关于如何插入数据库的简短示例:
$stmt = $dbo->prepare("INSERT INTO customer (name, email, password)
VALUES (:name, :mail, :pass)");
$stmt->bindParam(':name', $_POST['full_name']);
$stmt->bindParam(':mail', $_POST['email']);
$stmt->bindParam(':pass', $_POST['password']);
$stmt->execute();