PHP / MySQL

Posted by Ferrari_Fuhrer 
PHP / MySQL
Date: August 01, 2006 09:59PM
Posted by: Ferrari_Fuhrer
I am working on a new system to test on my PWS. I'm trying to do as follows:

1) Build a form. Using a drop-down menu, select one item, click 'submit'. (This is done.)
2) Another file processes this form, assigning a variable to the data from the form. This is now referred to as '$table_row'. (This is done.)
3) I now connect to a database as the information from the form, using this variable, is to be used to display certain information from the database. (This is done.)
4) I extract the information from the database using the variable. (This I have not managed.)

The script I currently have is displayed below:




<h1>PHP Processing Form</h1>

<?php

$table_row = $_POST['table_row'];

echo "You selected to view the '". $table_row . "' " . row . ".<br /><br />";
echo "If this is not correct, there is a problem with the script. Please note any problem for future reference";

?>

<br/><br/>


<?php

$link = mysql_connect("localhost", "root", "JoQuavKiks1";);

mysql_select_db("savage";);

$sql = mysql_query("SELECT $table_row FROM links;";);
while ($r = mysql_fetch_array($sql))

{

echo $r['table_row'];

}

mysql_close($link);

?>




My problem is one, or more likely both, of two places.

1) If I were wanting to select a particular row in the database, eg. text, I would use 'SELECT text FROM links', but I want it to select one of three, based on the result of the previous form. The rows that could be selected are 'link', 'text' and 'title', which are the three possible values of '$table_row'. I suspect I have called '$table_row' incorrectly here. How should I call it in this statement?

2) The last 'echo' statement. '$r' should be the list of results from the table row selected in the previous statement, thus they are the results from 'link', 'text' or 'title', in other words, the results from the '$table_row' variable. I don't think I've called this correctly.

Is anyone able to help? It's not a simple question, but it's worth a try :)

[Website]
Re: PHP / MySQL
Date: August 01, 2006 10:12PM
Posted by: stephan
another example for learning..

<?php
 // Connecting, selecting database
 $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password';)
    or die('Could not connect: ' . mysql_error());
 echo 'Connected successfully';
 mysql_select_db('my_database';) or die('Could not select database';);
 
 // Performing SQL query
 $query = 'SELECT * FROM my_table';
 $result = mysql_query($query) or die('Query failed: ' . mysql_error());
 
 // Printing results in HTML
 echo "<table>\n";
 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
 }
 echo "</table>\n";
 
 // Free resultset
 mysql_free_result($result);
 
 // Closing connection
 mysql_close($link);
 ?>
Sorry, only registered users may post in this forum.

Click here to login

Maintainer: mortal, stephan | Design: stephan, Lo2k | Moderatoren: mortal, TomMK, Noog, stephan | Downloads: Lo2k | Supported by: Atlassian Experts Berlin | Forum Rules | Policy