karnegyhall Posted April 21, 2009 Share Posted April 21, 2009 Hi i'm new to coding and php. Is there a problem with this code? I have checked and re-checked the host/db/table names. Getting error: "cannot select db: sportstalkadmin" <?php $athleteid = $_POST['name']; $conn = mysql_connect("sportstalkadmin.db.********.hostedresource.com", "(username)", "(password)"); if (!$conn) { die('Could not connect: ' . mysql_error()); } $select = mysql_select_db('sportstalkadmin', $conn); if ($select) mysql_query("INSERT INTO tbathletepics (AthleteID) VALUES ('$athleteid')"); { die('could not select db: sportstalkadmin ' . mysql_error()); } if ($select); echo "your data has been posted, testing complete.."; { die('your data was not posted. ' . mysql_error()); } $id = mysql_query("SELECT * FROM athletepics"); while($row = mysql_fetch_array($id)) { echo $row['AthleteID']; echo "<br />"; } mysql_close($conn); ?> Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/ Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted April 21, 2009 Share Posted April 21, 2009 Your if doesn't make sense. <?php ... if (!$select) { /* Code if $select is false */ } .... ?> Not this : <?php ... if (!$select) (???) <-- don't put code there { /* Code if $select is false */ } .... ?> Don't put a ; after a if like that if ($select); And you can use the [ code] [/code] tags when you post on forums it's easier to read. Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/#findComment-815897 Share on other sites More sharing options...
karnegyhall Posted April 21, 2009 Author Share Posted April 21, 2009 Ok, thank you for the help. I think i'm making progress. I changed the if statement to: <code> if ($select) { die('your data was not posted. ' . mysql_error()); } </code> now i'm getting the following error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/m/i/k/mikekarnas/html/TEST1.php on line 29 .. hate to bug you more, but i do appreciate your help. Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/#findComment-815911 Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted April 21, 2009 Share Posted April 21, 2009 INSERT INTO tbathletepics (AthleteID) VALUES ('$athleteid') SELECT * FROM athletepics You don't use the same table name ? Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/#findComment-815919 Share on other sites More sharing options...
karnegyhall Posted April 21, 2009 Author Share Posted April 21, 2009 [ code]INSERT INTO athletepics (AthleteID) VALUES ('$athleteid') SELECT * FROM athletepics made that change. error persists.. Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/#findComment-815934 Share on other sites More sharing options...
fenway Posted April 22, 2009 Share Posted April 22, 2009 Listen to the error... you didn't select a db. Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/#findComment-816384 Share on other sites More sharing options...
karnegyhall Posted April 22, 2009 Author Share Posted April 22, 2009 i've been testing in PHPMyAdmin on Godaddy.com and now the code looks like this. now i'm getting Parse error: syntax error, unexpected '>' in /home/content/m/i/k/mikekarnas/html/TEST1.php on line 34 <?php $athleteid = $_POST['name']; $conn = mysql_connect("sportstalkadmin.db.******.hostedresource.com", "(username)", "password"); if (!$conn) { die('Could not connect: ' . mysql_error()); } $select = mysql_select_db('sportstalkadmin', $conn); if (!$select) { die('could not select db: sportstalkadmin ' . mysql_error()); } $Ins = mysql_query("INSERT INTO athletepics (AtheleteID) VALUES ('$athleteid')"); if ($Ins) { die('your data was not posted. ' . mysql_error()); } $query = "SELECT intAtheleteID FROM tblathletepics"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) echo $row['intAtheleteID']. "; echo "<br />"; mysql_close($conn); Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/#findComment-816524 Share on other sites More sharing options...
karnegyhall Posted April 22, 2009 Author Share Posted April 22, 2009 did a bit more testing, and i've finally 'not' received any errors! however now my result is simply a '0'! does this have to do with my fetch statement? $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) echo $row['intAtheleteID']; mysql_close($conn); ?> Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/#findComment-816533 Share on other sites More sharing options...
fenway Posted April 22, 2009 Share Posted April 22, 2009 The code looks fine... Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/#findComment-816535 Share on other sites More sharing options...
karnegyhall Posted April 22, 2009 Author Share Posted April 22, 2009 latest update.. this is my script now. currently i am getting "your data was not posted". i've also included the form that $athleteid is getting Post data from. <?php $athleteid = $_POST['name']; $conn = mysql_connect("sportstalkadmin.db.******.hostedresource.com", "(username)", "password"); if (!$conn) { die('Could not connect: ' . mysql_error()); } $select = mysql_select_db('sportstalkadmin', $conn); if (!$select) { die('could not select db: sportstalkadmin ' . mysql_error()); } $Ins = mysql_query("INSERT INTO tblathletepics (intAtheleteID) VALUES ('$athleteid')"); if ($Ins) { die('your data was not posted. ' . mysql_error()); } $query = "SELECT intAtheleteID FROM tblathletepics"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) echo $row['intAtheleteID']. "; echo "<br />"; mysql_close($conn); <html> <form action="TEST1.php" method="post"> Enter your Athlete ID: <input type="text" name="name" /> <input type="submit" /> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/#findComment-816558 Share on other sites More sharing options...
fenway Posted April 22, 2009 Share Posted April 22, 2009 mysql_query() will return TRUE on success... don't know why you think otherwise. Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/#findComment-816580 Share on other sites More sharing options...
karnegyhall Posted April 22, 2009 Author Share Posted April 22, 2009 ok, i see your point. i looked in phpmyadmin and i see the intAtheleteID field Cardinality increasing. however, my code is still not right. what i want is to to post the data, then select the data that was just posted, and echo it to the screen. i just want to have confirmation that what i entered was posted. any suggestions? Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/#findComment-816601 Share on other sites More sharing options...
fenway Posted April 22, 2009 Share Posted April 22, 2009 Then get LAST_INSERT_ID() from mysql -- php has a function for this, I think -- and pull back the newly inserted record. Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/#findComment-816664 Share on other sites More sharing options...
karnegyhall Posted April 22, 2009 Author Share Posted April 22, 2009 man, i'm so close. this is what i get now: $query = "SELECT intAtheleteID FROM tblathletepics"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) echo $row['intAtheleteID']; $q = "SELECT LAST_INSERT_ID() FROM tblathletepics"; return ($q); i figured out it's outputting zeros in place of text. the 0's represent the default value for each entry i've submitted. so as far as I'm concerned you have fixed the php-mysql code problem. thank you. in php myadmin the default value shows 0 for the field i've selected b/c that's the way the dba who built my db made it. do you know how to change that in phpmyadmin by any chance? Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/#findComment-816751 Share on other sites More sharing options...
fenway Posted April 22, 2009 Share Posted April 22, 2009 You don't select last_insert_id "from" anywhere. See here for a better way. Link to comment https://forums.phpfreaks.com/topic/155105-newbie-getting-error-selecting-db/#findComment-816814 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.