douba05167 2013-06-15 20:41
浏览 33
已采纳

表格帖子不起作用

I have coded the following and its posting the data as I can see it in console. However, it does not seem to add to the database. I think it could be my condition in the process.php file for action: add_new, but I'm not sure.

Not a huge person on PHP so unsure how to see why it's failing but it's returning successfully to the AJAX, I think but not adding to database, so I assume it might be what I said above.

if($_POST['action'] == "add_new"){

jQuery and 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());
}
?>
  • 写回答

3条回答 默认 最新

  • dongxiongshi9952 2013-06-15 22:11
    关注

    In God I trust is right. Using PDO is much safer. Here's a short example on how to insert into your database:

    $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();   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向