Jump to content

[SOLVED] Query Array n stuff


l3asturd

Recommended Posts

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?

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.