Jump to content

Inserting Data in MySQL Datebase


liamk

Recommended Posts

Code;

 

-- phpMyAdmin SQL Dump
-- version 3.4.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 23, 2012 at 02:55 PM
-- Server version: 5.5.16
-- PHP Version: 5.3.8

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `stock`
--

-- --------------------------------------------------------

--
-- Table structure for table `stocktransfers`
--

CREATE TABLE IF NOT EXISTS `stocktransfers` (
  `id` tinyint(11) NOT NULL AUTO_INCREMENT,
  `brands` varchar(250) NOT NULL,
  `season` varchar(250) NOT NULL,
  `name` varchar(250) NOT NULL,
  `tillcode` varchar(250) NOT NULL,
  `departments` varchar(250) NOT NULL,
  `size` varchar(250) NOT NULL,
  `quantity` varchar(250) NOT NULL,
  `color` varchar(250) NOT NULL,
  `auth` varchar(250) NOT NULL,
  `status` varchar(250) NOT NULL,
  `from` varchar(250) NOT NULL,
  `to` varchar(250) NOT NULL,
  `date` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

 

SQL Data Structure

 

CREATE TABLE IF NOT EXISTS `stocktransfers` (
  `id` tinyint(11) NOT NULL AUTO_INCREMENT,
  `brands` varchar(250) NOT NULL,
  `season` varchar(250) NOT NULL,
  `name` varchar(250) NOT NULL,
  `tillcode` varchar(250) NOT NULL,
  `departments` varchar(250) NOT NULL,
  `size` varchar(250) NOT NULL,
  `quantity` varchar(250) NOT NULL,
  `color` varchar(250) NOT NULL,
  `auth` varchar(250) NOT NULL,
  `status` varchar(250) NOT NULL,
  `from` varchar(250) NOT NULL,
  `to` varchar(250) NOT NULL,
  `date` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

 

No errors appear just cant seem to get this to insert the data everthing else i have created so far works just not that :\

 

Your help would be appreciated.

 

Thanks,

Liam.

Link to comment
Share on other sites

Sorry i noticed i never entered the php code my bad.

 

<?php
    ob_start();
    
    if (isset($_POST['add'])) {
    
    $brands= $_POST['brands'];
    $season= $_POST['season'];
    $name= $_POST['name'];
    $tillcode = $_POST['tillcode'];
    $departments = $_POST['departments'];
    $size = $_POST['size'];
    $quantity = $_POST['quantity'];
    $color = $_POST['color'];
    $auth = $_POST['auth'];
    $status = $_POST['status'];
    $from = $_POST['from'];
    $to = $_POST['to'];
    $date = $_POST['date'];
    
    $query = mysql_query("INSERTINTO stocktransfers (brands, season, name, tillcode, departments, size, quantity, color, auth, status, from, to, date) VALUES('$brands','$season','$name','$tillcode','$departments','$size','$quantity','$color','$auth,','$status','$from','$to','$date')");
    
    echo "<meta http-equiv='refresh' content='0;url=page.php?id=stocktransfers' />";
    }
    else
    {
    ?>
        
        <fieldset>
        <legend>Post Request</legend>
        <form method='POST'>
        <label>Brand:</label> 
        
        <?php
        
        $sql = "SELECT brands FROM brands ORDER BY brands";
        $result = mysql_query($sql);
        
        echo "<select name='brands'>";
        while ($row = mysql_fetch_array($result)) {
            echo "<option value='" . $row['brands'] . "'>" . $row['brands'] . "</option>";
        }
        echo "</select>";
        
        ?>
        <br />
        <label>Season:</label> 
        
        <?php
        
        $sql = "SELECT season FROM season";
        $result = mysql_query($sql);
        
        echo "<select name='season'>";
        while ($row = mysql_fetch_array($result)) {
            echo "<option value='" . $row['season'] . "'>" . $row['season'] . "</option>";
        }
        echo "</select>";
        
        ?>
        <br />
        <label>Product Name:</label> <input type='text' size='35' maxlength='255' name='name' /><br />
        <label>Till Code:</label> <input type='text' size='35' maxlength='35' name='tillcode' /><br />
        <label>Color:</label> <input type='text' size='35' maxlength='25' name='color' /><br />
        <label>Size:</label> <input type='text' size='35' maxlength='25' name='size' /><br />
        <label>Quantity:</label> <input type='text' size='35' maxlength='25' name='quantity' /><br />
        <label>Department:</label> 
        
        <?php
        
        $sql = "SELECT departments FROM departments";
        $result = mysql_query($sql);
        
        echo "<select name='departments'>";
        while ($row = mysql_fetch_array($result)) {
            echo "<option value='" . $row['departments'] . "'>" . $row['departments'] . "</option>";
        }
        echo "</select>";
        
        ?>
        <br />
        <label>Authorized By:</label> <input type='text' size='35' maxlength='25' name='auth' /><br />
        <label>Status:</label> 
        
        <select name='status'>
        <option value='pending'>Pending</option>
        <option value='completed'>Completed</option>
        </select>
        <br />
        
        <label>From:</label> 
        
        <?php
        
        $sql = "SELECT branch FROM branch";
        $result = mysql_query($sql);
        
        echo "<select name='from'>";
        while ($row = mysql_fetch_array($result)) {
            echo "<option value='" . $row['branch'] . "'>" . $row['branch'] . "</option>";
        }
        echo "</select>";
        
        ?>
        <br />
        
        <label>To:</label> 
        
        <?php
        
        $sql = "SELECT branch FROM branch";
        $result = mysql_query($sql);
        
        echo "<select name='to'>";
        while ($row = mysql_fetch_array($result)) {
            echo "<option value='" . $row['branch'] . "'>" . $row['branch'] . "</option>";
        }
        echo "</select>";
        
        ?>
        <br />
        
        <label>Date:</label> <input readonly="readonly"type='text' name='date' size='35' value='<?php echo date("D M d, Y G:i a"); ?>' /><br />
        
        <input name='add' type='submit' value='Post Request' />
        </form>
        </fieldset>
    <?
    }
    ?>

Link to comment
Share on other sites

Add:

 

 or die(mysql_error());

 

to the end of your query, like so:

 

$query = mysql_query("INSERT INTO stocktransfers (brands, season, name, tillcode, departments, size, quantity, color, auth, status, from, to, date) VALUES('$brands','$season','$name','$tillcode','$departments','$size','$quantity','$color','$auth,','$status','$from','$to','$date')") or die(mysql_error());

 

and see if any errors come up.

Link to comment
Share on other sites

I got this error

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from, to, date) VALUES('47 Brand','SS11','sada2','asd','T-Shirts','sad','sad','a' at line 1

 

Any ideas?

Link to comment
Share on other sites

Just looking quickly, you are using MySQL Reserved Words as column names in your database table.

 

from and to are reserved words, and need to be encapsulated with ticks.  Using `ticks` are good practice, but are rarely encouraged.  And this is the issue noobs will find themselves in when not learning the real basics.

 

Using error control also helps as you would have seen that there was a syntax error in your query had you used mysql_error() to begin with.

 

So, add ticks to your column/field names like so:

 

$query = mysql_query("INSERT INTO `stocktransfers` (`brands`, `season`, `name`, `tillcode`, `departments`, `size`, `quantity`, `color`, `auth`, `status`, `from`, `to`, `date`) VALUES('$brands'.....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.