Hi there,

I have written a page which is meant to allow a user to enter details of a new car for sale (via a form), and then the submission of this form should add the details to the database.
As far as I can see all the code is correct, yet I don't get a new entry.

The Form
------------

<form action="addCar.php" method="post">
            Category: <select name="Category">
                        <option>Classic</option>
                        <option>Modern</option>
                        <option>Range_Rover</option>
                      </select>
                      <br />
            Make:<input type="text" name="Make" value=""><br />
            Model: <input type="text" name="Model" value=""><br />
            Reg Year: <input type="text" name="Reg_Year" value=""><br />
            Reg Letter: <input type="text" name="Reg_Letter" value=""><br />
            Mileage: <input type="text" name="Mileage" value=""><br />
            Price: <input type="text" name="Price" value=""><br />
            Price Alternate: <input type="text" name="Price_Alternate" value=""><br />
            Description: <textarea cols="60" name="Description" rows="4"></textarea><br />
            Image 1: <input type="text" name="Image1" value="<? echo $Image1; ?>"><br />
            Image 2: <input type="text" name="Image2" value="<? echo $Image2; ?>"><br />
            Image 3: <input type="text" name="Image3" value="<? echo $Image3; ?>"><br />
            Image 4: <input type="text" name="Image4" value="<? echo $Image4; ?>"><br />
        <input type="Submit" value="Add" name="send">
    </form>

addCar.php
--------------

<?php
     
        $Category=$_POST['Category'];
        $Make=$_POST['Make'];
        $Model=$_POST['Model'];
        $Reg_Year=$_POST['Reg_Year'];
        $Reg_Letter=$_POST['Reg_Letter'];
        $Mileage=$_POST['Mileage'];
        $Price=$_POST['Price'];
        $Price_Alternate=$_POST['Price_Alternate'];
        $Description=$_POST['Description'];
        $Image1=$_POST['Image1'];
        $Image2=$_POST['Image2'];
        $Image3=$_POST['Image3'];
        $Image4=$_POST['Image4'];
    
        $username="usrname";
        $password="pwrd";
        $database="dbName";
        
        mysql_connect(localhost,$username,$password);
        @mysql_select_db($database) or die( "Unable to select database");

        $query="INSERT INTO cars (Category, Make, Model, Reg_Year, Reg_Letter, Mileage, Price, Price_Alternate, Description, Image1, Image2, Image3, Image4) VALUES ('$Category,'$Make','$Model','$Reg_Year','$Reg_Letter,'$Mileage','$Price','$Price_Alternate','$Description','$Image1','$Image2','$Image3','$Image4')";
        mysql_query($query);
        echo "Car Added";

        mysql_close();
    ?>

Is this a case of "can't see the wood for the tree's"? Am I missing something glaringly obvious?

Cheers guys,

Ed

Recommended Answers

All 11 Replies

you forgot quote ' ' in localhost.
mysql_connect('localhost',$username,$password);

I have other pages running without the ' ' around localhost, but I did try it just to be sure, and I still get the same problem.

Thanks for the suggestion though.

Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.

<?php

if(isset($_POST['send'])){  //<-- add this and the last bracket at the bottom of this page.

$Category=$_POST['Category'];
        $Make=$_POST['Make'];
        $Model=$_POST['Model'];
        $Reg_Year=$_POST['Reg_Year'];
        $Reg_Letter=$_POST['Reg_Letter'];
        $Mileage=$_POST['Mileage'];
        $Price=$_POST['Price'];
        $Price_Alternate=$_POST['Price_Alternate'];
        $Description=$_POST['Description'];
        $Image1=$_POST['Image1'];
        $Image2=$_POST['Image2'];
        $Image3=$_POST['Image3'];
        $Image4=$_POST['Image4'];
    
        $username="usrname";
        $password="pwrd";
        $database="dbName";
        
        mysql_connect(localhost,$username,$password);
        @mysql_select_db($database) or die( "Unable to select database");

        $query="INSERT INTO cars (Category, Make, Model, Reg_Year, Reg_Letter, Mileage, Price, Price_Alternate, Description, Image1, Image2, Image3, Image4) VALUES ('$Category,'$Make','$Model','$Reg_Year','$Reg_Letter,'$Mileage','$Price','$Price_Alternate','$Description','$Image1','$Image2','$Image3','$Image4')";
        mysql_query($query);
        echo "Car Added";

//        mysql_close(); //<-- you can remove this

      }

?>

Again, thanks for the suggestion, but that doesn't appear to work either. I don't get the echoed "Car added" message with that option either.

ok give me the structure of your cars database. I can try to my site.
you know how to export your database in SQL Format? Maybe something wrong with the data types.

you remove the @ in database query command @mysql_select_db so that you will see the error message.

I'm using phpMyAdmin, have options to export as CSV, SQL, Open Document Text?

Guessing the last one?

How do you want me to send it to you?
Just pasted into here?

use SQL paste here

-- phpMyAdmin SQL Dump
-- version 2.11.9.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Dec 15, 2009 at 09:10 PM
-- Server version: 5.0.85
-- PHP Version: 5.2.6

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: `edmundr1_carsForSale`
--

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

--
-- Table structure for table `cars`
--

CREATE TABLE IF NOT EXISTS `cars` (
`ID` int(4) NOT NULL auto_increment,
`Category` set('Classic','Modern','Range_Rover') NOT NULL,
`Make` varchar(30) default NULL,
`Model` varchar(20) default NULL,
`Reg_Year` year(4) default NULL,
`Reg_Letter` varchar(1) default NULL,
`Mileage` int(6) default NULL,
`Price` int(8) default NULL,
`Price_Alternate` varchar(20) default NULL,
`Description` varchar(150) default NULL,
`Image1` varchar(30) default 'blank.jpg',
`Image2` varchar(30) default 'blank.jpg',
`Image3` varchar(30) default 'blank.jpg',
`Image4` varchar(30) default 'blank.jpg',
KEY `ID` (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Cars For Sale' AUTO_INCREMENT=8 ;

--
-- Dumping data for table `cars`
--

INSERT INTO `cars` (`ID`, `Category`, `Make`, `Model`, `Reg_Year`, `Reg_Letter`, `Mileage`, `Price`, `Price_Alternate`, `Description`, `Image1`, `Image2`, `Image3`, `Image4`) VALUES
(2, 'Classic', 'Jaguar', 'E-Type', 1963, 'A', 65000, 80000, NULL, 'A very nice car', 'car.jpg', 'blank.jpg', 'blank.jpg', 'blank.jpg'),
(3, 'Classic', 'Jaguar', 'XJ', 1970, 'T', 20000, NULL, 'POA', 'Another fine example', 'car.jpg', 'blank.jpg', 'blank.jpg', 'blank.jpg'),
(4, 'Modern', 'Jaguar', 'XF', 2009, 'Z', 500, 100000, NULL, 'A brand new XF', 'car.jpg', 'blank.jpg', 'blank.jpg', 'blank.jpg'),
(5, 'Range_Rover', 'Range Rover', 'Sport', 2005, 'V', 15000, NULL, 'Coming Soon', 'Sports version of the Range Rover', 'car.jpg', 'blank.jpg', 'blank.jpg', 'blank.jpg');

In your $query '$Category' you forgot the right quote in $Category :-) see the green color.

you forgot one single quote'.

$query="INSERT INTO cars (Category, Make, Model, Reg_Year, Reg_Letter, Mileage, Price, Price_Alternate, Description, Image1, Image2, Image3, Image4) VALUES ('$Category,'$Make','$Model','$Reg_Year','$Reg_Letter,'$Mileage','$Price','$Price_Alternate','$Description','$Image1','$Image2','$Image3','$Image4')";

I'd actually missed two, another one should have been at the end of '$Reg_Letter'

But as it is, that hasn't fixed it either.

Actually, I have managed to solve it.
Having added the two missing ' back in, I put mysql_close(); back into it, took out the if statement and it works fine now.

It was just a simple case of missing two characters. Thanks for your help though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.