Sjay
Sjay

Reputation: 801

Insert Into MYSQL DB using jquery/ajax 500 internal server error

I'm inserting radio checked data values into mysql db with php/jquery and ajax while inserting values i'm getting internal server 500 error...

Any help is appreciated Thanks!

Html Code

<div>
    <input class="radio_div_clicked" value="<?php echo $row['name'];?> name="radio_section" id="<?php echo $row['id'];?>"/>
    <p><?php echo $row['name'];?></p>
    <button id="add_feed">Add</button>
</div>

JQUERY CODE

<script type="text/javascript">
$('body').delegate('#add_feed','click',function(){

    var radio_div_clicked = $('input:radio[name=radio_section]:checked').val();
    $.ajax({
            type:"POST",
            url:"index.php",
            cache: false,
            data:{
                insert_section:1,
                radio_div_clicked:radio_div_clicked
            },
            success:function(data)
            {
                alert('Successfully Inserted Into Table');
            }
        });
    }

});
</script>

PHP code

<?php 
    $con = @mysql_connect('localhost','root','') or die('failed to connect server');
    $db = mysql_select_db('demo',$con) or die('failed to select db');
    if(isset($_POST['insert_section']))
    {
        $radio_div_clicked = mysql_real_escape_string($_POST['radio_div_clicked']);
        $sql = mysql_query("insert into test(name) values($radio_div_clicked)";
        if($sql)
        {
            echo "Successfully Inserted Into Table";
        }
        else
        {
            echo "Failed To Insert Value Into Table"
        }
    }
?>
<?php 
    mysql_close ($con);
?>

Upvotes: 0

Views: 1051

Answers (1)

Norlihazmey Ghazali
Norlihazmey Ghazali

Reputation: 9060

Mysql query values($radio_div_clicked)"; did't closed properly, should be

$sql = mysql_query("insert into test(name) values('".$radio_div_clicked."')");

If you're using chrome, click on network tab, and click the requested link. Then, again click on preview or response header. Usually the details errors showing up there.

Upvotes: 1

Related Questions