Jump to content

HTML/PHP form to MySQL Database


xx_princess_xx

Recommended Posts

Sorry, I guess that talk isn't getting through. Let me try another method.

 

Okay princess, in your properties table, you have a field AID which is a FOREIGN KEY that references the AID in the agents table. Now, because you set it to NOT NULL, you *have* to give it a value when you INSERT new row into the table. The problem is how do we know what value to put? This is what I'm asking you because this is your system.

 

So is the user supposed to specify an AID or are you supposed to get it?

 

Does that help?

ok i understand,

 

the value has to be the same as AID in the agents table,

 

i have set the AID in the agents table to add 1 everytime a new record has been entered,

 

i would like it if the user can select an AID from a list already available,

 

so this makes that property linked to one specific agent,

 

 

Hi

 

I agree with Ken2k7 as a short term fix to get it working.

 

Longer term I presume you need a drop down list box for the user to select the AID.

 

As a slightly less short term solution:-

 

<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>Agent: <select name='aid'>
<option value='1'>Agent 1</option>
<option value='2'>Agent 2</option>
<option value='3'>Agent 3</option>
<option value='4'>Agent 4</option>
</select></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,Aid)
VALUES
('$_POST[type]','$_POST[price]','$_POST[address]', '$_POST[post_code]', '$_POST[photo]', '$_POST[isavailable]', '$_POST[aid]'),";

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

mysql_close($con)
?>

 

However longer term you need to generate a list of available agents from the database.

 

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

mysql_select_db("comreg098hassan", $con);
?>
<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>Agent: <select name='aid'>
<?php
$sql = "SELECT AID, ANAME FROM agents";
$cursor = mysql_query($sql,$con) or die(mysql_error());
while($row = mysql_fetch_assoc($cursor))
{
    echo "<option value='".$row['AID']."'>".$row['ANAME']."</option>";
}
?>
</select></p>
<p><input type="submit"/></p>
</fieldset>
</form>

 

All the best

 

Keith

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.