Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157264-htmlphp-form-to-mysql-database/page/2/#findComment-828910 Share on other sites More sharing options...
xx_princess_xx Posted May 7, 2009 Author Share Posted May 7, 2009 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, Quote Link to comment https://forums.phpfreaks.com/topic/157264-htmlphp-form-to-mysql-database/page/2/#findComment-828927 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 So if we create a list of AIDs for the user to select, the user would know what the AID stand for? They are just numbers. Quote Link to comment https://forums.phpfreaks.com/topic/157264-htmlphp-form-to-mysql-database/page/2/#findComment-828932 Share on other sites More sharing options...
xx_princess_xx Posted May 7, 2009 Author Share Posted May 7, 2009 the user would not know what they stand for because they are just number you are right, how else could i get this form to work, could i take away the not null from the foreign key or does that hav to be there? Quote Link to comment https://forums.phpfreaks.com/topic/157264-htmlphp-form-to-mysql-database/page/2/#findComment-828950 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 Yeah, for now, just get rid of NOT NULL to have it working. You can decide later how to handle it. Quote Link to comment https://forums.phpfreaks.com/topic/157264-htmlphp-form-to-mysql-database/page/2/#findComment-828955 Share on other sites More sharing options...
kickstart Posted May 7, 2009 Share Posted May 7, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/157264-htmlphp-form-to-mysql-database/page/2/#findComment-829004 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.