l3asturd Posted July 30, 2007 Share Posted July 30, 2007 OK I read the beginner tutorial about writing and reading strings to MySQL. I used the code and it worked fine. Now my goal is to write mutliple entries and read them. For example: Name: Mike Score: 10 Money:$25 <?php // database information $host = '10.6.166.34'; $user = 'TLB1'; $password = 'youdontneedthis'; $dbName = 'TLB1'; // connect and select the database $conn = mysql_connect($host, $user, $password) or die(mysql_error()); $db = mysql_select_db($dbName, $conn) or die(mysql_error()); // insert new entry in the database if entry submitted if (isset($_POST['newEntry']) && trim($_POST['newEntry']) != '') { // for easier variable handling... $newEntry = $_POST['newEntry']; // insert new entry into database $sql = "insert into TLB (Name) values ('$newEntry')"; $result = mysql_query($sql, $conn) or die(mysql_error()); } // end if new entry posted // insert new entry in the database if entry submitted if (isset($_POST['newEntry2']) && trim($_POST['newEntry2']) != '') { // for easier variable handling... $newEntry2 = $_POST['newEntry2']; // insert new entry into database $sql = "insert into TLB (Score) values ('$newEntry2')"; $result = mysql_query($sql, $conn) or die(mysql_error()); } // end if new entry posted // insert new entry in the database if entry submitted if (isset($_POST['newEntry3']) && trim($_POST['newEntry3']) != '') { // for easier variable handling... $newEntry3 = $_POST['newEntry3']; // insert new entry into database $sql = "insert into TLB (Purse) values ('$newEntry3')"; $result = mysql_query($sql, $conn) or die(mysql_error()); } // end if new entry posted // select all the entries from the table $sql = "select Name,Score,Purse from TLB"; $result = mysql_query($sql, $conn) or die(mysql_error()); // echo out the results to the screen while ($list = mysql_fetch_array($result)) { echo "1{$list['Name']} <br>"; echo "2{$list['Score']} <br>"; echo "3{$list['Purse']} <br>"; } // end while echo <<<FORMENT <br> Make a new entry: <br> <form action = "{$_SERVER['PHP_SELF']}" method = "post"> <input type = "text" name = "newEntry" maxlength="20" size = "10"> <br> <input type = "text" name = "newEntry2" maxlength="4" size = "10"> <br> <input type = "text" name = "newEntry3" maxlength="6" size = "10"> <br> <input type = "submit" value = "Add Entry"> </form> FORMENT; ?> I tried to do this from the original code, but it just produces a huge mess. I know I'm a noob, but can someone help? Either correct my code or point me to another tutorial that deals with this. I read the array tutorial but it just left me with more questions. Help please? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted July 30, 2007 Share Posted July 30, 2007 What about this? <?php // database information $host = '10.6.166.34'; $user = 'TLB1'; $password = 'youdontneedthis'; $dbName = 'TLB1'; // connect and select the database $conn = mysql_connect($host, $user, $password) or die(mysql_error()); $db = mysql_select_db($dbName, $conn) or die(mysql_error()); if(count($_POST)) { $data = array( 'Name' => $_POST['newEntry'], 'Score' => $_POST['newEntry2'], 'Purse' => $_POST['newEntry3'], ); $field_names = array(); $values = array(); foreach($data as $field_name => $value) { if(empty($value)) continue; $field_names[] = $field_name; $values[] = "'".mysql_real_escape_string($value, $conn)."'"; } $insert_result = mysql_query("INSERT INTO TLB (".join(',',$field_names).") VALUES(".join(',',$values).")", $conn); $select_result = mysql_query("SELECT Name,Score,Purse FROM TLB", $conn); while($list = mysql_fetch_assoc($result)) { echo <<<EOF 1{$list['Name']}<br /> 2{$list['Score']}<br /> 3{$list['Purse']}<br /> EOF; } } else { echo <<<FORMENT <br> Make a new entry: <br> <form action = "{$_SERVER['PHP_SELF']}" method = "post"> <input type = "text" name = "newEntry" maxlength="20" size = "10"> <br> <input type = "text" name = "newEntry2" maxlength="4" size = "10"> <br> <input type = "text" name = "newEntry3" maxlength="6" size = "10"> <br> <input type = "submit" value = "Add Entry"> </form> FORMENT; } ?> Why are you calling your text fields newEntry, newEntry2 and newEntry3? It would be much smarter to name them something descriptive such as "name". Quote Link to comment Share on other sites More sharing options...
l3asturd Posted July 30, 2007 Author Share Posted July 30, 2007 Thanks a lot Daniel for taking the time to analyze what I was doing. The code looks much more efficient and helps me to understand arrays a litte more. Unfortunately, I can't see the results of the code yet. When I ran the code I got the following errors: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/me/name2.php on line 33 The only thing I did was change the descriptors newentry, newentry2, newentry3 to name, score, and purse like you suggested. I didn't change the code otherwise. Is there something else I need to change? Thanks a ton. Quote Link to comment Share on other sites More sharing options...
l3asturd Posted July 30, 2007 Author Share Posted July 30, 2007 Can anyone see what's wrong with Daniels code? Specifically around line 33? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted July 30, 2007 Share Posted July 30, 2007 Ah, sorry... change while($list = mysql_fetch_assoc($result)) to while($list = mysql_fetch_assoc($select_result)) then it should work. Quote Link to comment Share on other sites More sharing options...
l3asturd Posted July 30, 2007 Author Share Posted July 30, 2007 Thanks again. It's working nicely. I removed the "else" after it echos the list so I can continue adding new data. I'm starting to understand....... Quote Link to comment 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.