Jump to content

HTML/PHP form to MySQL Database


xx_princess_xx

Recommended Posts

I am really new to php and mysql,

 

I am doing a project for school work,

 

i am stuck at one particular point,

 

the database is of an estate agent, it has 4 tables,

agents table has an ID that is the primary key

this ID is the foriegn key in the properties table

 

i have created a html form that will insert property details into the table, it gives me an error something to do with the foriegn key,

 

in the form are you supposed you enter the foriegn key or is in supposed to be an option,

 

if so how do you do it.

 

any help will be really greatfull

Link to comment
Share on other sites

Hello princess,

Allowing people to enter in foreign keys is fine as long as they enter in the correct value. I much rather have them enter something else and use SQL to get the foreign key. But what's the error message?

 

Ken

Link to comment
Share on other sites

basically i want the user to be able to enter in property details from a form into the database

 

 

the error mesage i get is

 

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 neat "1" at line 3

 

Link to comment
Share on other sites

This is the two tables i created in sql

 

CREATE TABLE agents (

  AID int(5) NOT NULL auto_increment,

  ANAME char(50) NOT NULL default 'unknown',

  ADDRESS char(200) NOT NULL default 'unknown',

  POST_CODE char(7) NOT NULL default 'unknown',

  PRIMARY KEY (AID)

) ENGINE=INNODB AUTO_INCREMENT=1;

 

 

CREATE TABLE properties (

  PID int(5) NOT NULL auto_increment,

  TYPE char(20) NOT NULL default 'unknown',

  PRICE int(8) NOT NULL,

  ADDRESS char(200) NOT NULL default 'unknown',

  POST_CODE char(7) NOT NULL default 'unknown',

  PHOTO blob NOT NULL,

  ISAVAILABLE char(3) NOT NULL,

  AID int(5) NOT NULL,

  PRIMARY KEY (PID),

  FOREIGN KEY (AID) references agents(AID)

  ON UPDATE CASCADE ON DELETE CASCADE

) ENGINE=INNODB AUTO_INCREMENT=1;

 

this form is for users to enter property details

 

<form action="properties_insert2.php" method="post">

<fieldset>

<p>Type: <input type="text" name="type"/></p>

<p>Price: <input type="text" name="price"/></p>

<p>Address: <input type="text" name="address"/></p>

<p>Post Code: <input type="text" name="post_code"/></p>

<p>Photo: <input type="text" name="photo"/></p>

<p>Available: <input type="text" name="isavailable"/></p>

<p><input type="submit"/></p>

</fieldset>

</form>

 

this code sends the form data to the database, but in my case it doesn't

 

<?php

$con = mysql_connect("localhost","root","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("comreg098hassan", $con);

 

$sql="INSERT INTO Properties (Type, Price, Address, Post_Code, Photo, Isavailable)

VALUES

('$_POST[type]','$_POST[price]','$_POST[address]', '$_POST[post_code]', '$_POST[photo]', '$_POST[isavailable]'),";

 

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "1 record added";

 

mysql_close($con)

?>

 

 

Link to comment
Share on other sites

Also, your table name is properties not Properties. Case-sensitive.

 

The best thing for those NOT NULL table column is to give them a DEFAULT value if applicable.

 

One more thing, you can't interpolating array entries into a string like that. I suggest string concatenation. :)

Link to comment
Share on other sites

There are a few things and a few options.

 

To address kickstart's issue, you can give the columns in properties that are set to NOT NULL a DEFAULT value. OR, insert something not null in your SQL.

 

To address the one I posted, you can change this"

$sql="INSERT INTO Properties (Type, Price, Address, Post_Code, Photo, Isavailable)
VALUES ('$_POST[type]','$_POST[price]','$_POST[address]', '$_POST[post_code]', '$_POST[photo]', '$_POST[isavailable]'),";

 

To:

$sql="INSERT INTO Properties (Type, Price, Address, Post_Code, Photo, Isavailable)
VALUES
('" . $_POST['type'] . "','" . $_POST['price'] . "','" . $_POST['address'] . "', '" . $_POST['post_code'] . "', '" . $_POST['photo'] . "', '" . $_POST[isavailable] . "'),";

 

OH!!! One more thing, give the submit button a name and check for it before running that INSERT SQL. You don't want to insert unless the form is submitted.

Link to comment
Share on other sites

i added in test data directly into the database, but i dont think this is the issue,

 

all i want is the user to send the property details from the form to the database, this is not working, if i take out the foreign key it works, but my coursework says that it has to have this foreign key, what can i do to make this work?

Link to comment
Share on other sites

I have 2 tables "agents" and "properties"

 

I have created a form on my web page for users to send data but it does not work, i get an error which says

 

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 "1" at line 3

 

CREATE TABLE agents (
  AID int(5) NOT NULL auto_increment,
  ANAME char(50) NOT NULL default 'unknown',
  ADDRESS char(200) NOT NULL default 'unknown', 
  POST_CODE char(7) NOT NULL default 'unknown',
  PRIMARY KEY (AID)
) ENGINE=INNODB AUTO_INCREMENT=1;

 

CREATE TABLE properties (
  PID int(5) NOT NULL auto_increment,
  TYPE char(20) NOT NULL default 'unknown',
  PRICE int( NOT NULL,
  ADDRESS char(200) NOT NULL default 'unknown',
  POST_CODE char(7) NOT NULL default 'unknown',
  PHOTO blob NOT NULL,
  ISAVAILABLE char(3) NOT NULL,
  AID int(5) NOT NULL,
  PRIMARY KEY (PID),
  FOREIGN KEY (AID) references agents(AID)
  ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=INNODB AUTO_INCREMENT=1;

 

<form action="properties_insert2.php" method="post">
<fieldset>
<p>Type: <input type="text" name="type"/></p>
<p>Price:	<input type="text" name="price"/></p>
<p>Address: <input type="text" name="address"/></p>
<p>Post Code: <input type="text" name="post_code"/></p>
<p>Photo: <input type="text" name="photo"/></p>
<p>Available: <input type="text" name="isavailable"/></p>
<p><input type="submit"/></p>
</fieldset>
</form>

 

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("comreg098hassan", $con);

$sql="INSERT INTO Properties (Type, Price, Address, Post_Code, Photo, Isavailable)
VALUES
('$_POST[type]','$_POST[price]','$_POST[address]', '$_POST[post_code]', '$_POST[photo]', '$_POST[isavailable]'),";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>

 

when i add echo it gives me the same error just longer,

 

when i type in test data in my database for agents nothing appears in the foreign key in the property table,

 

sorry about the post before, i was just a little stressed ive been trying to solve this all day

Link to comment
Share on other sites

Error in INSERT INTO Properties (Type, Price, Address, Post_Code, Photo, Isavailable) VALUES ('detatched','100','123 brick lane', 'aw4 6yy', 'yes', 'yes'), hereYou 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 3

Link to comment
Share on other sites

  FOREIGN KEY (AID) references agents(AID)

 

this is the code in the sql

 

is it wrong?

 

how do i make it wright?

 

in the form do i add an option saying please select agent ID (a list of ID's from a drop menu)

then enter property details from a form

 

if so do you know how to do this or do you know a good tutorial

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.