Jump to content

Inserting into a table in the database


lauram340

Recommended Posts

in MYSQL i have a database called policedb1 and in that i have a table called workshift.

I want to enter data into workshift but when i click the insert button it says that the information hass been updated to the table workshift, but it hasn't.

I really don't know why this is happening and i can't see where i am going wrong.

The code im using is here. Any help would be much appriacted

 

CONFIRM INSERT

<?php
    
    $flag_insert = '';
    if (isset($_REQUEST['flag_insert']))
    {
        $flag_insert = $_REQUEST['flag_insert'];
    }
    
    if ($flag_insert == 'OK')
    {
    
    
    $query_insert = "Insert into ".$table." (";
    
    $flag_position_in_query = 0;
    for ($i = 0; $i < $columns; $i++)
    {
        if (!(strstr(mysql_field_flags($result,$i),'primary_key')) and !(strstr(mysql_field_flags($result,$i),'timestamp')))
        {
            if ($flag_position_in_query > 0)
            {
                $query_insert = $query_insert.",".mysql_field_name($result,$i);
            }
            else
            {
                $query_insert = $query_insert.mysql_field_name($result,$i);
                $flag_position_in_query++;
            }
        }
    }
    $query_insert = $query_insert.") values (";
    $flag_position_in_query = 0;
    for ($i = 0; $i < $columns; $i++)
    {
        if (!(strstr(mysql_field_flags($result,$i),'primary_key')) and !(strstr(mysql_field_flags($result,$i),'timestamp')))
        {
            if ($flag_position_in_query > 0)
            {
                //$query_insert = $query_insert.",".$_REQUEST['input_'.mysql_field_name($result,$i)] ;
                $query_insert = $query_insert.",";
                $flag_position_in_query++;
            }
            else
            {
                //$query_insert = $query_insert.$_REQUEST['input_'.mysql_field_name($result,$i)] ;
                $flag_position_in_query++;
            }
            if (mysql_field_type($result,$i) == 'string' or mysql_field_type($result,$i) == 'date')
            {
                $query_insert = $query_insert."'".$_REQUEST['input_'.mysql_field_name($result,$i)]."'" ;
            }
            else
                $query_insert = $query_insert.$_REQUEST['input_'.mysql_field_name($result,$i)] ;
            
        }
    }
    $query_insert = $query_insert.");";
    
    //echo $query_insert."<P>";
    
    mysql_query($query_insert);
    
    if($result2!=null)
    {
    $table = "people";


        $query_insert = "Insert into ".$table." (";
    
    $flag_position_in_query = 0;
    for ($i = 0; $i < $columns2; $i++)
    {
        if (!(strstr(mysql_field_flags($result2,$i),'primary_key')) and !(strstr(mysql_field_flags($result2,$i),'timestamp')))
        {
            if ($flag_position_in_query > 0)
            {
                $query_insert = $query_insert.",".mysql_field_name($result2,$i);
            }
            else
            {
                $query_insert = $query_insert.mysql_field_name($result2,$i);
                $flag_position_in_query++;
            }
        }
    }
    $query_insert = $query_insert.") values (";
    $flag_position_in_query = 0;
    for ($i = 0; $i < $columns2; $i++)
    {
        if (!(strstr(mysql_field_flags($result2,$i),'primary_key')) and !(strstr(mysql_field_flags($result2,$i),'timestamp')))
        {
            if ($flag_position_in_query > 0)
            {
                //$query_insert = $query_insert.",".$_REQUEST['input_'.mysql_field_name($result2,$i)] ;
                $query_insert = $query_insert.",";
                $flag_position_in_query++;
            }
            else
            {
                //$query_insert = $query_insert.$_REQUEST['input_'.mysql_field_name($result2,$i)] ;
                $flag_position_in_query++;
            }
            if (mysql_field_type($result2,$i) == 'string' or mysql_field_type($result2,$i) == 'date')
            {
                $query_insert = $query_insert."'".$_REQUEST['input_'.mysql_field_name($result2,$i)]."'" ;
            }
            else
                $query_insert = $query_insert.$_REQUEST['input_'.mysql_field_name($result2,$i)] ;
            
        }
    }
    $query_insert = $query_insert.");";
    
    //echo $query_insert."<P>";
    
    mysql_query($query_insert);
    
    }
    
    echo "Inserted new item <P>";
    
    } //end if ($flag_insert == 'OK')
    
    else
    
    {
        echo "A refresh has been taken without re-submitting data for the new item to be inserted<P>";
    }
    

    
?> 

 

WORKSHIFT CODE

<?php

//$query = "SELECT name,owner,species,sex,birth FROM pet WHERE name!='fluffy' ORDER BY name";
$header = "WORK SHIFTS";
$table = "workshift";
$query = "Select * from ".$table." where true ";

    $value = '';
    if (isset($_GET['value']))
    {
        $value = $_GET['value'];
    }

    $field = '';
    if (isset($_GET['field']))
    {
        $field = $_GET['field'];
    }

    $mode = $_GET['mode'];
    if ($mode == '')
    {
        $mode = 'search';
    }
    
$result = mysql_query ($query) or die("Query failed");

$rows = mysql_num_rows ($result);
$columns = mysql_num_fields($result);



?>



<script>
    function refresh()
    {        
        var sURL = unescape(window.location.href);
        //alert (sURL);
        window.location.href = sURL;
    }
    
    function viewall()
    {        
        var sURL = unescape(window.location.pathname);
        //alert (sURL);
        window.location.href = sURL+"?here=<?php echo $here ?>";
    }
    
    function insert_new()
    {        
        var sURL = unescape(window.location.pathname);
        //alert (sURL);
        window.location.href = sURL+"?here=<?php echo $here ?>&mode=insert";
    }
    
    function update_data(pino,gino)
    {        
        var sURL = unescape(window.location.pathname);
        //alert (sURL);
        window.location.href = sURL+"?here=<?php echo $here ?>&mode=update&field="+pino+"&value="+gino;
        //alert (sURL+"?here=<?php echo $here ?>&mode=update&field="+pino+"&value="+gino);
    }
    
    
    function setFormattedDate(pino)
    {
        window.open('content/creadata1.php?sender='+pino+'', 'none', config='width=275,height=130,resizable=no');
    }

    function submitForm1()
    {
        if (confirm("Do you really wish to insert this new record?"))
        {
        document.form1.submit();
        }
    }
    
    function submitForm2()
    {
        if (confirm("Do you really wish to uptade this record?"))
        {
        document.form2.submit();
        }
    }
    
        function submitForm3()
    {
        if (confirm("Do you really wish to view only those records matching these criteria?"))
        {
        document.form3.submit();
        }
    }
</script>


<div id=manage_content>
    <input type=button id=button1 value='Refresh' onclick="javascript:refresh();">
    <input type=button id=button1 value='Search table' onclick="javascript:viewall();">
    <input type=button id=button1 value='Insert new' onclick="javascript:insert_new();">
</div>

<div id=manage_form>
<?php
    $form_as_html = "<B>FIELDS</B> "."MODE = ".$mode." <P><table id='table2' width=100%>";
    
if ($mode == 'search')
{
    include("crud_single_table/search.php");
    echo $form_as_html;
}

if ($mode == 'insert')    
{
    include("crud_single_table/insert.php");
    echo $form_as_html ;
}    

if ($mode == 'confirm_insert')
{
    include("crud_single_table/confirm_insert.php");
    //echo $form_as_html;
}

if ($mode == 'update')
{
    include("crud_single_table/update.php");
    echo $form_as_html ;
}

if ($mode == 'confirm_update')
{
    include("crud_single_table/confirm_update.php");
    //echo $form_as_html;
}

if ($mode == 'confirm_search')
{
    include("crud_single_table/search.php");
    echo $form_as_html;
}
?>

</div>


<div id=list_content>

<?php
    include("crud_single_table/show_select.php");
    echo ($result_as_html);
    
?>

</div>
<?php
    mysql_close();
?> 

 

INSERT CODE

 

<?php
$form_as_html = $form_as_html . "<form name=form1 action=index.php?here=".$here."&mode=confirm_insert method=post>";
    
    $i = 0;
    for($i = 0; $i < $columns; $i++)
    {
        
        
        $form_as_html = $form_as_html ."<tr><td> ".mysql_field_name($result,$i);
        for ($j = 0; $j < count(mysql_field_flags($result,$i)); $j++)
        {
            if (strstr(mysql_field_flags($result,$i),'primary_key'))
            {
                $form_as_html = $form_as_html. "<td><input type=text name='input_".mysql_field_name($result,$i)."' readonly> ";
            }
            else if (mysql_field_type($result,$i)=='date')
            {
                $form_as_html = $form_as_html. "<td><input type=text name='input_".mysql_field_name($result,$i)."' id='input_".mysql_field_name($result,$i)."' value='".date('Y-m-d')."' readonly size=11><input type=button onclick=javascript:setFormattedDate('input_".mysql_field_name($result,$i)."') value='Date' id=button_date name=button_".mysql_field_name($result,$i)."> ";
            }
            else if (strstr(mysql_field_flags($result,$i),'binary') and !(strstr(mysql_field_flags($result,$i),'timestamp')))
            {
                $form_as_html = $form_as_html. "<td><select name='input_".mysql_field_name($result,$i)."'><option value=n>n<option value=y>y</select>";
            }
            else if (strstr(mysql_field_flags($result,$i),'binary') and (strstr(mysql_field_flags($result,$i),'timestamp')))
            {
                //$form_as_html = $form_as_html. "<td><select name='input_".mysql_field_name($result,$i)."'><option value=n>n<option value=y>y</select>";
                //$form_as_html = $form_as_html. "<td><input name='input_".mysql_field_name($result,$i)."' value='".mysql_field_flags($result,$i)."' readonly >";
                $form_as_html = $form_as_html. "<td><input name='input_".mysql_field_name($result,$i)."' value='' readonly >";
            }
            else
                //$form_as_html = $form_as_html. "<td><input type=text name='input_".mysql_field_name($result,$i)."' value='".mysql_field_type($result,$i)." ".mysql_field_flags($result,$i)."'> ";
                $form_as_html = $form_as_html. "<td><input type=text name='input_".mysql_field_name($result,$i)."' value=''> ";
        }
    }
    $form_as_html = $form_as_html . "</tr><input type=hidden name='flag_insert' value='OK'></table><input type=button onclick='javascript:submitForm1()' value='Insert' name=Submit1 id=button_date ></form>";
    
?> 

 

 

Link to comment
https://forums.phpfreaks.com/topic/141579-inserting-into-a-table-in-the-database/
Share on other sites

I can tell you how to trace this problem first at your end -  print the sql queries and run in phpmyadmin i.e your query browser and if it is related to query syntax error you can easily catch in the Query browser window.

 

If all queries you found is correct and then also you are facing some problem do let us know to help you.

 

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.