Jump to content

Submitting data to database with HTML form


joshgarrod

Recommended Posts

Hi, i have a script that is "supposed" to submit data to my databse, but it is not working. I can't figure out why, the error tells me it is something to do with my sql syntax?

 

<?php
error_reporting (E_ALL ^ E_NOTICE);
    $usr = "ust";
    $pwd = "pword";
    $db = "db";
    $host = "ipad";

    # connect to database
    $cid = mysql_connect($host,$usr,$pwd);
    if (!$cid) { echo("ERROR: " . mysql_error() . "\n");    }

    if ($_POST['submit']) {

	$title = mysql_real_escape_string($_POST['title']);
	$desc = mysql_real_escape_string($_POST['desc']);
	$partno = mysql_real_escape_string($_POST['partno']);
	$price = mysql_real_escape_string($_POST['price']);
	$avail = mysql_real_escape_string($_POST['avail']);
	$image = mysql_real_escape_string($_POST['image']);

        $SQL = " INSERT INTO spares";
        $SQL .= " (title, desc, partno, price, avail, image) VALUES ";
        $SQL .= " ('$title', '$desc', '$partno', '$price', '$avail', '$image') ";
	      
        $result = mysql_db_query($db,$SQL,$cid);
        
        if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n");    }

        echo ("<P><B>Your itemhas been added to our classified section</B></P>\n");

    }
?>

First, I would remove whitespace from in front of INSERT, and it might be printing an extra whitespace between VALUES and the ( on the next line.

 

Also, I format my SQL queries a little differently.  If I was doing it, the third SQL line would look like this:

 

$SQL .= "('" . $title . "'. '" . $desc . "', '" . $partno . "', '" . $price . "', '" . $avail . "'. '" $image . "')";

 

That may be what is causing it also.

 

Try echo $SQL; before your mysql die section to see if it is formatting your SQL the way it needs to be inserted into the database.

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.