shawn10642 Posted May 31, 2011 Share Posted May 31, 2011 This is reguarding a weather script so i got this basic sql script CREATE TABLE `cities` ( `id` int(11) NOT NULL auto_increment, `city_name` varchar(100) NOT NULL default '', `city_code` varchar( NOT NULL default '', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=240 ; -- -- Dumping data for table `countries` -- INSERT INTO `cities` VALUES (1, 'Vancouver', 'BC-50'); now that's all done i need to connect to the database, via php so <?php // Connect to database server $hd = mysql_connect("localhost", "username", "password") or die ("Unable to connect"); // Select database mysql_select_db ("weather", $hd) or die ("Unable to select database"); // Select info $res = mysql_query("SELECT * FROM cities", $hd) or die ("Unable to run query"); <NEED HELP HERE> ?> Now under the NEED HELP HERE i need the script to run a form of cities that's auto populated by the sql DB above, and when that is selected, i need it to POST the city code BC-50, and that is the directed to another script with is already setup, can someone help me please Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/ Share on other sites More sharing options...
Maq Posted May 31, 2011 Share Posted May 31, 2011 In the future, please place tags around your code. To loop through the results, use mysql_fetch_array. In pseudo your code will look like: echo ""; while(...) { echo "{$row['city']}"; } echo ""; Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1222924 Share on other sites More sharing options...
shawn10642 Posted May 31, 2011 Author Share Posted May 31, 2011 I'm good at php, i've just hit a brain fart at the moment , i've been at this for 4 day's. Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1222932 Share on other sites More sharing options...
gristoi Posted May 31, 2011 Share Posted May 31, 2011 $res = mysql_query("SELECT * FROM cities", $hd) echo "<select>"; while($row = mysql_fetch_array($res)) { echo "<option value='{$row['code']}'>{$row['city']}</option>"; } echo "</select>"; Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1222934 Share on other sites More sharing options...
Maq Posted May 31, 2011 Share Posted May 31, 2011 I'm good at php, i've just hit a brain fart at the moment , i've been at this for 4 day's. Ok, well did that help you? Did you try it? Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1222935 Share on other sites More sharing options...
shawn10642 Posted May 31, 2011 Author Share Posted May 31, 2011 get me an "internal server error". Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1222939 Share on other sites More sharing options...
shawn10642 Posted May 31, 2011 Author Share Posted May 31, 2011 $res = mysql_query("SELECT * FROM cities", $hd) echo "<select>"; while($row = mysql_fetch_array($res)) { echo "<option value='{$row['code']}'>{$row['city']}</option>"; } echo "</select>"; Internal server error Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1222941 Share on other sites More sharing options...
Maq Posted May 31, 2011 Share Posted May 31, 2011 $res = mysql_query("SELECT * FROM cities", $hd) echo ""; while($row = mysql_fetch_array($res)) { echo "{$row['city']}"; } echo ""; Internal server error I don't think that has to do with the code. Usually you get that error when you have a malformed .htaccess, missing php.ini or something like that. Check your error logs, specifically apache. Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1222943 Share on other sites More sharing options...
shawn10642 Posted May 31, 2011 Author Share Posted May 31, 2011 $res = mysql_query("SELECT * FROM cities", $hd) echo "<select>"; while($row = mysql_fetch_array($res)) { echo "<option value='{$row['code']}'>{$row['city']}</option>"; } echo "</select>"; Internal server error OPPS double while($row = mysql_fetch_array($res)) it work, but not populating Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1222944 Share on other sites More sharing options...
shawn10642 Posted May 31, 2011 Author Share Posted May 31, 2011 ... i mean double $res = mysql_query("SELECT * FROM cities", $hd) Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1222945 Share on other sites More sharing options...
Maq Posted May 31, 2011 Share Posted May 31, 2011 Change this line to: echo "{$row['city_name']}"; Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1222946 Share on other sites More sharing options...
shawn10642 Posted May 31, 2011 Author Share Posted May 31, 2011 echo "<option value='{$row['city_code']}'>{$row['city_name']}</option>";}echo "</select> so this work to auto populate the form, but how do i POST the city_code. thanks again, sorry i'm being a total noob right now Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1222952 Share on other sites More sharing options...
Maq Posted May 31, 2011 Share Posted May 31, 2011 echo "{$row['city_name']}";}echo " so this work to auto populate the form, but how do i POST the city_code. thanks again, sorry i'm being a total noob right now You need a form with an action. Google and try it then come back for specific help. Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1222955 Share on other sites More sharing options...
gristoi Posted May 31, 2011 Share Posted May 31, 2011 $res = mysql_query("SELECT * FROM cities", $hd) echo "<form action ='' method='POST'>"; echo "<select>"; while($row = mysql_fetch_array($res)) { echo "<option value='{$row['city_code']}'>{$row['city_name']}</option>";}echo "</select> } echo "</select>"; echo "<input type='submit' name='submitForm' value="Submit"/>"; </form> Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1222957 Share on other sites More sharing options...
shawn10642 Posted May 31, 2011 Author Share Posted May 31, 2011 unfortunantly this doesn't work. $res = mysql_query("SELECT * FROM cities", $hd) echo "<form action ='' method='POST'>"; echo "<select>"; while($row = mysql_fetch_array($res)) { echo "<option value='{$row['city_code']}'>{$row['city_name']}</option>";}echo "</select> } echo "</select>"; echo "<input type='submit' name='submitForm' value="Submit"/>"; </form> Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1223005 Share on other sites More sharing options...
Maq Posted May 31, 2011 Share Posted May 31, 2011 There is an obvious error in the for loop. Place these 2 lines directly following your opening <?php tags: ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1223012 Share on other sites More sharing options...
gristoi Posted May 31, 2011 Share Posted May 31, 2011 Define 'didnt work'. Just given an ambiguous answer like this is not helping anyone, I am trying to help you here, but you need to help yourself aswell. How did you reach the conclusion that it didn't post? Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1223084 Share on other sites More sharing options...
shawn10642 Posted May 31, 2011 Author Share Posted May 31, 2011 getting internal server error Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1223102 Share on other sites More sharing options...
gristoi Posted May 31, 2011 Share Posted May 31, 2011 Paste the code You've got so far Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1223108 Share on other sites More sharing options...
shawn10642 Posted May 31, 2011 Author Share Posted May 31, 2011 <?php // Connect to database server $hd = mysql_connect("localhost", "root", "1064111") or die ("Unable to connect"); // Select database mysql_select_db ("weather", $hd) or die ("Unable to select database"); // Select info $res = mysql_query("SELECT * FROM cities", $hd) or die ("Unable to run query"); $res = mysql_query("SELECT * FROM cities", $hd) echo "<form action='' method='POST'>"; echo "<select>"; while($row = mysql_fetch_array($res)) { echo "<option value='{$row['city_code']}'>{$row['city_name']}</option>"; } echo "</select>"; echo "<input type='submit' name='submitForm' value="Submit"/>"; echo "</form>echo "</form>";echo "</form>"; <?php // Connect to database server $hd = mysql_connect("localhost", "root", "1064111") or die ("Unable to connect"); // Select database mysql_select_db ("weather", $hd) or die ("Unable to select database"); // Select info $res = mysql_query("SELECT * FROM cities", $hd) or die ("Unable to run query"); $res = mysql_query("SELECT * FROM cities", $hd) echo "<form action='' method='POST'>"; echo "<select>"; while($row = mysql_fetch_array($res)) { echo "<option value='{$row['city_code']}'>{$row['city_name']}</option>"; } echo "</select>"; echo "<input type='submit' name='submitForm' value="Submit"/>"; echo "</form>"; i knoe there's something wrong, but flustered right now :'( Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1223243 Share on other sites More sharing options...
shawn10642 Posted June 1, 2011 Author Share Posted June 1, 2011 <?php // Connect to database server $hd = mysql_connect("localhost", "root", "1064111") or die ("Unable to connect"); // Select database mysql_select_db ("weather", $hd) or die ("Unable to select database"); // Select info $res = mysql_query("SELECT * FROM cities", $hd) or die ("Unable to run query"); $res = mysql_query("SELECT * FROM cities", $hd) echo "<form action='' method='POST'>"; echo "<select>"; while($row = mysql_fetch_array($res)) { echo "<option value='{$row['city_code']}'>{$row['city_name']}</option>"; } echo "</select>"; echo "<input type='submit' name='submitForm' value="Submit"/>"; echo "</form>echo "</form>"; echo "</form>"; the other code above, had doubles. Quote Link to comment https://forums.phpfreaks.com/topic/238001-need-help-with-php/#findComment-1223270 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.