Thursday 22 March 2012


Problem: How to insert integer and string in SQL Query using PHP?
Impact: You can’t insert in DB if Data type aren’t match!
Solution: in Query syntax for integer data type use “ ” and for string data type use ‘ “ “ ‘.
Like:  
<?php
      if(isset($_POST['Insert']))
      {
            $id_sidebar = $_POST['id'];
            $title_sidebar = $_POST['title'];
            $body_sidebar = $_POST['body'];
            $url_sidebar = $_POST['url'];
            $insert = "INSERT INTO Sidebar(id_sidebar, title_sidebar, body_sidebar, url_link) VALUES (".$id_sidebar.",'".$title_sidebar."','".$body_sidebar."','".$url_sidebar."')";
      }
?>
<form name="myform"  method="post" action="newfile.php">
    ID:<input name="id" type="text" value="<?php echo $id_sidebar;?>"/><br /><br/>
      Title:<input name="title" type="text" value="<?php echo $title_sidebar;?>"/><br /><br/>
      Body:<input name="body" type="text" value="<?php echo $body_sidebar;?>"/><br /><br/>
      Url:<input name="url" type="text" value="<?php echo $url_sidebar;?>"/><br /><br/>
      <input type="submit" value="Insert" name="Insert"/>
</form>

In this sample data type for ‘id_sidebar’is integer and data type for ‘title_sidebar’ is string.

No comments:

Post a Comment