-1

Im using the following code for records inserting into database field.I want to correct the following if and else condition because when records are inserting in database it should echo 0 but it is giving 1.Im unable to figure out the problem.Please i need correction in my code.

<?php    /****************dbfile.php****/
$data = $_REQUEST['columns_one'];
$store_data = explode(",",$data);
//print_r($store_data);
$con = mysql_connect("localhost","root","");
mysql_select_db("formbuilder",$con);

$query = "INSERT INTO record(form_id,form_structure) VALUES('1','value')";
$result = mysql_query($query);

if(mysql_num_rows($result)>0)
{
    echo 0;
}
else
{
    echo 1;

}

?>

<script>   <!--- ajax call----->
$.post("dbfile.php", {columns_one:columns_one },  
            function(result){  
                //if the result is 1  
                if(result == 1){
                alert('success');  

                    return true;
                }else{ 
                alert('fail'); 

                    return false;

                }  
</script>
Sam
  • 27
  • 2
  • 5
  • 1
    http://codereview.stackexchange.com/ – Yam Marcovic Nov 01 '12 at 09:22
  • You are using [an obsolete database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Nov 01 '12 at 09:23
  • 2
    @YamMarcovic — This is a (very poorly expressed) debugging question, not a request for a code review. – Quentin Nov 01 '12 at 09:24

2 Answers2

3

You are using the wrong function. mysql_num_rows will not return the rows affected by INSERT statements, see here:

http://www.php.net/manual/en/function.mysql-affected-rows.php

Use mysql_affected_rows.

Arran
  • 24,648
  • 6
  • 68
  • 78
0

You must insert space after commas ! Change this line of your code:

$query = "INSERT INTO record(form_id,form_structure) VALUES('1','value')";

with this code:

$query = "INSERT INTO record (`form_id`, `form_structure`) VALUES ('1', 'value')";

Hope it can work Good luck...

Michael Antonius
  • 942
  • 2
  • 11
  • 20