0

I am having a problem inserting values from a selection of dropdown boxes into MYSQL database.

I think I have narrowed it down to a problem in the .sql file. I have sat for hours trying to figure this out but I am a complete beginner to all this and may be missing something very small.

mysql_error() shows:

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 '', '', '', '')' at line 1

SQL file:

-- phpMyAdmin SQL Dump
-- version 3.3.9
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jun 27, 2012 at 10:06 PM
-- Server version: 5.5.8
-- PHP Version: 5.3.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


/*!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: `chilli`
--
DROP DATABASE IF EXISTS `chilli`;
CREATE DATABASE `chilli` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `chilli`;

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

--
-- Table structure for table `survey`
--

DROP TABLE IF EXISTS `survey`;
CREATE TABLE IF NOT EXISTS `survey` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `videos` varchar(50) NOT NULL,
  `scoville` varchar(50) NOT NULL,
  `extract` varchar(50) NOT NULL,
  `onions` varchar(50) NOT NULL,
  `retail` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--
-- Dumping data for table `survey`
--

INSERT INTO `survey` (`id`, `username`, `videos`, `scoville`, `extract`, `onions`, `retail`) VALUES
(1, 'sean', 'Yes', '0-200,000 SHU', 'Yes', 'No', 'No');


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



--

-- Table structure for table `orders`

--



DROP TABLE IF EXISTS `orders`;

CREATE TABLE IF NOT EXISTS `orders` (

  `id` int(11) NOT NULL AUTO_INCREMENT,
  `address` varchar(50) NOT NULL,

  `mild` varchar(50) NOT NULL,

  `medium` varchar(50) NOT NULL,

  `hot` varchar(50) NOT NULL,

  `v_hot` varchar(50) NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;



--

-- Dumping data for table `orders`

--



INSERT INTO `orders` (`id`, `address`, `mild`, `medium`, `hot`, `v_hot`) VALUES

(1, 'Fairman Place', 'Ancho', 'Chipotle', 'Shabu Shabu', 'Infinity Chilli');

PHP code:

<?php require_once('Connections/localhost.php'); ?>

<?php


$add = $_POST['address'];
$mild = $_POST['mild'];
$medi = $_POST['medium'];
$hot = $_POST['hot'];
$v_hot = $_POST['v_hot'];


mysql_select_db($database_localhost,$localhost);

$q2 = "INSERT INTO orders VALUES (NULL, '$add', $mild', '$medi', '$hot', '$v_hot')";
$r2 = mysql_query($q2,$localhost);

if (mysql_affected_rows($localhost) == 1) {
    echo '<script type="text/javascript" language="javascript">
    alert("Your order has been placed! Thank You.");
    history.back();</script>';
    }

    else {
        //query failure
    echo mysql_error();
    echo '<script type="text/javascript" language="javascript">
    alert("The order could not be completed due to a system error");
    history.back();</script>';  
    }

mysql_close($localhost);


?>

Please Note

As you can see from the sql file there is two tables (survey & orders). The first table(survey) works fine, with close to identical PHP code. It adds records to table with no problem. But I can't seem to add values to the second table(orders).

From what I see from other peoples code on this site, my code may seem a little unorthodox so feel free to mention any tips to tidy the code.

This topic has been covered a few times from what I can see but I still can't seem to find a resolution by emulating the answers given.

Thanks in advance.

1 Answer 1

3

You're missing a single quote in your query:

$q2 = "INSERT INTO orders VALUES (NULL, '$add', $mild', '$medi', '$hot', '$v_hot')";
                                               ^
                                               Right here
Sign up to request clarification or add additional context in comments.

1 Comment

I think I hate you and love you in equal measure. Thank you very much, that did the trick. I will accept your answer when it allows me in 10 minutes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.